Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best way to download file from server

I have interesting task which requires me to download a dynamically generated file from a server (ASP.NET) to the client. On the client side it is just JavaScript (jQuery) and the client is able to generate a lot of parameters to customize how the data is exported.

What is the best way to do download the file from the server? Should I use a WCF service such as what is described here or simple page like this one?

I don't know how to download a file without reloading the page (I'm not sure that $.ajax will work in this case). Could someone please give me some direction on this topic? Thanks.

like image 300
Arbejdsglæde Avatar asked Jun 06 '12 10:06

Arbejdsglæde


People also ask

How do I get files from server to local machine?

To copy the files you will need to first invoke the SCP, followed by the remote username@IP address, path to file. If you do not specify the path, it is assumed as default in this case which will be the user's home directory, this will be followed the path where the file will be stored locally.

How do I download a file from Linux server?

wget and curl are just two of the most popular commands for downloading files in Linux. There are more such command line tools. Terminal based web-browsers like elinks, w3m etc can also be used for downloading files in command line. Personally, for a simple download, I prefer using wget over curl.


1 Answers

First you can create the file from a handler .ashx

Let say that you have the file for downloading at download.ashx and you have some parametres to pass from your javascript, eg download.ashx?p1=8827&p2=8831 to know what you going to create.

Then on your javascript you simple can make a redirect as

window.location = "download.ashx?p1=8827&p2=8831";

or alternative you can use the window.open for do the same think

window.open("download.ashx?p1=8827&p2=8831");

and your file will start the download.

Just make sure that you have set the header of attachment, and the correct contenttype on your handle eg:

  HttpContext.Current.Response.ContentType = "application/octet-stream";
  HttpContext.Current.Response.AddHeader("Content-Disposition", 
                    "attachment; filename=" + SaveAsThisFileName);

Simple and clear, both tested and working.

Also you may interesting on this answer: How to handle errors.

like image 60
Aristos Avatar answered Oct 17 '22 00:10

Aristos