如何禁用文件下载弹出窗口并将文件直接保存到C盘

时间:2015-06-25 17:54:44

标签: c#

我的程序在浏览器上生成doc文件,我想将此文件动态保存到我的C盘

string filename = "fileName"; 
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment;filename= " + filename +".doc" );

Response.Charset = "";
Response.ContentType = "application/vnd.ms-word ";       
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
GridView1.AllowPaging = false;
GridView1.DataBind();
GridView1.RenderControl(hw);
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();

2 个答案:

答案 0 :(得分:2)

您无法决定用户保存文件的位置。用户决定。

答案 1 :(得分:1)

You can save a given location on the server, but you can't tell a client where to save a file in the HTTP response. Beyond the security problems inherent in that approach, you have to take into account that there are a diverse array of clients with different file structures. Even Windows clients do not necessarily have a C: drive. A few ActiveX controls could allow you to dictate a save location, but those are client-side code and recent version of Internet Explorer intentionally make that difficult to implement. Also, if the user is using some sort of download client, you can pre-populate a location, but if you don't know their file structure you're still going to have trouble.