【问题标题】:File downloads in a self-host Nancy application自托管 Nancy 应用程序中的文件下载
【发布时间】:2013-12-05 23:13:51
【问题描述】:

我正在开发一个使用从 WPF 应用程序中托管的 Nancy 的小项目。我希望能够远程下载大约 8MB 的 PDF 文件。我能够让下载工作,但在下载过程中,应用程序不会响应任何其他请求。有没有办法让我在不占用所有其他请求的情况下允许文件下载?

Public Class ManualsModule : Inherits NancyModule
    Public Sub New()
        MyBase.New("/Manuals")

        Me.Get("/") = Function(p)
            Dim model As New List(Of String) From {"electrical", "opmaint", "parts"}
            Return View("Manuals", model)
        End Function

        Me.Get("/{name}") = Function(p)
            Dim manualName = p.name
            Dim fileResponse As New GenericFileResponse(String.Format("Content\Manuals\{0}.pdf", manualName))
            Return fileResponse
        End Function
    End Sub
End Class

或者在 C# 中

public class ManualsModule : NancyModule
{
    public ManualsModule() : base("/Manuals")
    {
        this.Get("/") = p =>
        {
            List<string> model = new List<string> {
                "electrical",
                "opmaint",
                "parts"
            };

            return View("Manuals", model);
        };

        this.Get("/{name}") = p =>
        {
            dynamic manualName = p.name;
            GenericFileResponse fileResponse = new GenericFileResponse(string.Format("Content\\Manuals\\{0}.pdf", manualName));
            return fileResponse;
        };
    }
}

【问题讨论】:

    标签: nancy


    【解决方案1】:

    如果你想将文件作为附件,Monivs 的答案非常有用,如果你想直接在浏览器中打开 pdf,你可以这样做:

    MemoryStream ms = new MemoryStream(documentBody);
    var response = new Response();
    response.ContentType = "application/pdf";
    response.Contents = stream => {
        ms.WriteTo(stream);
    };
    return response;
    

    【讨论】:

      【解决方案2】:

      最简单的方法是围绕它创建一个 StreamWriter,如下所示:

      var response = new Response();
      
      response.Headers.Add("Content-Disposition", "attachment; filename=test.txt");
      response.ContentType = "text/plain";
      response.Contents = stream => {
          using (var writer = new StreamWriter(stream))
          {
              writer.Write("Hello");
          }
      };
      
      return response;
      

      【讨论】:

        【解决方案3】:
        var file = new FileStream(zipPath, FileMode.Open);
        string fileName = //set a filename
        
        var response = new StreamResponse(() => file, MimeTypes.GetMimeType(fileName));
        return response.AsAttachment(fileName);
        

        【讨论】:

        • 愚蠢的问题:由于您不能在 return 语句之前关闭流 - 在 Nancy 完成后流会自动关闭吗?我想没关系..
        • 流应该由委托创建。这可能是StreamResponse需要一个动作而不是流对象的原因:new StreamResponse(() =&gt; new FileStream(zipPath, FileMode.Open), MimeTypes.GetMimeType(fileName));
        【解决方案4】:

        我发现我实际上是在 WCF 中托管 Nancy,而不是自托管。我描述的行为仅在托管在 WCF 中时才会发生。 Self-Host 将适用于我的应用程序,所以我会继续这样做。

        【讨论】:

        • 问题可能是围绕最大消息大小的 WCF 的神秘配置 - 但如果自托管为您工作,无论如何,这是一个更好的选择 imo :)
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-07-30
        • 1970-01-01
        • 2016-12-24
        • 1970-01-01
        • 2018-04-02
        • 2020-09-16
        • 1970-01-01
        相关资源
        最近更新 更多