在服务器端有一个文件,我想实现在客户端单击一下按钮,就直接打开下载这个文件的下载框,不知在按钮中应该写什么语句,请高手赐教??

解决方案 »

  1.   

    private void Button1_Click(object sender, System.EventArgs e)
    {

    Response.Redirect("http://www.www.ww/aa.exe");

    }
      

  2.   

    strFilePath //文件路径
    strFileName //下载显示文件名Response.Clear();
    strFileName = DateTime.Now.ToString("yyyyMMddHHmmss")+Path.GetExtension(strFilePath);
    Response.AppendHeader("Content-Disposition","attachment;filename="+strFileName); 
    Response.WriteFile(strFilePath);
    Response.End();
      

  3.   

    Response.AppendHeader("Content-Disposition","attachment;filename="+strFileName); 
      

  4.   

    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();
      

  5.   

    /// <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();
    }
      

  6.   

    不需要写在按钮里面,写在HTML中就行了
      

  7.   

    <a id="id" hrft=".../download/filename.rar" target="">filename</a>
    以上就OK了,download是文件夹。
      

  8.   

    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();
      

  9.   

    //出现保存或者打开的框
    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);
      

  10.   

    注意上面的文件路径必须用Server.MapPaht(""),也就是相对路径得到