向网易下载附件一样  点击一个超链接可以选择打开文件,也可以选择保存文件。请给出代码。谢谢。

解决方案 »

  1.   

    1 Page.Response.Buffer=true;  
     2 Page.Response.Clear();  
     3 //这里的ContentType也可以读存入数据库中的文件类型.
     4 Page.Response.ContentType="Application/unknown";  
     5 //attachment是以附件的形式下载,也可以改为online在线找开.
     6 Response.AddHeader("Content-Disposition","attachment;  filename="  +  
     7                                                              ds.Tables[0].Rows[0]["filename"].ToString()  +  ";");  
     8            
     9 Page.Response.BinaryWrite(file);  
    10 Page.Response.Flush();  
    11 Page.Response.End();
      

  2.   

    public static void DownloadFile(string physicalFilePath)
    {
    FileStream stream=null;
    try 
    {
    stream = new FileStream(physicalFilePath, FileMode.Open, FileAccess.Read, FileShare.Read);    
    int bufSize = (int)stream.Length;
    byte[] buf = new byte[bufSize]; int bytesRead = stream.Read(buf, 0, bufSize);
    HttpContext.Current.Response.ContentType = "application/octet-stream"; 
    HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename="+System.IO.Path.GetFileName(physicalFilePath));
    HttpContext.Current.Response.OutputStream.Write(buf, 0, bytesRead);
    HttpContext.Current.Response.End();
    }
    finally 
    {
    stream.Close();
    }
    }
    private void Button1_Click(object sender, System.EventArgs e)
    {
    DownloadFile(@"c:\1.txt");
    }