在任何Web浏览器中打开下载对话框?

时间:2011-08-06 06:56:29

标签: asp.net

如何打开下载对话框在任何Web浏览器中下载后我可以选择文件保存AS路径?

2 个答案:

答案 0 :(得分:2)

使用此技术。

string filePath = Server.MapPath("~/files/myFileName.csv");
    System.IO.FileInfo fileInfo = new System.IO.FileInfo(filePath);
    Response.ContentType = "application/octet-stream";
    Response.AddHeader("Content-Disposition", string.Format("attachment;filename=\\\"{0}\\\"", filePath));
    Response.AddHeader("Content-Length", fileInfo.Length.ToString());
    Response.WriteFile(filePath);
    Response.End();

答案 1 :(得分:2)

我认为您的解决方案是:

Response.ContentType = "application/ms-excel";
Response.AddHeader("content-disposition", "attachment; filename=CSVhhID" + Uid + ".csv");
string newpath2 = System.Web.HttpContext.Current.Server.MapPath("~//downloadfile//CSVID" + Uid + ".csv");
FileStream sourceFile = new FileStream(newpath2, FileMode.Open);
long FileSize;
FileSize = sourceFile.Length;
byte[] getContent = new byte[(int)FileSize];
sourceFile.Read(getContent, 0, (int)sourceFile.Length);
sourceFile.Close();