C#弹出另存为的对话框

找到两种方法.
第一种是最一般的.
         // Identify the file to download including its path.       

     string filepath = Server.MapPath("softfile/this.rar");
        // Identify the file name.         

    string filename = System.IO.Path.GetFileName(filepath);
         Response.Clear();
        // Specify the Type of the downloadable file.        

   Response.ContentType = "application/octet-stream";
        // Set the Default file name in the FileDownload dialog box.    

     Response.AddHeader("Content-Disposition", "attachment; filename=" + filename);
         Response.Flush();
        // Download the file.        
   Response.WriteFile(filepath);
第二种是,用了asp.net2.0版中,新提供的一个方法TransmitFile().
将指定的文件直接写入 HTTP 响应输出流,而不在内存中缓冲该文件。

 这么做的好处就是解决了writefile()的,输出时会占用服务器大量内存.效率低下,不能下载大文件的问题.
下面是一个小例子.        

 string filepath = Server.MapPath("softfile/this.rar");        

 string filename = System.IO.Path.GetFileName(filepath);                      

 Response.Clear();        

 Response.ContentType = "application/octet-stream";    

//这里的filename可以输出时自定义,不一定用原来的.             

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

 Response.Flush();        

 Response.Close();

posted @ 2010-10-22 12:51  lcisok  阅读(2021)  评论(0编辑  收藏  举报