在vb2005中,如何使用Net.HttpListener模拟web浏览器的文件下载?
当前程序的功能有点像Web服务器。如何从web浏览器启动客户端
询问文件我是否允许他们下载文件?目前,该方法将告诉浏览器有一个文件,但在客户端确认该文件以供下载后,该方法将失败。
下面是我试图使用的代码
Dim fname As New IO.FileInfo(cuser.Request.QueryString(itm))
hedlst.Add(Net.HttpRequestHeader.ContentType, "application/octet-stream")
hedlst.Add(Net.HttpRequestHeader.ContentEncoding, "UTF-8")
hedlst.Add("Content-Disposition", "attachment;filename=""" & fname.Name & """")
cuser.Response.ContentEncoding = System.Text.Encoding.UTF8
Me.Invoke(xnl, uname & " begining download of file : " & fname.FullName)
writeoutstream(cuser.Response, fname.OpenRead, hedlst)
Me.Invoke(xnl, uname & " downloaded file : " & fname.FullName)
Private Sub writeoutstream(ByRef uret As Net.HttpListenerResponse, ByRef outtxt As IO.Stream, ByVal headers As System.Net.WebHeaderCollection)
uret.SendChunked = True
uret.Headers = headers
outtxt.Position = 0
Dim cnl(outtxt.Length) As Byte
outtxt.Read(cnl, 0, cnl.Length)
uret.OutputStream.Write(cnl, 0, cnl.Length)
cnl = Nothing
outtxt.Dispose()
End Sub