首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在浏览器中使用ASP.NET MVC FileContentResult流文件吗?

在浏览器中使用ASP.NET MVC FileContentResult流文件吗?
EN

Stack Overflow用户
提问于 2010-07-09 02:16:14
回答 6查看 133.1K关注 0票数 63

有没有办法在浏览器中使用ASP.NET MVC FileContentResult以特定的名称流式传输文件?

我注意到,你可以有一个FileDialog (打开/保存),或者你可以在浏览器窗口中流式传输文件,但是当你尝试保存文件时,它将使用ActionName。

我有以下场景:

byte[] contents = DocumentServiceInstance.CreateDocument(orderId, EPrintTypes.Quote);
result = File(contents, "application/pdf", String.Format("Quote{0}.pdf", orderId));

当我使用它时,我可以流式传输字节,但是会给用户一个打开/保存文件的对话框。我想在浏览器窗口中实际流传输此文件。

如果我只是使用FilePathResult,它会在浏览器窗口中显示文件,但当我单击“保存”按钮将文件保存为PDF格式时,它会显示操作名称作为文件的名称。

有没有人遇到过这种情况?

EN

回答 6

Stack Overflow用户

发布于 2010-07-09 03:37:18

public ActionResult Index()
{
    byte[] contents = FetchPdfBytes();
    return File(contents, "application/pdf", "test.pdf");
}

为了在浏览器中打开PDF,你需要设置Content-Disposition头:

public ActionResult Index()
{
    byte[] contents = FetchPdfBytes();
    Response.AddHeader("Content-Disposition", "inline; filename=test.pdf");
    return File(contents, "application/pdf");
}
票数 95
EN

Stack Overflow用户

发布于 2013-04-15 18:34:44

实际上,最简单的方法是执行以下操作...

byte[] content = your_byte[];

FileContentResult result = new FileContentResult(content, "application/octet-stream") 
{
  FileDownloadName = "your_file_name"
};

return result;
票数 60
EN

Stack Overflow用户

发布于 2013-07-27 07:07:01

前面的答案是正确的:添加行...

Response.AddHeader("Content-Disposition", "inline; filename=[filename]");

导致多个内容部署标头向下发送到浏览器的...will。如果您提供文件名,b/c FileContentResult会在内部应用标头。另一种非常简单的替代解决方案是简单地创建FileContentResult的子类并覆盖其ExecuteResult()方法。下面是一个示例,它实例化System.Net.Mime.ContentDisposition类的一个实例(与内部FileContentResult实现中使用的对象相同),并将其传递给新类:

public class FileContentResultWithContentDisposition : FileContentResult
{
    private const string ContentDispositionHeaderName = "Content-Disposition";

    public FileContentResultWithContentDisposition(byte[] fileContents, string contentType, ContentDisposition contentDisposition)
        : base(fileContents, contentType)
    {
        // check for null or invalid ctor arguments
        ContentDisposition = contentDisposition;
    }

    public ContentDisposition ContentDisposition { get; private set; }

    public override void ExecuteResult(ControllerContext context)
    {
        // check for null or invalid method argument
        ContentDisposition.FileName = ContentDisposition.FileName ?? FileDownloadName;
        var response = context.HttpContext.Response;
        response.ContentType = ContentType;
        response.AddHeader(ContentDispositionHeaderName, ContentDisposition.ToString());
        WriteFile(response);
    }
}

在您的Controller或基本Controller中,您可以编写一个简单的帮助器来实例化FileContentResultWithContentDisposition,然后从操作方法中调用它,如下所示:

protected virtual FileContentResult File(byte[] fileContents, string contentType, ContentDisposition contentDisposition)
{
    var result = new FileContentResultWithContentDisposition(fileContents, contentType, contentDisposition);
    return result;
}

public ActionResult Report()
{
    // get a reference to your document or file
    // in this example the report exposes properties for
    // the byte[] data and content-type of the document
    var report = ...
    return File(report.Data, report.ContentType, new ContentDisposition {
        Inline = true,
        FileName = report.FileName
    });
}

现在,文件将以您选择的文件名和content-disposition头"inline;filename=filename“发送到浏览器。

我希望这对你有帮助!

票数 8
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3206682

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档