【发布时间】:2011-01-01 18:22:26
【问题描述】:
我的目标是获取一些表单输入,并在单击某个按钮后提示用户下载所有内容的摘要。下载后我不需要该文件,因此我希望有一个解决方案,可以将数据直接流式传输给用户。我当前的解决方案甚至没有提示用户下载。谁能指出我做错了什么?
我已经把它包装成一个 web 方法,所以我需要添加一个 _Default 页面类的定义,这样我就可以访问某些东西了。
Public Shared Sub SaveText(ByVal Text As String)
Dim d As New _Default
Dim FileName As String = System.IO.Path.GetRandomFileName()
Using sw As New System.IO.StreamWriter(d.Server.MapPath(FileName + ".txt"))
sw.WriteLine(Text)
sw.Close()
End Using
Dim fs As System.IO.FileStream = Nothing
fs = System.IO.File.Open(d.Server.MapPath(FileName + ".txt"), System.IO.FileMode.Open)
Dim btFile(fs.Length) As Byte
fs.Read(btFile, 0, fs.Length)
fs.Close()
With HttpContext.Current.Response
.AddHeader("Content-disposition", "attachment;filename=output.txt")
.AddHeader("Content-Length", btFile.Length.ToString)
.ContentType = "application/octet-stream"
.BinaryWrite(btFile)
.End()
End With
End Sub
抱歉,前面没有提到,但是 AJAX 请求正在调用 webmethod!
【问题讨论】: