请教一个文件下载的问题!!! 在服务器端有一个文件,我想实现在客户端单击一下按钮,就直接打开下载这个文件的下载框,不知在按钮中应该写什么语句,请高手赐教?? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 private void Button1_Click(object sender, System.EventArgs e) { Response.Redirect("http://www.www.ww/aa.exe"); } strFilePath //文件路径strFileName //下载显示文件名Response.Clear();strFileName = DateTime.Now.ToString("yyyyMMddHHmmss")+Path.GetExtension(strFilePath);Response.AppendHeader("Content-Disposition","attachment;filename="+strFileName); Response.WriteFile(strFilePath); Response.End(); Response.AppendHeader("Content-Disposition","attachment;filename="+strFileName); Response.Clear(); strFileName =HttpUtility.UrlEncode(reader["Name"].ToString(),System.Text.Encoding.UTF8); strFilePath=System.Web.HttpContext.Current.Request.MapPath("UpFiles/")+Request.QueryString["ID"].ToString()+"."+reader["Type"].ToString(); Response.AppendHeader("Content-Disposition","attachment;filename="+strFileName); Response.WriteFile(strFilePath); Response.End(); /// <summary>/// 文件下载/// </summary>/// <param name="FullFileName"></param>private void FileDownload(string FullFileName){ FileInfo DownloadFile = new FileInfo(FullFileName); Response.Clear(); Response.ClearHeaders(); Response.Buffer=false; Response.ContentType="application/octet-stream"; Response.AppendHeader("Content-Disposition","attachment;filename=" HttpUtility.UrlEncode(DownloadFile.FullName,System.Text.Encoding.UTF8)); Response.AppendHeader("Content-Length",DownloadFile.Length.ToString()); Response.WriteFile(DownloadFile.FullName); Response.Flush(); Response.End();} 不需要写在按钮里面,写在HTML中就行了 <a id="id" hrft=".../download/filename.rar" target="">filename</a>以上就OK了,download是文件夹。 Response.Clear();Response.AddHeader("Content-Dispositon","attechment:filename="+file.Name);Response.AddHeader("Content-Length",file.Length.ToString());Response.ContentType ="applicaiton/octet-stream";Response.WriteFile(file.FullName);Response.End(); //出现保存或者打开的框if(!System.IO.File.Exists(strFile)) { Response.Write("<script language='javascript'>alert('对不起,文件不存在!');</script>"); return; } Response.Clear(); Response.ClearHeaders(); Response.Charset = "GB2312"; Response.ContentEncoding =System.Text.Encoding.UTF8; Response.ContentType = "application/octet-stream"; FileInfo fi=new FileInfo(strFile); Response.AddHeader("Content-Disposition","attachment; filename=" + HttpUtility.UrlEncode(fi.Name)) ; Response.AddHeader("Content-Length",fi.Length.ToString()); byte[] tmpbyte=new byte[1024*8]; FileStream fs=fi.OpenRead(); int count; while((count=fs.Read(tmpbyte,0,tmpbyte.Length))>0) { Response.BinaryWrite(tmpbyte); Response.Flush(); } fs.Close(); Response.End();//将文件直接显示在浏览器中 string strFile="F:\\a.doc"; Response.Clear(); Response.ClearHeaders(); Response.Charset = "GB2312"; Response.ContentEncoding =System.Text.Encoding.UTF8; Response.ContentType = "application/msword"; Response.WriteFile(strFile); 注意上面的文件路径必须用Server.MapPaht(""),也就是相对路径得到 急求啊。。。 求段代码,文件上传问题,高手帮忙! 用label做链接下面语句错在那 如何才能不经常访问数据库? 高分求一SQL语句.... HTML代码存入数据库的问题 求助!!(在线等) 看书 和 实际 中遇到的几个问题,请各位大哥大姐,帮帮俺。。。。帮的有分。。。 一个逻辑问题,关于可添加行的表格,传值到数据库的问题。求帮忙!! 如何在提交以后取到动态生成的checkbox的选中值? 小项目开发碰到个DropDownList的绑定问题,哪位好心的帮俺看看. 一个棘手的问题,请各位老大赐教一下,不胜感激
{
Response.Redirect("http://www.www.ww/aa.exe");
}
strFileName //下载显示文件名Response.Clear();
strFileName = DateTime.Now.ToString("yyyyMMddHHmmss")+Path.GetExtension(strFilePath);
Response.AppendHeader("Content-Disposition","attachment;filename="+strFileName);
Response.WriteFile(strFilePath);
Response.End();
strFileName =HttpUtility.UrlEncode(reader["Name"].ToString(),System.Text.Encoding.UTF8);
strFilePath=System.Web.HttpContext.Current.Request.MapPath("UpFiles/")+Request.QueryString["ID"].ToString()+"."+reader["Type"].ToString();
Response.AppendHeader("Content-Disposition","attachment;filename="+strFileName);
Response.WriteFile(strFilePath);
Response.End();
/// 文件下载
/// </summary>
/// <param name="FullFileName"></param>
private void FileDownload(string FullFileName)
{
FileInfo DownloadFile = new FileInfo(FullFileName);
Response.Clear();
Response.ClearHeaders();
Response.Buffer=false;
Response.ContentType="application/octet-stream";
Response.AppendHeader("Content-Disposition","attachment;filename=" HttpUtility.UrlEncode(DownloadFile.FullName,System.Text.Encoding.UTF8));
Response.AppendHeader("Content-Length",DownloadFile.Length.ToString());
Response.WriteFile(DownloadFile.FullName);
Response.Flush();
Response.End();
}
以上就OK了,download是文件夹。
Response.AddHeader("Content-Dispositon","attechment:filename="+file.Name);
Response.AddHeader("Content-Length",file.Length.ToString());
Response.ContentType ="applicaiton/octet-stream";
Response.WriteFile(file.FullName);
Response.End();
if(!System.IO.File.Exists(strFile))
{
Response.Write("<script language='javascript'>alert('对不起,文件不存在!');</script>");
return;
}
Response.Clear();
Response.ClearHeaders();
Response.Charset = "GB2312";
Response.ContentEncoding =System.Text.Encoding.UTF8;
Response.ContentType = "application/octet-stream";
FileInfo fi=new FileInfo(strFile);
Response.AddHeader("Content-Disposition","attachment; filename=" + HttpUtility.UrlEncode(fi.Name)) ;
Response.AddHeader("Content-Length",fi.Length.ToString());
byte[] tmpbyte=new byte[1024*8];
FileStream fs=fi.OpenRead();
int count;
while((count=fs.Read(tmpbyte,0,tmpbyte.Length))>0)
{
Response.BinaryWrite(tmpbyte);
Response.Flush();
}
fs.Close();
Response.End();//将文件直接显示在浏览器中
string strFile="F:\\a.doc";
Response.Clear();
Response.ClearHeaders();
Response.Charset = "GB2312";
Response.ContentEncoding =System.Text.Encoding.UTF8;
Response.ContentType = "application/msword";
Response.WriteFile(strFile);