【问题标题】:Handling file download in ASP.NET在 ASP.NET 中处理文件下载
【发布时间】:2010-10-25 08:02:36
【问题描述】:

可以接受 C# 或 VB.NET 中的建议。

我有一个类来处理文件下载链接 ASP.NET 项目,如下所示:

Public Class AIUFileHandler    
    Public Shared Sub DownloadFileHandler(ByVal fileName As String, ByVal filePath As String)    
        Dim r As HttpResponse    
        r.ContentType = "application/vnd.openxmlformats-officedocument.presentationml.presentation"
        r.AddHeader("content-disposition", String.Format("attachment;filename={0}", fileName))
        r.TransmitFile(filePath)    
        r.[End]()    
    End Sub    
End Class

然后,我从 ASP.NET 页面后面的代码中调用该函数,如下所示:

Protected Sub btnGetForm_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnGetForm.Click
    Dim fileName = "test.docx"
    Dim filePath = Server.MapPath("~/pub/test.docx")  
    AIUFileHandler.DownloadFileHandler(fileName, filePath)
End Sub

我收到这样的错误消息:

对象引用未设置为对象的实例。

r.ContentType = "application/vnd.openxmlformats-officedocument.presentationml.presentation"

但是如果我这样使用它而不创建另一个类,它会起作用:

Protected Sub btnGetForm_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnGetForm.Click
    Dim fileName = "test.docx"
    Dim filePath = Server.MapPath("~/pub/test.docx")  
    Response.ContentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
    Response.AddHeader("content-disposition", String.Format("attachment;filename={0}", fileName))
    Response.TransmitFile(filePath)
    Response.[End]()  
End Sub

我的班级有什么问题?

谢谢。

【问题讨论】:

    标签: c# asp.net vb.net


    【解决方案1】:

    替换

    Dim r As HttpResponse
    

    Dim r as HttpResponse  = HttpContext.Current.Response
    

    AIUFileHandler 类中

    【讨论】:

      【解决方案2】:

      在使用之前,您需要在DownloadFileHandler 方法中初始化r 变量:

      Dim r As HttpResponse = HttpContext.Current.Response
      

      或将其作为参数传递:

      Public Class AIUFileHandler
          Public Shared Sub DownloadFileHandler(ByVal fileName As String, ByVal filePath As String, ByVal r as HttpResponse)
              r.ContentType = "application/vnd.openxmlformats-officedocument.presentationml.presentation"
              r.AddHeader("content-disposition", String.Format("attachment;filename={0}", fileName))
              r.TransmitFile(filePath)
              r.[End]()
          End Sub
      End Class
      

      并调用:

      Protected Sub btnGetForm_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnGetForm.Click
          Dim fileName = "test.docx"
          Dim filePath = Server.MapPath("~/pub/test.docx")
          AIUFileHandler.DownloadFileHandler(fileName, filePath, Response)
      End Sub
      

      【讨论】:

        【解决方案3】:

        顺便说一句,我使用下一个代码将文件作为附件发送给用户:

        var info = new FileInfo(Server.MapPath(path));
        Response.Clear();
        Response.AppendHeader("Content-Disposition", String.Concat("attachment; filename=", info.Name));
        Response.AppendHeader("Content-Length", info.Length.ToString(CultureInfo.InvariantCulture));
        Response.ContentType = type;
        Response.WriteFile(info.FullName, true);
        Response.End();
        

        如果目标文件是以编程方式生成的,您也可以将其包装到 try-finally 块中:

        var info = ..
        try
        {
            // do stuff
        }
        finally
        {
            File.Delete(info.FullName);
        }
        

        【讨论】:

          猜你喜欢
          • 2011-03-06
          • 1970-01-01
          • 2010-12-27
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-06-24
          相关资源
          最近更新 更多