无法通过c#.net下载exe文件
我设计了一个网站,当我点击一个按钮时,.EXE文件应该从我的计算机的特定路径下载。
但它不下载exe文件而不是下载网站的aspx页面。
我使用以下代码:
WebClient myWebClient = new WebClient(); // Concatenate the domain with the Web resource filename. myWebClient.DownloadFile("http://localhost:1181/Compile/compilers/sample2.exe", "sample2.exe");
你能试试吗?
string filename = "yourfilename"; if (filename != "") { string path = Server.MapPath(filename); System.IO.FileInfo file = new System.IO.FileInfo(path); if (file.Exists) { Response.Clear(); //Content-Disposition will tell the browser how to treat the file.(eg in case of jpg file, Either to display the file in browser or download it) //Here the attachement is important. which is telling the browser to output as an attachment and the name that is to be displayed on the download dialog Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name); //Telling length of the content.. Response.AddHeader("Content-Length", file.Length.ToString()); //Type of the file, whether it is exe, pdf, jpeg etc etc Response.ContentType = "application/octet-stream"; //Writing the content of the file in response to send back to client.. Response.WriteFile(file.FullName); Response.End(); } else { Response.Write("This file does not exist."); } }
我希望我编辑的评论有助于理解。 但请注意:这只是一个粗略的总结。 你可以做更多的事情。
- WebForms UnobtrusiveValidationMode需要一个用于jquery的ScriptResourceMapping
- 如何找出处理请求的程序集
- 从数据库中检索图像并在没有httphandler的情况下显示在ASP.NET上
- Gridview具有固定标题和整页宽度网格
- 如何使用构造函数dependency injectionunit testingasp.net核心应用程序
- xamarin android应用程序:如何获取我的项目的Resources文件夹
- 在asp.net,C#中保持Fileupload控件的值
- 获取查询以使用参数和“喜欢”
- IIS6 ASP.NET 2.0应用程序缓存 – 大量数据的数据存储选项和性能