Downloading a file from server and save it in client
I am currently developing an ASP.net application, where I generate a word document in server and I want to save it in clien开发者_如何学运维t machine who access that feature with out user interactions. How can I download it and save it in client machine, using Javascript?
you cann't save it in clients machine with out knowledge of client.
You can give a link
of the word document,user need to click on the link and save it in his machine.
<a href="serverLink.doc" >Click to Save Word document</a>
Note: you cant do any manipulation on client PC by using Javascript or any scripting language
You can do either of this :
CASE 1 :
private static string GetWebTest1(string url)
{
System.Net.WebClient Client = new WebClient();
return Client.DownloadString(url);
}
CASE 2 :
private static string GetWebTest2(string url)
{
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
WebResponse response = request.GetResponse();
Stream stream = response.GetResponseStream();
StreamReader reader = new StreamReader(stream);
return reader.ReadToEnd();
}
Use System.Net.WebClient.DownloadFile
and you are doing bulk downloads then do use WebClient.DownloadProgressChanged
Event. Sometimes we do bulk download but user get the impression that system stuck or fail somewhere and start hitting refresh
. Avoid that!
精彩评论