下載文件

本文介绍了一个用于处理不同文件类型的下载过程的方法。该方法根据文件扩展名设置正确的MIME类型,并为用户提供下载选项。支持的文件类型包括文档、电子表格、图片等。
ExpandedBlockStart.gifContractedBlock.gif/**////   <summary> 
InBlock.gif
///   下载文件 
InBlock.gif
///   </summary> 
ExpandedBlockEnd.gif
///   <param   name= "filepath "> </param> 

None.gifpublic   static   void   DownloadFile(string   filepath) 
ExpandedBlockStart.gifContractedBlock.gif
dot.gif
InBlock.gif
string strContentType =   " "
InBlock.gif
string strFileName =   " "
InBlock.gif
string strFilePath =   " "
InBlock.gif
int intIndex1 =   -1
InBlock.gif
int intIndex2 =   -1
InBlock.gif
int intIndex =   0
InBlock.gif
InBlock.gif
if   (filepath.EndsWith( ".doc ")) 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gifstrContentType   
=   "application/msword "
ExpandedSubBlockEnd.gif}
 
InBlock.gif
else   if   (filepath.EndsWith( ".xls ")) 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
//strContentType   =   "application/x-zaurus-xls " 
InBlock.gif
strContentType   =   "application/vnd.ms-excel "
ExpandedSubBlockEnd.gif}
 
InBlock.gif
else   if   (filepath.EndsWith( ".pdf ")) 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gifstrContentType   
=   "application/pdf "
ExpandedSubBlockEnd.gif}
 
InBlock.gif
else   if   (filepath.EndsWith( ".ppt ")) 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
//strContentType   =   "application/powerpoint " 
InBlock.gif
strContentType   =   "application/vnd.ms-powerpoint "
ExpandedSubBlockEnd.gif}
 
InBlock.gif
else   if   (filepath.EndsWith( ".jpg ")   ¦ ¦   filepath.EndsWith( ".jpeg ")   ¦ ¦   filepath.EndsWith( ".jpe ")) 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gifstrContentType   
=   "image/jpeg "
ExpandedSubBlockEnd.gif}
 
InBlock.gif
else   if   (filepath.EndsWith( ".bmp ")) 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gifstrContentType   
=   "image/bmp "
ExpandedSubBlockEnd.gif}
 
InBlock.gif
else   if   (filepath.EndsWith( ".gif ")) 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gifstrContentType   
=   "image/gif "
ExpandedSubBlockEnd.gif}
 
InBlock.gif
else   if   (filepath.EndsWith( ".zip ")) 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gifstrContentType   
=   "application/zip "
ExpandedSubBlockEnd.gif}
 
InBlock.gif
else   if   (filepath.EndsWith( ".txt ")) 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gifstrContentType   
=   "text/plain "
ExpandedSubBlockEnd.gif}
 
InBlock.gif
else 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gifstrContentType   
=   "application/octet-stream "
ExpandedSubBlockEnd.gif}
 
InBlock.gif
InBlock.gifintIndex1   
=   filepath.LastIndexOf( ""); 
InBlock.gifintIndex2   
=   filepath.LastIndexOf( "\\ "); 
InBlock.gif
InBlock.gif
if   (intIndex1   >   intIndex2) 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gifintIndex   
=   intIndex1; 
ExpandedSubBlockEnd.gif}
 
InBlock.gif
else 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gifintIndex   
=   intIndex2; 
ExpandedSubBlockEnd.gif}
 
InBlock.gif
InBlock.gif
if   (intIndex   >   -1
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
if   (intIndex   +   1   <   filepath.Length) 
InBlock.gifstrFileName   
=   filepath.Substring(intIndex   +   1); 
ExpandedSubBlockEnd.gif}
 
InBlock.gif
else 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gifstrFileName   
=   filepath; 
ExpandedSubBlockEnd.gif}
 
InBlock.gif
InBlock.gif
//   ファイルパス処理   
InBlock.gif
if   (filepath.StartsWith( "")   ¦ ¦   filepath.StartsWith( "\\ ")) 
InBlock.gifstrFilePath   
=   HttpContext.Current.Server.MapPath( "")   +   filepath; 
InBlock.gif
else 
InBlock.gifstrFilePath   
=   filepath; 
InBlock.gif
InBlock.gif
if   (FileUtil.isFileExist(strFilePath)) 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gifHttpContext.Current.Response.Clear(); 
InBlock.gifHttpContext.Current.Response.ContentType   
=   strContentType; 
InBlock.gifHttpContext.Current.Response.AddHeader( 
"Content-Disposition ",   "attachment;   filename= "   +   HttpUtility.UrlEncode(strFileName)); 
InBlock.gif
InBlock.gifHttpContext.Current.Response.WriteFile(strFilePath); 
InBlock.gif
//HttpContext.Current.Response.BinaryWrite(objFileIO.IFileByte(strFilePath)); 
InBlock.gif
InBlock.gif
//HttpContext.Current.Response.End(); 
InBlock.gif
HttpContext.Current.Response.Flush(); 
InBlock.gifHttpContext.Current.Response.Close(); 
ExpandedSubBlockEnd.gif}
 
InBlock.gif
else 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gifSetGlobalMessageID(FindKeyValueCutNull(GXML.COMMONMESSAGE,   GXML.DOWNLOAD_FILE_NOEXIST)); 
ExpandedSubBlockEnd.gif}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值