I have an ASP.NET web application. At some step I create a text file and store it in a folder on the server. I do this on the server code. But then just after getting done with creating the file I want to download that file to client computer. Is this even possible? I know about John Culviner's jquery filedownload plugin. But after reviewing it I understood that the file to download must be in the same directory where the web site itself resides - like www.mywebsite.com/downloads/text1.txt. But I would like to put the created files into some other directory. I feel that ftp will come into play here but don't exactly know how. Could you please guide me a little.
2 Answers
What you will want is to use the Response.TransmitFile method in your backend code rather than javascript.
Dim FileLocation As String = "C:/File.csv" Response.ContentType = "application/csv" Response.AppendHeader("Content-Disposition", "attachment;filename=" + FileLocation ) Response.TransmitFile(FileLocation ) Response.End() So once you've created your file simply use that code, and you will automatically (it will ask them if they want to obviously) start a file download of that file on the clients computer.
Comments
You can't start an FTP session from javascript, but you can create a page that downloads this file and issues the proper headers to let the browser know the file should be downloaded instead of displayed. Then in your javascript just redirect the page to this download url and the file will be downloaded, but instead of changing the page view it will just trigger the download dialog in the browser.
The header should at the very least include the content-disposition of attachment:
Response.AddHeader("content-disposition:", "attachment;filename=...")