Search code examples
c#asp.net-mvcfileresult

FilePathResult doesn't work for swf files?


I'm using FileResult to show files in my web site as the following:

public ActionResult GetSwf(long id = 0)
{
    if (id <= 0) return null;
    Attachment attachment = Service.GetAttachmentById(id);
    if (attachment == null) return null;

    string filename = attachment.Name;
    string mimeType = "application/x-shockwave-flash";
    string absoluteFilePath = UploadedPaths.GetAbsolutePath(attachment.Path);

    return File(absoluteFilePath, mimeType, filename);
}

It doesn't work for the following tag and the browser is going to download file instead of show it!

<object type="application/x-shockwave-flash" width="220" height="166" data="File/GetSwf/1232" class="flash-object" data-name="ads-file"></object>

What's wrong, how can I fix it?


Solution

  • Eventually, I found the solution.
    I had to change the action as the following:

    public ActionResult GetSwf(long id = 0)
    {
        if (id <= 0) return null;
        Attachment attachment = Service.GetAttachmentById(id);
        if (attachment == null) return null;
    
        string filename = attachment.Name;
        string mimeType = "application/x-shockwave-flash";
        string absoluteFilePath = UploadedPaths.GetAbsolutePath(attachment.Path);
    
        byte[] bytes = System.IO.File.ReadAllBytes(absoluteFilePath);
        return new FileContentResult(bytes, mimeType);
    }