1

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 generateReport 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)
        fileStream.Close()
        fileStream.Dispose()
        binaryReader.Close()

        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)
        context.Response.OutputStream.Flush()
        context.Response.OutputStream.Close()
        context.Response.End()

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.

Michael Miner
  • 964
  • 2
  • 17
  • 39

2 Answers2

0

Your ContentType (MIME Type) looks incorrect. Try this:

context.Response.ContentType = "application/vnd.ms-excel"
Jeff McCloud
  • 5,777
  • 1
  • 19
  • 21
  • unfortunately this still does nothing. My first handler forces the file to disk, which is fine, but why with the second handler can I not grab that same file and give it to the user? – Michael Miner Oct 19 '16 at 21:27
0

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

Community
  • 1
  • 1
Will P.
  • 8,437
  • 3
  • 36
  • 45
  • So I will have to make an invisible page with some url which will download the file? Have the ajax call a window.open(url) then for that url have in the Page_Load method have my handlers logic? – Michael Miner Oct 19 '16 at 21:31
  • You can point it to the exact same url you are using to make the ajax call, as long as the response to that call is the file with correct response headers. You don't need to use a new page. – Will P. Oct 19 '16 at 21:32
  • My only issue here is I am not point to a url, rather a path on disk. So I have it returning the proper path, it just opens a blank window. – Michael Miner Oct 19 '16 at 21:42
  • Your javascript cannot access your web server's disk paths directly. The ajax call must be using a url, can you edit that portion of the javascript code into your question? – Will P. Oct 19 '16 at 21:44
  • Unfortunately I do not have a url to this particular folder, but creating an empty page, and having the download in the Page_Load method works! – Michael Miner Oct 20 '16 at 13:37