【发布时间】: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
我的班级有什么问题?
谢谢。
【问题讨论】: