Handling file download in ASP.NET
Suggestion in either in C# or VB.NET is acceptable.
I have a class to handle file download link ASP.NET project like the following:
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
Then, I call that function from a code behind of an ASP.NET page like this:
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
I got this error message like this:
Object reference not set to an instance of an object.
r.ContentType = "application/vnd.openxmlformats-officedocument.presentationml.presentation"
But if I use it like this without making another class, it works:
Protected Sub btnGetForm_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnGetForm.Click
Dim fileName = "test.docx"
Dim filePath = Server.开发者_如何学PythonMapPath("~/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
What's the problem with my class?
Thank you.
Replace
Dim r As HttpResponse
with
Dim r as HttpResponse = HttpContext.Current.Response
in AIUFileHandler
class
You need to initialize the the r
variable inside the DownloadFileHandler
method before using it:
Dim r As HttpResponse = HttpContext.Current.Response
or pass it as argument:
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
and to call:
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
btw, I use next code to send file to a user as an attachment:
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();
Also you can wrap it into try-finally
block if target file is programmatically generated:
var info = ..
try
{
// do stuff
}
finally
{
File.Delete(info.FullName);
}
精彩评论