FilePathResult不适用于swf文件?

时间:2014-11-01 05:42:21

标签: c# asp.net-mvc fileresult

我使用FileResult在我的网站上显示文件如下:

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);
}

它不适用于以下标记,浏览器将下载文件而不是显示它!

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

出了什么问题,我该如何解决?

1 个答案:

答案 0 :(得分:0)

最终,我找到了解决方案 我不得不改变行动如下:

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);
}