Private Sub SaveFile(ByVal reportNumber As String, ByVal logicFullFileName As String, ByVal reportType As String)
Dim fullFileName As String = Server.MapPath(logicFullFileName)
Dim downFile As FileInfo = New FileInfo(fullFileName)
If Not File.Exists(fullFileName) Then
Response.Write("The file not exist.")
Exit Sub
End If
Response.Clear()
Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(reportNumber & "." & reportType))
Response.AddHeader("Content-Length", downFile.Length.ToString())
Response.ContentType = "application/octet-stream"
Response.TransmitFile(fullFileName)
Response.End()
End Sub
Dim fullFileName As String = Server.MapPath(logicFullFileName)
Dim downFile As FileInfo = New FileInfo(fullFileName)
If Not File.Exists(fullFileName) Then
Response.Write("The file not exist.")
Exit Sub
End If
Response.Clear()
Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(reportNumber & "." & reportType))
Response.AddHeader("Content-Length", downFile.Length.ToString())
Response.ContentType = "application/octet-stream"
Response.TransmitFile(fullFileName)
Response.End()
End Sub

本文介绍了一个ASP.NET中用于文件下载的示例代码。该示例演示了如何使用Server.MapPath获取服务器上的文件路径,检查文件是否存在,并设置HTTP响应头以触发文件下载。通过这个例子,读者可以了解到实现文件下载功能的具体步骤。
4635

被折叠的 条评论
为什么被折叠?



