How can I upload only ".pdf" files using upload datatype?
hi, in the "Media" section, I just want user to upload ".pdf" files only for "file" datatype, I tried to set validation expression in "upload" datatype to "^.+\.pdf$" in the "file" datatype, when I uploaded ".txt" file, it seems it can find the error with error message: Upload file at File is not in a correct format, but it not stops the uploading, so, even the error message pops, that ".txt" file still be uploaded successfully.
Does anyone know how to solve this problem? Thanks.
You might want to write a Media.BeforeSave eventhandler that checks the file. You can do a cancel before saving it. But I'm not sure that would prevent the file being written to disk.
If it still writes to disk, you could include a delete method in your actionhandler as well (but be ever so careful!).
using System.IO; using umbraco.BusinessLogic; using umbraco.cms.businesslogic.media;
namespace Sanitarium.VegeToolkit.Common { public class uevent:ApplicationBase { public uevent() { Media.BeforeSave += new Media.SaveEventHandler(Media_BeforeSave); Media.AfterSave += new Media.SaveEventHandler(Media_AfterSave); }
void Media_AfterSave(Media sender, umbraco.cms.businesslogic.SaveEventArgs e) { throw new Exception("The method or operation is not implemented."); }
void Media_BeforeSave(Media sender, umbraco.cms.businesslogic.SaveEventArgs e) { //throw new Exception("The method or operation is not implemented."); Media media=sender as Media; //get uploaded media file string file_location = media.getProperty("umbracoFile").Value.ToString(); //get uploaded file location string file_extension = media.getProperty("umbracoExtension").Value.ToString(); //get uploaded file extension string file_path = Path.Combine(umbraco.GlobalSettings.FullpathToRoot, file_location.Substring(1)); //get the uploaded file physical location if (!file_extension.Equals("pdf") && System.IO.File.Exists(file_path)) //if extension is not "pdf" and uploaded file exists { System.IO.File.Delete(file_path); media.getProperty("umbracoFile").Value = string.Empty; } } } }
it seems before calling "BeforeSave" event, the file already be uploaded, if I uncomments the "throw new exception..." in "Media_BeforeSave" function, I got the system error shows "BeforeSave" has been called after media "Save" call, don't know why, it doesn't make sense.
Also, when I run the code the "AfterSave" event never be called, is it a bug?
How can I upload only ".pdf" files using upload datatype?
hi, in the "Media" section, I just want user to upload ".pdf" files only for "file" datatype, I tried to set validation expression in "upload" datatype to "^.+\.pdf$" in the "file" datatype, when I uploaded ".txt" file, it seems it can find the error with error message: Upload file at File is not in a correct format, but it not stops the uploading, so, even the error message pops, that ".txt" file still be uploaded successfully.
Does anyone know how to solve this problem? Thanks.
You might want to write a Media.BeforeSave eventhandler that checks the file. You can do a cancel before saving it. But I'm not sure that would prevent the file being written to disk.
If it still writes to disk, you could include a delete method in your actionhandler as well (but be ever so careful!).
hi, thanks Sebastiaan, just for future reading of other readers, I just put the my solution code below:
=====================================================================================
using System.IO;
using umbraco.BusinessLogic;
using umbraco.cms.businesslogic.media;
namespace Sanitarium.VegeToolkit.Common
{
public class uevent:ApplicationBase
{
public uevent()
{
Media.BeforeSave += new Media.SaveEventHandler(Media_BeforeSave);
Media.AfterSave += new Media.SaveEventHandler(Media_AfterSave);
}
void Media_AfterSave(Media sender, umbraco.cms.businesslogic.SaveEventArgs e)
{
throw new Exception("The method or operation is not implemented.");
}
void Media_BeforeSave(Media sender, umbraco.cms.businesslogic.SaveEventArgs e)
{
//throw new Exception("The method or operation is not implemented.");
Media media=sender as Media; //get uploaded media file
string file_location = media.getProperty("umbracoFile").Value.ToString(); //get uploaded file location
string file_extension = media.getProperty("umbracoExtension").Value.ToString(); //get uploaded file extension
string file_path = Path.Combine(umbraco.GlobalSettings.FullpathToRoot, file_location.Substring(1)); //get the uploaded file physical location
if (!file_extension.Equals("pdf") && System.IO.File.Exists(file_path)) //if extension is not "pdf" and uploaded file exists
{
System.IO.File.Delete(file_path);
media.getProperty("umbracoFile").Value = string.Empty;
}
}
}
}
========================================================================================
it seems before calling "BeforeSave" event, the file already be uploaded, if I uncomments the "throw new exception..." in "Media_BeforeSave" function, I got the system error shows "BeforeSave" has been called after media "Save" call, don't know why, it doesn't make sense.
Also, when I run the code the "AfterSave" event never be called, is it a bug?
Zip, how did you resolve this issue?
is working on a reply...