+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / Some PDFs on our site are not returning the proper MIME type

Some PDFs on our site are not returning the proper MIME type

20 posts, 0 answered
  1. Keith
    Keith avatar
    67 posts
    Registered:
    13 Sep 2010
    09 Feb 2011
    Link to this post

    Hello,
    We are having problems with our website returning the proper MIME types for PDFs in our libraries in Sitefinity.  Some PDF files will return application/pdf, and a few are returning application/octet-stream.  This is causing problems with some of our users when they try to view PDFs on our site.

    The problem seems to be random, it occurs whether the files are in the same library together or not, and the date & time of upload doesn't seem to be relevant.  The libraries are all classified as "Document Libraries"

    I need to get this fixed so that they -all- return "application/pdf."

  2. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    30 Sep 2016
    09 Feb 2011
    Link to this post
    Hello Keith,

    There re two reasons for this behavior so you should check them

    1. The problem is with the MIME type of the file
    2. The MIME Type depends on the browser. It is possible that you experience this issue when you upload the file using Chrome browser. We use RadUpload for uploading items and from UploadedFile.Content we get the mime type. Under Server 2008  and Chrome the ASP.NET control might passed different mime type. Our RadUpload is a wrapper of ASP.NET HttpPostedFile and we are not changing anything. This is from where the differences might come.

    All the best,
    Ivan Dimitrov
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  3. Keith
    Keith avatar
    67 posts
    Registered:
    13 Sep 2010
    09 Feb 2011
    Link to this post
    Hi Ivan,
    Thank you for your prompt reply.
    It sounds like this might be what is going on, because the users that are reporting this issue are the same ones that are uploading the actual content.

    Is there any way that I can override PDFs in Sitefinity so that they always return "application/pdf?"
  4. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    30 Sep 2016
    09 Feb 2011
    Link to this post
    Hello Keith,

    We use RadUpload control which uses the UploadedFile.Content that is set by the ASP.NET control. If this is the case I am not able to provide you a solution, since it is a bug in the .NET control.

    In Sitefinity 4.0 we use Silverlight based upload control and we ensured that this issue does not appear there.

    All the best,
    Ivan Dimitrov
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  5. Keith
    Keith avatar
    67 posts
    Registered:
    13 Sep 2010
    09 Feb 2011
    Link to this post
    Is there any way I can override RadUpload so that for future uploads it forces "application/pdf" for any future uploads?
  6. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    30 Sep 2016
    10 Feb 2011
    Link to this post
    Hi Keith,

    Ok. A possible solution here would be to subscribe for LibrariesModule.Executing or Executed static events in Global.asax - Application_Start and get the type of the file from the file extension. Then you can set the MimeType of the IContent item to pdf explicitly.

    sample

    void Application_Start(object sender, EventArgs e)
    {
        // Code that runs on application startup
     
        Telerik.Libraries.LibraryManager.Executing += new EventHandler<Telerik.ExecutingEventArgs>(LibraryManager_Executing);
    }
     
    void LibraryManager_Executing(object sender, Telerik.ExecutingEventArgs e)
    {
        if (e.CommandName == "CreateContent" && e.CommandArguments is Telerik.Cms.Engine.IContent)
        {
            Telerik.Cms.Engine.IContent currentItem = ((Telerik.Cms.Engine.IContent)e.CommandArguments);
            currentItem.MimeType = "";
     
        }
    }


    Greetings,
    Ivan Dimitrov
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  7. Keith
    Keith avatar
    67 posts
    Registered:
    13 Sep 2010
    10 Feb 2011
    Link to this post
    I tried implementing your solution and it is not working.  It is not setting the mimetype explicitly.

    This is the code I used:

    void LibraryManager_Executing(object sender, Telerik.ExecutingEventArgs e)
        {
            if (e.CommandName == "CreateContent" && e.CommandArguments is Telerik.Cms.Engine.IContent)
            {
                Telerik.Cms.Engine.IContent currentItem = ((Telerik.Cms.Engine.IContent)e.CommandArguments);
                if (currentItem.GetMetaData("Extension").ToString().ToLower().Equals(".pdf"))
                    currentItem.MimeType = "application/pdf";
            }
        }
  8. Keith
    Keith avatar
    67 posts
    Registered:
    13 Sep 2010
    10 Feb 2011
    Link to this post
    I have also noticed, that even if I update the mime type in the CmsContentBase table in the database manually, if I later go in and edit the document via Images & Documents, the mime type is changed back to whatever it was before!
    Should I override the Executed event instead? Do I need to override whatever event is fired or CommandName is passed whenever an item is edited as well?
    I would like to figure out how to do this or some other way to resolve this issue.  Documentation of these events and most of the API in general is virtually nonexistent.
  9. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    30 Sep 2016
    10 Feb 2011
    Link to this post
    Hi Keith,

    You should also check for UpdateContent command name, so that you will handle the edit mode. You can subscribe for Executed event - it is called after the Executing.

    Best wishes,
    Ivan Dimitrov
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  10. Keith
    Keith avatar
    67 posts
    Registered:
    13 Sep 2010
    10 Feb 2011
    Link to this post
    Hi Ivan,

    Ok I am now subscribing to Executed and it seems to be almost working properly.

    I also modified my code:

    void LibraryManager_Executed(object sender, Telerik.ExecutedEventArgs e)
        {
            if ((e.CommandName == "CreateContent" || e.CommandName == "SaveContent") && e.Data is Telerik.Cms.Engine.IContent)
            {
                Telerik.Cms.Engine.IContent currentItem = ((Telerik.Cms.Engine.IContent)e.Data);
                if (currentItem.GetMetaData("Extension").ToString().ToLower().Equals(".pdf"))
                {
                    Telerik.Cms.Engine.ContentManager cm = new Telerik.Cms.Engine.ContentManager("Libraries");
                    Telerik.Cms.Engine.IContent thisItem = cm.GetCurrentState(currentItem.ID);
                    thisItem.MimeType = "application/pdf";
                    cm.SaveContent(thisItem);
                }
            }
        }

    The only problem I have now is if I also check for "UpdateContent" then every time I edit a document, the browser hangs, then the connection to the server resets.  An ASP.NET 2.0 runtime error is recorded in the logs.  Otherwise it works for uploading a new doc.
  11. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    30 Sep 2016
    10 Feb 2011
    Link to this post
    Hello Keith,

    I see that you have added SaveContent, not UpdateContent

    Best wishes,
    Ivan Dimitrov
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  12. Keith
    Keith avatar
    67 posts
    Registered:
    13 Sep 2010
    10 Feb 2011
    Link to this post
    Hi Ivan,

    The only problem I have now is if I also check for "UpdateContent" then every time I edit a document, the browser hangs, then the connection to the server resets.  An ASP.NET 2.0 runtime error is recorded in the logs.  Otherwise it works for uploading a new doc.
    ^ That is why I didn't add UpdateContent.
  13. Keith
    Keith avatar
    67 posts
    Registered:
    13 Sep 2010
    11 Feb 2011
    Link to this post
    Any help?
  14. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    30 Sep 2016
    15 Feb 2011
    Link to this post
    Hello Keith,

    1. You should use Executing event as initially suggested
    2. You should not call SaveContent method of the manager.

    Below is a code that changes the Name - metafield of a library item represented by IContent object when you update or create a new item.

    void ContentManager_Executing(object sender, Telerik.ExecutingEventArgs e)
    {
        if (e.CommandName == "UpdateContent" || e.CommandName == "CreateContent")
        {
            var manager = new Telerik.Cms.Engine.ContentManager("Libraries");
     
            Telerik.Cms.Engine.IContent c = manager.GetContent(((Telerik.Cms.Engine.Data.CmsContentBase)(e.CommandArguments)).ID);
            c.SetMetaData("Name", "QQQ");
            
     
        }
    }


    You are getting stack overflow exception, because you are calling SaveContent in the Executed event and this again calls the ContentManager_Executed

    Greetings,
    Ivan Dimitrov
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  15. Mike
    Mike avatar
    208 posts
    Registered:
    10 Dec 2007
    29 Mar 2011
    Link to this post
    Hi Ivan,

    I'm experiencing the exact same problem on Sitefinity 3.2.  PDFs end up as application/unknown.  I tried your approach (snippet below) but it doesn't seem to save the MimeType when I set it.  Of course, if I call SaveContent(), then I get a recursion stack overflow.  Here's my code (I tried both Executing and Executed):

    Attach the event handlers:

    void Application_Start(object sender, EventArgs e) 
    {
        // Code that runs on application startup
        Telerik.Libraries.LibraryManager.Executing += new EventHandler<Telerik.ExecutingEventArgs>(LibraryManager_Executing);
        Telerik.Libraries.LibraryManager.Executed += new EventHandler<Telerik.ExecutedEventArgs>(LibraryManager_Executed);
    }

    And the event handlers themselves.

    void LibraryManager_Executing(object sender, Telerik.ExecutingEventArgs e)
    {
        object obj = e;
        if ((e.CommandName == "CreateContent" || e.CommandName == "UpdateContent") && e.CommandArguments is Telerik.Cms.Engine.IContent)
        {
            Telerik.Cms.Engine.IContent currentItem = ((Telerik.Cms.Engine.IContent)e.CommandArguments);
            if (currentItem.GetMetaData("Extension").ToString().ToLower().Equals(".pdf"))
            {
                Telerik.Cms.Engine.ContentManager cm = new Telerik.Cms.Engine.ContentManager("Libraries");
                Telerik.Cms.Engine.IContent thisItem = cm.GetCurrentState(currentItem.ID);
                thisItem.MimeType = "application/foo";
            }
        }
    }
    void LibraryManager_Executed(object sender, Telerik.ExecutedEventArgs e)
    {
        object obj = e;
        if ((e.CommandName == "CreateContent" || e.CommandName == "UpdateContent") && e.Data is Telerik.Cms.Engine.IContent)
        {
            Telerik.Cms.Engine.IContent currentItem = ((Telerik.Cms.Engine.IContent)e.Data);
            if (currentItem.GetMetaData("Extension").ToString().ToLower().Equals(".pdf"))
            {
                Telerik.Cms.Engine.ContentManager cm = new Telerik.Cms.Engine.ContentManager("Libraries");
                Telerik.Cms.Engine.IContent thisItem = cm.GetCurrentState(currentItem.ID);
                thisItem.MimeType = "application/foo";
            }
        }
    }


    Note, i'm setting "application/foo" just to see if this code can persist the change.  But when I look in sf_CmsContentBase, the MimeType hasn't been changed.

    Regards,
    Mike Sharp
  16. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    30 Sep 2016
    29 Mar 2011
    Link to this post
    Hello Mike,

    I do not remember if we changed the Executed and Executing events in 3.2, since this version was released 3 years ago, but I checked the code with the latest SP and
    "You are getting stack overflow exception, because you are calling SaveContent in the Executed event and this again calls the ContentManager_Executed"

    Regards,
    Ivan Dimitrov
    the Telerik team
  17. Mike
    Mike avatar
    208 posts
    Registered:
    10 Dec 2007
    29 Mar 2011
    Link to this post
    Hi Ivan,

    I understand the stack overflow, but how do I make changes to the item (like setting the MIME type) and persist them without causing the stack overflow.

    Again, I try to set the MIME type with that code (from your post), but the changes are not saved, no matter which event I use, Executing or Executed.

    Thanks,
    Mike
  18. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    30 Sep 2016
    01 Apr 2011
    Link to this post
    Hi Mike,

    The problem is that the life cycle for the libraries items is a bit different. In this case you should not call SaveChanges

    void ContentManager_Executing(object sender, Telerik.ExecutingEventArgs e)
        {
            if (e.CommandName == "UpdateContent" || e.CommandName == "CreateContent")
            {
                var manager = new Telerik.Cms.Engine.ContentManager(((Telerik.Cms.Engine.Data.CmsContentBase)(e.CommandArguments)).ProviderName);

                Telerik.Cms.Engine.IContent c = manager.GetContent(((Telerik.Cms.Engine.Data.CmsContentBase)(e.CommandArguments)).ID);
                if (c != null)
                {
                    var currentMime = c.MimeType;
                    c.MimeType = "application/octet-stream";
                }
            }
        }

    Kind regards,
    Ivan Dimitrov
    the Telerik team
  19. Mike
    Mike avatar
    208 posts
    Registered:
    10 Dec 2007
    02 Apr 2011
    Link to this post
    Ah, that's closer.  I can now get this problem fixed for e.CommandName == "UpdateContent" but not for new items. 

    Since manager.GetContent returns null for new content (it hasn't been saved yet), there's no opportunity to save the correct MimeType.  I tried doing it with the GetCurrentState method, but this doesn't seem to have any effect either.

    void ContentManager_Executing(object sender, Telerik.ExecutingEventArgs e)
    {
        if (e.CommandName == "UpdateContent" || e.CommandName == "CreateContent")
        {
            Telerik.Cms.Engine.ContentManager manager = new Telerik.Cms.Engine.ContentManager(((Telerik.Cms.Engine.Data.CmsContentBase)(e.CommandArguments)).ProviderName);
            Telerik.Cms.Engine.IContent c = manager.GetContent(((Telerik.Cms.Engine.Data.CmsContentBase)(e.CommandArguments)).ID);
            if (c != null && c.GetMetaData("Extension").ToString().ToLower().Equals(".pdf"))
            {
                c.MimeType = "application/pdf";
            }
            else
            {
                c = manager.GetCurrentState(((Telerik.Cms.Engine.Data.CmsContentBase)(e.CommandArguments)).ID);
                if (c != null && c.GetMetaData("Extension").ToString().ToLower().Equals(".pdf"))
                {
                    c.MimeType = "application/octet-stream";
                }
            }
                      
        }
    }

    Ideas?

    Regards,
    Mike Sharp
  20. Dan Sorensen
    Dan Sorensen avatar
    256 posts
    Registered:
    13 Aug 2010
    25 Apr 2012
    Link to this post
    Does anyone know if this bug between Chrome and Windows 2008 to mess up the MIME type on upload would still affect Sitefinity 5? (i.e. is Sitefinity 5 handling uploads in the same manner as Sitefinity 3 AND does the bug still exist between Chrome and Server 2008?)

    We are seeing a similar issue and wondered if this may be related.
Register for webinar
20 posts, 0 answered