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

Bal*_*yal 5 c# asp.net exe download

我设计了一个网站,当我点击一个按钮时,.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");
Run Code Online (Sandbox Code Playgroud)

Usm*_*lid 5

你能试试吗?

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.");
    }
}
Run Code Online (Sandbox Code Playgroud)

我希望我编辑的评论有助于理解.但请注意:这只是一个粗略的总结.你可以做更多的事情.