FileCommon.cs

生成文件存储的虚拟路径:
None.gif private   static   string  GenerateVirtualFilePath()
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            Byte[] b 
=  Guid.NewGuid().ToByteArray(); 
InBlock.gif            StringBuilder virtualFilePath 
= new StringBuilder();
InBlock.gif            
for(Int32 i = 0; i < b.Length; i+=2)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                Int32 folderName 
= b[i]*256 + b[i+1];
InBlock.gif                virtualFilePath.AppendFormat(
"{0}\\", folderName.ToString());
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return virtualFilePath.ToString();    
ExpandedBlockEnd.gif        }

存储Client端上传的文件
None.gif public   static   string  Save( string  saveRoot, HttpPostedFile entity)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            
string fileName = Path.GetFileName(entity.FileName);
InBlock.gif            
string savePath = Path.Combine(saveRoot, GenerateVirtualFilePath());
InBlock.gif            
if(!Directory.Exists(savePath))
InBlock.gif                Directory.CreateDirectory(savePath);
InBlock.gif            savePath 
= Path.Combine(savePath, fileName);
InBlock.gif            entity.SaveAs(savePath);
InBlock.gif            
return savePath;
ExpandedBlockEnd.gif        }

简单的替换字符串的操作,解决频繁的替换成Web访问路径
None.gif public   static   string  MapWebPath( string  fileName,  string  saveRoot,  string  webRoot)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            
return fileName.Replace(saveRoot, webRoot);
ExpandedBlockEnd.gif        }

将Post到客户端的函数
None.gif public   static   void  DownLoad(System.Web.UI.Page page,  string  filePath)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif
InBlock.gif            
string fileName = Path.GetFileName(filePath);
InBlock.gif            fileName 
= HttpUtility.UrlPathEncode(fileName);
InBlock.gif            page.Response.AppendHeader(
"content-disposition""attachment; filename=" + fileName);
InBlock.gif            page.Response.ContentType 
= "application/octet-stream";
InBlock.gif            page.Response.WriteFile(filePath);
InBlock.gif            page.Response.End();
ExpandedBlockEnd.gif        }

验证文件类型
None.gif public   static   bool  ValidateFileType( string  fileType,  string  fileExtention)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            
string[] fileTypeCollection = fileType.Split(',');
InBlock.gif            
foreach(string type in fileTypeCollection)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if(fileExtention.ToLower() == type.ToLower())
InBlock.gif                    
return true;
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
return false;
ExpandedBlockEnd.gif        }

转载于:https://www.cnblogs.com/anthozoan/archive/2005/11/02/267344.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值