Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/275.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在ASP.NET中处理文件下载_C#_Asp.net_Vb.net - Fatal编程技术网

C# 在ASP.NET中处理文件下载

C# 在ASP.NET中处理文件下载,c#,asp.net,vb.net,C#,Asp.net,Vb.net,可以接受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 = "applicat

可以接受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
我收到如下错误消息:

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 of cedocument.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
我的班有什么问题


谢谢。

在使用
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
替换

Dim r As HttpResponse


AIUFileHandler
class

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

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);
}