c# - 无法通过c#.net下载exe文件

标签 c# asp.net download 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.(e.g. 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.");
    }
}

我希望我编辑的评论有助于理解。但请注意:这只是一个粗略的总结。您可以做的远不止这些。

关于c# - 无法通过c#.net下载exe文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21823065/

相关文章:

php - 使用 PHP POST 从 MYSQL 下载 CSV 文件

javascript - 如何知道文件是否下载完成?

c# - WebService调用异常: Thread was being aborted

c# - Web API 2 Windows 身份验证不断提示登录

c# - 在下拉菜单上应用样式后,OnSelectedIndexChanged 不起作用

c# - 如何在 Lucene.NET 中进行部分词搜索?

Asp.Net 网页网站 - 如何添加 Web Api Controller

java - 从 Servlet 下载 Excel

C# - 获取两个日期之间的天数

c# - 在父子关系中具有多个对象的 Linq 查询