Michael Miner Michael Miner - 1 year ago 55
Vb.net Question

Browsers Not Showing File Was Downloaded

For work I am working on a file export. The data all exports just fine but what is concerning is that no file dialog appears, as well there is no indication from the browser that the file has been downloaded.

My file download consists of two ajax handlers and some javascript.
My button has an onclick event, this calls

which places a spinner over the page and calls a Handler. This handler prepares the data and saves it to file.

Once that is done the javascript calls a second handler which retrieves the saved file and is supposed to send it to the user with a prompt for them to save it. The second handler is called and throws no errors.

My second (not working) handler is here:

baseDir = ProjectConfig.BaseShareFolderPath
Dim fileStream As FileStream = New FileStream((baseDir + "\" + filePath), FileMode.Open, FileAccess.Read)
Dim bytes As Byte()
Dim binaryReader As BinaryReader = New BinaryReader(fileStream)

bytes = binaryReader.ReadBytes(fileStream.Length)

Dim fileName As String = filePath.Substring(filePath.IndexOf("\Crm") + 1)
context.Response.ContentType = "xls"
context.Response.AppendHeader("content-disposition", "attachment;filename=" & fileName)
context.Response.OutputStream.Write(bytes, 0, bytes.Length)

Can anyone see any reason this would not prompt the user with the file dialog? We use similar code in other projects, mine just seems to be missing some small piece.

Answer Source

Files cannot be downloaded to the client machine using ajax.

In your javascript, you need to use window.location.replace(downloadUrl) or window.open(downloadUrl). The latter will open a new window to download the file, the first will use the current window to perform the download.

For reference: Download a file by jQuery.Ajax

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download