程序如下public bool DownloadFile( System.Web.UI.Page refPage, string _FilePath, string _FileName ){ try { string MyContentType = ""; switch ( _FileName.Substring( _FileName.LastIndexOf( "." ), 4 ) ) { case ".xls": MyContentType = "application/vnd.ms-excel"; break; case ".doc": MyContentType = "application/msword"; break;
default: MyContentType = "application/zip"; break; } System.IO.FileStream fs = System.IO.File.OpenRead( _FilePath ); byte[] FileData = new byte[ fs.Length ]; fs.Read( FileData, 0, ( int ) fs.Length ); refPage.Response.Clear(); refPage.Response.AddHeader( "Content-Type", MyContentType ); string FileName = System.Web.HttpUtility.UrlEncode(System.Text.Encoding.UTF8.GetBytes( _FileName ) ); refPage.Response.AddHeader("Content_Disposition", "inline;filename="+System.Convert.ToChar(34) + FileName + System.Convert.ToChar(34) ); refPage.Response.AddHeader("Content-Length", fs.Length.ToString() ); refPage.Response.BinaryWrite( FileData ); fs.Close(); System.IO.File.Delete( _FilePath ); refPage.Response.End(); return true; } catch( Exception e ) { System.Web.HttpContext.Current.Response.Write( e.ToString() ); return false; }} 调用方法:C盘有1.txt文件DownloadFile( this, "C:\\1.txt", "测试.txt" );
現在的問題是可以下載,但是下載時儲存文件名是亂碼,這個亂碼如何解決
我編碼,解碼都試過,webconfig上配置為(繁體系統)
<globalization requestEncoding="big5" responseEncoding="big5" culture="zh-tw" fileEncoding="big5"/>不知有什麼方法,請高手相告
分不夠再加
default: MyContentType = "application/zip"; break; } System.IO.FileStream fs = System.IO.File.OpenRead( _FilePath ); byte[] FileData = new byte[ fs.Length ]; fs.Read( FileData, 0, ( int ) fs.Length ); refPage.Response.Clear(); refPage.Response.AddHeader( "Content-Type", MyContentType ); string FileName = System.Web.HttpUtility.UrlEncode(System.Text.Encoding.UTF8.GetBytes( _FileName ) ); refPage.Response.AddHeader("Content_Disposition", "inline;filename="+System.Convert.ToChar(34) + FileName + System.Convert.ToChar(34) ); refPage.Response.AddHeader("Content-Length", fs.Length.ToString() ); refPage.Response.BinaryWrite( FileData ); fs.Close(); System.IO.File.Delete( _FilePath ); refPage.Response.End(); return true; } catch( Exception e ) { System.Web.HttpContext.Current.Response.Write( e.ToString() ); return false; }} 调用方法:C盘有1.txt文件DownloadFile( this, "C:\\1.txt", "测试.txt" );
現在的問題是可以下載,但是下載時儲存文件名是亂碼,這個亂碼如何解決
我編碼,解碼都試過,webconfig上配置為(繁體系統)
<globalization requestEncoding="big5" responseEncoding="big5" culture="zh-tw" fileEncoding="big5"/>不知有什麼方法,請高手相告
分不夠再加
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货