javascript 从服务器下载文件并将其保存在客户端
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4183033/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Downloading a file from server and save it in client
提问by Asanka
I am currently developing an ASP.net application, where I generate a word document in server and I want to save it in client machine who access that feature with out user interactions. How can I download it and save it in client machine, using Javascript?
我目前正在开发一个 ASP.net 应用程序,我在服务器中生成一个 word 文档,我想将它保存在客户端机器中,该机器无需用户交互即可访问该功能。如何使用 Javascript 下载它并将其保存在客户端计算机中?
回答by anishMarokey
you cann't save it in clients machine with out knowledge of client.
您无法在不了解客户端的情况下将其保存在客户端机器中。
You can give a linkof the word document,user need to click on the link and save it in his machine.
您可以给出一个linkword文档,用户需要点击链接并将其保存在他的机器中。
<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
注意:您不能使用 Javascript 或任何脚本语言在客户端 PC 上进行任何操作
回答by Pratik
You can do either of this :
您可以执行以下任一操作:
CASE 1 :
情况1 :
private static string GetWebTest1(string url)
{
System.Net.WebClient Client = new WebClient();
return Client.DownloadString(url);
}
CASE 2 :
案例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();
}
回答by Ramiz Uddin
Use System.Net.WebClient.DownloadFileand you are doing bulk downloads then do use WebClient.DownloadProgressChangedEvent. Sometimes we do bulk download but user get the impression that system stuck or fail somewhere and start hitting refresh. Avoid that!
使用System.Net.WebClient.DownloadFile并且您正在进行批量下载,然后使用WebClient.DownloadProgressChangedEvent。有时我们进行批量下载,但用户会觉得系统卡住或在某处失败并开始点击refresh。避免这种情况!

