简体   繁体   中英

Download file using vb.net issue

I am using the following code to download a server side file

Dim fileName As String = abc.Text
Dim thePath As String = "e:\myFolder\" & fileName
Response.Clear()
Response.ContentType = "application/octet-stream"
Response.AppendHeader("Content-Disposition", "filename=" & fileName)
Response.end

I have created the textfile written only for test But after downloading the file it has all the html scripts that are used in aspx page. Can any one explain why this happen? can response.Flush() solve this issue?

You can user hyper link which is better. In your code, you need to read data from file and then Response.Write(data) before end.

PRB: Response.WriteFile cannot download a large file

(virual folder of file path)

<a href="../filename.xxx">Download File</a>

I would suggest you use an .ashx file to provide file downloads rather than .aspx.

The default handler for .aspx files does a lot of stuff for you that you specifically don't want in this case, so it makes more sense to use a generic handler (.ashx) and add your desired functionality than to use the page handler and remove functionality from that.

This looks like a reasonable explanation of .ashx files: http://www.dotnetperls.com/ashx

And yes, definately be aware of the potential problems with using Response.WriteFile.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM