public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Response.ContentType = "application/octet-stream";
Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
Response.WriteFile(Server.MapPath("Kav.exe"));
}
}
...{
if (fname == null || fname.trim() == "")
return;
fileinfo file = new fileinfo(fname); response.clear();
response.addheader("content-disposition",
"attachment; filename=" + httputility.urlencode((file.name), system.text.encoding.utf8));
response.addheader("content-length", file.length.tostring());
response.contenttype = "application/octet-stream";
//Response.WriteFile(Server.MapPath("/Kav.exe"));
response.writefile(file.fullname);
response.flush(); response.close();
response.end();
}
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
/// <summary>
/// 下载服务器上的文件
/// </summary>
/// <param name="PageResponse">程序中可以设置参数:HttpResponse ht=Page.Response;</param>
/// <param name="serverPath">服务器上的文件路径</param>
public void DownloadFile(HttpResponse response, string serverPath)
{
FileStream fs = null;
try
{
fs = File.OpenRead(serverPath);
byte[] buffer = new byte[1024];
long count = 1024;
response.Buffer = true;
response.AddHeader("Connection", "Keep-Alive");
response.ContentType = "application/octet-stream";
response.AddHeader("Content-Disposition", "attachment;filename=" + Path.GetFileName(serverPath));//下载时要保存的默认文件名
response.AddHeader("Content-Length", fs.Length.ToString());
while (count == 1024)
{
count = fs.Read(buffer, 0, 1024);
response.BinaryWrite(buffer);
}
}
catch
{
}
finally
{
fs.Close();
}
}
#endregion