【问题标题】:FilePathResult doesn't work for swf files?FilePathResult 不适用于 swf 文件?
【发布时间】:2014-11-04 02:26:58
【问题描述】:

我正在使用 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>

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

【问题讨论】:

  • 您检查过浏览器插件吗?安装flash插件并重新检查get.adobe.com/flashplayer
  • 我有最新版本的 chrome 38 Flash Player。
  • 尝试将 Response 标头的 Content-Disposition 属性模式设置为“内联”,看看是否有区别。
  • 这种行为(下载文件)在您返回 FileResult 时是正常的...您可以返回 json(new {FilePath=""});

标签: c# asp.net-mvc fileresult


【解决方案1】:

最终,我找到了解决方案。
我必须将操作更改如下:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-08
    • 1970-01-01
    相关资源
    最近更新 更多