无法通过C#.NET下载exe文件 [英] Cannot download exe files through c#.net

查看:132
本文介绍了无法通过C#.NET下载exe文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经设计了一个网站,通过它,当我点击一个按钮,一个.EXE文件应该从具体的路径从我的计算机上下载。

但它不是下载的exe文件,而不是它的下载网站的aspx页面。

我用下面的code:

  WebClient的myWebClient =新的WebClient();
//连接具有Web资源文件名域。
myWebClient.DownloadFile(HTTP://本地主机:1181 /编译/编译器/ sample2.exe,sample2.exe);


解决方案

能否请你试试这个。

 字符串文件名=yourfilename;
如果(文件名!=)
{
    字符串路径=使用Server.Mappath(文件名);
    System.IO.FileInfo文件=新System.IO.FileInfo(路径);
    如果(file.Exists)
    {
         Response.Clear();
         //内容处置会告诉浏览器如何处理该文件。(例如,在JPG文件的情况下,可以在浏览器中显示文件或下载)
         //这里attachement是重要的。这是告诉浏览器,以输出作为附件,这是在下载对话框被显示的名
         Response.AddHeader(内容处置,附件;文件名=+ file.Name);
         //内容讲述长度..
         Response.AddHeader(内容长度,file.Length.ToString());         //文件类型,无论是exe文件,PDF,JPEG等等等等
         Response.ContentType =应用程序/八位字节流;         //在响应写入该文件的内容,以发送回客户..
         Response.WriteFile(file.FullName);
         到Response.End();
    }
    其他
    {
         的Response.Write(此文件不存在。);
    }
}

我希望我的编辑评论将有助于了解。但要注意:这只是一个粗略的总结。你可以做得比这多很多。

I have designed a website through which when i click a button a .EXE file should download from specific path from my computer.

But its not downloading the exe file instead its downloading the aspx page of the website.

I use the following code:

WebClient myWebClient = new WebClient();
// Concatenate the domain with the Web resource filename.
myWebClient.DownloadFile("http://localhost:1181/Compile/compilers/sample2.exe", "sample2.exe");

解决方案

Can you please try this.

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.");
    }
}

I hope my edited comment will help to understand. But note: It is just a rough summary. You can do a lot more than this.

这篇关于无法通过C#.NET下载exe文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆