Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • zip 3 posts 24 karma points
    Nov 19, 2009 @ 02:24
    zip
    0

    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.

     

     

  • Sebastiaan Janssen 5060 posts 15522 karma points MVP admin hq
    Nov 19, 2009 @ 14:06
    Sebastiaan Janssen
    1

    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!).

  • zip 3 posts 24 karma points
    Nov 20, 2009 @ 03:06
    zip
    0

    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?

  • Connie DeCinko 931 posts 1160 karma points
    Feb 08, 2011 @ 20:18
    Connie DeCinko
    0

    Zip, how did you resolve this issue?

     

Please Sign in or register to post replies

Write your reply to:

Draft