asp.net文件下载

asp.net2.0以后,我们有了很多种文件下载的选择。

// 提供下载的文件,不编码的话文件名会乱码   
         private   string  fileName  =  HttpContext.Current.Server.UrlEncode( " 规范.rar " );   
        
private   string  filePath  =  HttpContext.Current.Server.MapPath( " 规范.rar " );   
        
// 使用TransmifFile下载文件   
         protected   void  btnDL1_Click( object  sender, EventArgs e)   
        {   
            FileInfo info 
=   new  FileInfo(filePath);   
            
long  fileSize  =  info.Length;   
            Response.Clear();   
            Response.ContentType 
=   " application/x-zip-compressed " ;   
            Response.AddHeader(
" Content-Disposition " " attachment;filename= " +  fileName);   
            
// 不指明Content-Length用Flush的话不会显示下载进度   
            Response.AddHeader( " Content-Length " , fileSize.ToString());   
            Response.TransmitFile(filePath, 
0 , fileSize);   
            Response.Flush();   
            Response.Close();   
        }   
  
        
// 使用WriteFile下载文件   
         protected   void  btnDL2_Click( object  sender, EventArgs e)   
        {   
            FileInfo info 
=   new  FileInfo(filePath);   
            
long  fileSize  =  info.Length;   
            Response.Clear();   
            Response.ContentType 
=   " application/octet-stream " ;   
            Response.AddHeader(
" Content-Disposition " " attachement;filename= "   +  fileName);   
            
// 指定文件大小   
            Response.AddHeader( " Content-Length " , fileSize.ToString());   
            Response.WriteFile(filePath, 
0 , fileSize);   
            Response.Flush();   
            Response.Close();   
        }   
  
        
// 使用OutputStream.Write分块下载文件   
         protected   void  btnDL3_Click( object  sender, EventArgs e)   
        {   
            
// 指定块大小   
             long  chunkSize  =   102400 ;   
            
// 建立一个100K的缓冲区   
             byte [] buffer  =   new   byte [chunkSize];   
            
// 已读的字节数   
             long  dataToRead  =   0 ;   
            FileStream stream 
=   null ;   
            
try     
            {   
                
// 打开文件   
                stream  =   new  FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read);   
                dataToRead 
=  stream.Length;   
  
                
// 添加Http头   
                Response.ContentType  =   " application/octet-stream " ;   
                Response.AddHeader(
" Content-Disposition " " attachement;filename= "   +  fileName);   
                Response.AddHeader(
" Content-Length " , dataToRead.ToString());   
  
                
while  (dataToRead  >   0 )    
                {   
                    
if  (Response.IsClientConnected)   
                    {   
                        
int  length  =  stream.Read(buffer,  0 , Convert.ToInt32(chunkSize));   
                        Response.OutputStream.Write(buffer, 
0 , length);   
                        Response.Flush();   
                        Response.Clear();   
                        dataToRead 
-=  length;   
                    }   
                    
else     
                    {   
                        
// 防止client失去连接   
                        dataToRead  =   - 1 ;   
                    }   
                }   
            }   
            
catch  (Exception ex)   
            {   
                Response.Write(
" Error: "   +  ex.Message);   
            }   
            
finally     
            {   
                
if  (stream  !=   null )    
                {   
                    stream.Close();   
                }   
                Response.Close();   
            }   
        }   
  
        
// 使用BinaryWrite下载文件,大文件效率不行   
         protected   void  btnDL4_Click( object  sender, EventArgs e)   
        {   
            FileStream stream 
=   null ;   
            
try     
            {   
                
// 读文件,大文件一次读入会占用大量内存   
                stream  =   new  FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read);   
                
byte [] bytes  =   new   byte [stream.Length];   
                stream.Read(bytes, 
0 , bytes.Length);   
                stream.Close();   
  
                
// 添加Http头   
                Response.ContentType  =   " application/octet-stream " ;   
                Response.AddHeader(
" Content-Disposition " " attachement;filename= "   +  fileName);   
                Response.AddHeader(
" Content-Length " , bytes.Length.ToString());   
                Response.BinaryWrite(bytes);   
                Response.Flush();   
            }   
            
catch  (Exception ex)   
            {   
                Response.Write(
" Error: "   +  ex.Message);   
            }   
            
finally     
            {   
                
if  (stream  !=   null )    
                {   
                    stream.Close();   
                }   
                Response.Close();   
            }   
        }   
        
// 使用BinaryWrite分块下载文件   
     protected   void  btnDL5_Click( object  sender, EventArgs e)
    {
        
// 指定区块和缓冲区   
         long  chunkSize  =   102400 ;
        
byte [] buffer  =   new   byte [chunkSize];
        FileStream stream 
=   null ;
        
long  dataToRead  =   0 ;
        
try
        {
            stream 
=   new  FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read);
            dataToRead 
=  stream.Length;
            
// 添加Http头   
            Response.ContentType  =   " application/octet-stream " ;
            Response.AddHeader(
" Content-Disposition " " attachement;filename= "   +  fileName);
            Response.AddHeader(
" Content-Length " , dataToRead.ToString());

            
while  (dataToRead  >   0 )
            {
                
if  (Response.IsClientConnected)
                {
                    
int  length  =  stream.Read(buffer,  0 , Convert.ToInt32(chunkSize));
                    Response.BinaryWrite(buffer);
                    Response.Flush();
                    Response.Clear();

                    dataToRead 
-=  length;
                }
                
else
                {
                    dataToRead 
=   - 1 ;
                }
            }

        }
        
catch  (Exception ex)
        {
            Response.Write(
" Error: "   +  ex.Message);
        }
        
finally
        {
            
if  (stream  !=   null )
            {
                stream.Close();
            }
            Response.Close();
        }
    }   

 

以上除了第四种不推荐以外,其他的都可以,但是个人感觉分块下载的要好一点。没有仔细测试,所以可能有问题。

注意:对于中文文件名要编码才能正确显示。对于中文文件名(UTF8编码后大于153字节的中文)即使编码了,还是有问题的,大家可以参考这个链接。 http://hi.baidu.com/river_5566/blog/item/d66804cef8afb031b700c863.html

你可能感兴趣的:(asp.net)