ASP.NET中文件上传下载方法集合(1)
点击次数:21 次 发布日期:2008-11-26 13:58:33 作者:源代码网
|
源代码网推荐 源代码网推荐 1、如何解决文件上传大小的限制 源代码网推荐 源代码网推荐 2、以文件形式保存到服务器 源代码网推荐 源代码网推荐 3、转换成二进制字节流保存到数据库以及下载方法 源代码网推荐 源代码网推荐 4、上传Internet上的资源 源代码网推荐 源代码网推荐 第一部分: 源代码网推荐 源代码网推荐 首先我们来说一下如何解决ASP.net中的文件上传大小限制的问题,我们知道在默认情况下ASP.NET的文件上传大小限制为2M,一般情况下,我们可以采用更改Web.Config文件来自定义最大文件大小,如下: 源代码网推荐 源代码网推荐 <httpRuntime executionTimeout="300" maxRequestLength="40960" useFullyQualifiedRedirectUrl="false"/>这样上传文件的最大值就变成了4M,但这样并不能让我们无限的扩大MaxRequestLength的值,因为ASP.NET会将全部文件载入内存后,再加以处理。解决的方法是利用隐含的HttpWorkerRequest,用它的GetPreloadedEntityBody和ReadEntityBody方法从IIS为ASP.NET建立的pipe里分块读取数据。实现方法如下: 源代码网推荐 源代码网推荐 IServiceProvidERProvider=(IServiceProvider)HttpContext.Current; 源代码网推荐 HttpWorkerRequestwr=(HttpWorkerRequest)provider.GetService(typeof(HttpWorkerRequest)); 源代码网推荐 byte[]bs=wr.GetPreloadedEntityBody(); 源代码网推荐 . 源代码网推荐 if(!wr.IsEntireEntityBodyIsPreloaded()) 源代码网推荐 { 源代码网推荐 intn=1024; 源代码网推荐 byte[]bs2=newbyte[n]; 源代码网推荐 while(wr.ReadEntityBody(bs2,n)>0) 源代码网推荐 { 源代码网推荐 .. 源代码网推荐 } 源代码网推荐 } 源代码网推荐 源代码网推荐 这样就可以解决了大文件的上传问题了。 源代码网推荐 源代码网推荐 第二部分: 源代码网推荐 源代码网推荐 下面我们来介绍如何以文件形式将客户端的一个文件上传到服务器并返回上传文件的一些基本信息。 源代码网推荐 源代码网推荐 首先我们定义一个类,用来存储上传的文件的信息(返回时需要)。 源代码网推荐 源代码网推荐 public class FileUpLoad 源代码网推荐 { 源代码网推荐 public FileUpLoad() 源代码网推荐 {} 源代码网推荐 /**//// <summary> 源代码网推荐 /// 上传文件名称 源代码网推荐 /// </summary> 源代码网推荐 public string FileName 源代码网推荐 { 源代码网推荐 get 源代码网推荐 { 源代码网推荐 return fileName; 源代码网推荐 } 源代码网推荐 set 源代码网推荐 { 源代码网推荐 fileName = value; 源代码网推荐 } 源代码网推荐 } 源代码网推荐 private string fileName; 源代码网推荐 源代码网推荐 /**//// <summary> 源代码网推荐 /// 上传文件路径 源代码网推荐 /// </summary> 源代码网推荐 public string FilePath 源代码网推荐 { 源代码网推荐 get 源代码网推荐 { 源代码网推荐 return filepath; 源代码网推荐 } 源代码网推荐 set 源代码网推荐 { 源代码网推荐 filepath = value; 源代码网推荐 } 源代码网推荐 } 源代码网推荐 private string filepath; 源代码网推荐 源代码网推荐 /**//// <summary> 源代码网推荐 /// 文件扩展名 源代码网推荐 /// </summary> 源代码网推荐 public string FileExtension 源代码网推荐 { 源代码网推荐 get 源代码网推荐 { 源代码网推荐 return fileExtension; 源代码网推荐 } 源代码网推荐 set 源代码网推荐 { 源代码网推荐 fileExtension = value; 源代码网推荐 } 源代码网推荐 } 源代码网推荐 private string fileExtension; 源代码网推荐 } 源代码网推荐 源代码网推荐 另外我们还可以在配置文件中限制上传文件的格式(App.Config): 源代码网推荐 源代码网推荐 <?XML version="1.0" encoding="gb2312" ?> 源代码网推荐 <Application> 源代码网推荐 <FileUpLoad> 源代码网推荐 <Format>.jpg|.gif|.png|.bmp</Format> 源代码网推荐 </FileUpLoad> 源代码网推荐 </Application> 源代码网推荐 源代码网推荐 这样我们就可以开始写我们的上传文件的方法了,如下: 源代码网推荐 源代码网推荐 public FileUpLoad UpLoadFile(HtmlInputFile InputFile,string filePath,string myfileName,bool isRandom) 源代码网推荐 { 源代码网推荐 FileUpLoad fp = new FileUpLoad(); 源代码网推荐 string fileName,fileExtension; 源代码网推荐 string saveName; 源代码网推荐 源代码网推荐 // 源代码网推荐 //建立上传对象 源代码网推荐 // 源代码网推荐 HttpPostedFile postedFile = InputFile.PostedFile; 源代码网推荐 源代码网推荐 fileName = System.IO.Path.GetFileName(postedFile.FileName); 源代码网推荐 fileExtension = System.IO.Path.GetExtension(fileName); 源代码网推荐 源代码网推荐 // 源代码网推荐 //根据类型确定文件格式 源代码网推荐 // 源代码网推荐 AppConfig app = new AppConfig(); 源代码网推荐 string format = app.GetPath("FileUpLoad/Format"); 源代码网推荐 源代码网推荐 // 源代码网推荐 //如果格式都不符合则返回 源代码网推荐 // 源代码网推荐 if(format.IndexOf(fileExtension)==-1) 源代码网推荐 { 源代码网推荐 throw new ApplicationException("上传数据格式不合法"); 源代码网推荐 } 源代码网推荐 源代码网推荐 // 源代码网推荐 //根据日期和随机数生成随机的文件名 源代码网推荐 // 源代码网推荐 if(myfileName != string.Empty) 源代码网推荐 { 源代码网推荐 fileName = myfileName; 源代码网推荐 } 源代码网推荐 源代码网推荐 if(isRandom) 源代码网推荐 { 源代码网推荐 Random objRand = new Random(); 源代码网推荐 System.DateTime date = DateTime.Now; 源代码网推荐 //生成随机文件名 源代码网推荐 saveName = date.Year.ToString() + date.Month.ToString() + date.Day.ToString() + date.Hour.ToString() + date.Minute.ToString() + date.Second.ToString() + Convert.ToString(objRand.Next(99)*97 + 100); 源代码网推荐 fileName = saveName + fileExtension; 源代码网推荐 } 源代码网推荐 源代码网推荐 string phyPath = HttpContext.Current.Request.MapPath(filePath); 源代码网推荐 源代码网推荐 //判断路径是否存在,若不存在则创建路径 源代码网推荐 DirectoryInfo upDir = new DirectoryInfo(phyPath); 源代码网推荐 if(!upDir.Exists) 源代码网推荐 { 源代码网推荐 upDir.Create(); 源代码网推荐 } 源代码网推荐 源代码网推荐 // 源代码网推荐 //保存文件 源代码网推荐 // 源代码网推荐 try 源代码网推荐 { 源代码网推荐 postedFile.SaveAs(phyPath + fileName); 源代码网推荐 源代码网推荐 fp.FilePath = filePath + fileName; 源代码网推荐 fp.FileExtension = fileExtension; 源代码网推荐 fp.FileName = fileName; 源代码网推荐 } 源代码网推荐 catch 源代码网推荐 { 源代码网推荐 throw new ApplicationException("上传失败!"); 源代码网推荐 } 源代码网推荐 源代码网推荐 //返回上传文件的信息 源代码网推荐 return fp; 源代码网推荐 } 源代码网推荐 源代码网推荐 然后我们在上传文件的时候就可以调用这个方法了,将返回的文件信息保存到数据库中,至于下载,就直接打开那个路径就OK了。 源代码网推荐 源代码网推荐 源代码网推荐 做人要厚道,请注明转自酷网动力(www.ASPCOOL.COM)。 源代码网推荐 源代码网供稿. |
