B/S(.net)文件下载方式

1. C#:
/// <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();
}


2. vb.net

Public   Sub WriteDLWindow(ByVal strFileName As StringByVal page As System.Web.UI.Page)
        
Try
           
If File.Exists(page.MapPath(strFileName)) Then
               
Dim TargetFile As FileInfo = New FileInfo(page.MapPath(strFileName))
               
'清除缓冲区流中的所有内容输出.
               page.Response.Clear()
               
'向输出流添加HTTP头 [指定下载/保存 对话框的文件名]
               page.Response.AppendHeader("Content-Disposition""attachment; filename=" + page.Server.UrlEncode(TargetFile.Name))

'繁体格式
                'page.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(strFileName, System.Text.Encoding.UTF8))

                
'向输出流添加HTTP头 [指定文件的长度,这样下载文件就会显示正确的进度
                page.Response.AppendHeader("Content-Length", TargetFile.Length.ToString())
                
'表明输出的HTTP为流[stream],因此客户端只能下载.
                page.Response.ContentType = "application/octet-stream"
                
'发送文件流到客户端.
                page.Response.WriteFile(TargetFile.FullName)
                
'停止执行当前页
                page.Response.End()
            
End If
        
Catch ex As Exception
            
Throw ex
        
End Try
    
End Sub




 

你可能感兴趣的:(B/S(.net)文件下载方式)