您的位置:首页 > 编程语言 > C#

上传文件

2015-06-02 14:44 393 查看
文件的上传下载是我们在实际项目开发过程中经常需要用到的技术,这里给出几种常见的方法,本文主要内容包括:

  1、如何解决文件上传大小的限制

  2、以文件形式保存到服务器

  3、转换成二进制字节流保存到数据库以及下载方法

  4、上传Internet上的资源

  第一部分:


  首先我们来说一下如何解决ASP.net中的文件上传大小限制的问题,我们知道在默认情况下ASP.NET的文件上传大小限制为2M,一般情况下,可以采用更改web.config和 machine.config文件对网站和网站目录进行配置,web.config文件包含了某一个具体应用所需的一些特殊的配置信息,比如会话状态设置和身份验证设置,machine.config文件包含了整个服务器的配置信息.web.config可以从 machine.config继存或者重写部分配置信息.针对一个具体的网站可以配置两部分信息,一是针对整个服务器的machine.config配置,另外一个是针对望站的
web.config配置.web.config文件一般存在于网站的根目录下,他包含的配置信息对该目录和目录下的子目录起作用

(1)修改web.config文件

在web.config文件中添加<httpRuntime/>配置可以自定义上传文件的大小限制.添加的设置代码如下. 


<configuration> 


<system.web> 


<httpRuntime maxRequestLength="4096" //此大小为默认值,可以根据需要修改 


        executionTimeout="600" //此值指定上传文件的有效时间为10分钟 




/> 




</system.web> 




</configuration> 




(2)修改machine.config文件 


在" %\Microsoft.NET\Framework \v1.0.3705\config"(1.0版本>或"%\Microsoft.NET\ Framework\v1.1.4322\config" (1.1版本>machine.config文件.打开machine.config文件可以看到如下设置代码 


<!-- 


httpRuntime Attributes: 


executionTimeout="[seconds]" -time in seconds before request is automatically timed out 


maxRequestLength="[KBytes]"-KBytes size of maximum request length to accept 


useFullyQualifedREdirectUrl="[true|false]"-fully qualifiy the URL for client redirects 


minFreeThreads="[count]"-minmum number of free thread to allow execution of new requests 


minLocalRequestFreeThreads="[count]" -minmum number of free thread to allow execution of new local requests 


appRequestQueueLimit="[count]" -maxmum number of Requests queued for the application --> 


< httpRuntime executionTimeout="90" maxRequestLength="4096" useFullyQualifiedRedirectUrl="false" minFreeThreads="8" 


minLocalRequestFreeThreas="4" appRequestQueueLimit="100"/> 


上面的代码中executionTimeout属性用于指定上传操作的有效时间(单位秒).maxRequestLength属性用于指定上传文件的最大字节数,单位KB,此属性默认大小为4096K(4MB).通过修改此属性可以设置上传文件的大小。

  这样上传文件的最大值就变成了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
;

 while(wr.ReadEntityBody(bs2,n)>0)

 {

  ..

 }

}
  这样就可以解决了大文件的上传问题了。

  第二部分:

  下面我们来介绍如何以文件形式将客户端的一个文件上传到服务器并返回上传文件的一些基本信息。

  首先我们定义一个类,用来存储上传的文件的信息(返回时需要)。
public class FileUpLoad

{

 public FileUpLoad()

 {}

 /**////

 /// 上传文件名称

 ///

 public string FileName

 {

  get

  {

   return fileName;

  }

  set

  {

   fileName = value;

  }

 }

 private string fileName;

 /**////

 /// 上传文件路径

 ///

 public string FilePath

 {

  get

  {

   return filepath;

  }

  set

  {

   filepath = value;

  }

 }

 private string filepath;

 /**////

 /// 文件扩展名

 ///

 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

</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了。

  第三部分:

  这里我们主要说一下如何以二进制的形式上传文件以及下载。首先说上传,方法如下:
public byte[] UpLoadFile(HtmlInputFile f_IFile)

{

 //获取由客户端指定的上传文件的访问

 HttpPostedFile upFile=f_IFile.PostedFile;

 //得到上传文件的长度

 int upFileLength=upFile.ContentLength;

 //得到上传文件的客户端MIME类型

 string contentType = upFile.ContentType;

 byte[] FileArray=new Byte[upFileLength];

 Stream fileStream=upFile.InputStream;

 fileStream.Read(FileArray,0,upFileLength);

 return FileArray;

}
  这个方法返回的就是上传的文件的二进制字节流,这样我们就可以将它保存到数据库了。下面说一下这种形式的下载,也许你会想到这种方式的下载就是新建一个 aspx页面,然后在它的Page_Load()事件里取出二进制字节流,然后再读出来就可以了,其实这种方法是不可取的,在实际的运用中也许会出现无法打开某站点的错误,我一般采用下面的方法:

  首先,在Web.config中加入:
<add verb="*" path="openfile.aspx" type="RuixinOA.Web.BaseClass.OpenFile, RuixinOA.Web"/>
  这表示我打开openfile.aspx这个页面时,系统就会自动转到执行RuixinOA.Web.BaseClass.OpenFile 这个类里的方法,具体实现如下:
using System;

using System.Data;

using System.Web;

using System.IO;

using Ruixin.WorkFlowDB;

using RXSuite.Base;

using RXSuite.Component;

using RuixinOA.BusinessFacade;

namespace RuixinOA.Web.BaseClass

{

 /**////

 /// NetUFile 的摘要说明。

 ///

 public class OpenFile : IHttpHandler

 {

  public void ProcessRequest(HttpContext context)

  {

   //从数据库中取出要下载的文件信息

   RuixinOA.BusinessFacade.RX_OA_FileManager os = new RX_OA_FileManager();

   EntityData data = os.GetFileDetail(id);

   if(data != null && data.Tables["RX_OA_File"].Rows.Count >0)

   {

    DataRow dr = (DataRow)data.Tables["RX_OA_File"].Rows[0];

    context.Response.Buffer = true;

    context.Response.Clear();

    context.Response.ContentType = dr["CContentType"].ToString();

    context.Response.AddHeader("Content-Disposition","attachment;filename=" + HttpUtility.UrlEncode(dr["CTitle"].ToString()));

    context.Response.BinaryWrite((Byte[])dr["CContent"]);

    context.Response.Flush();

    context.Response.End();

   }

  }

  public bool IsReusable

  {  

   get { return true;}

  }

 }

}
  执行上面的方法后,系统会提示用户选择直接打开还是下载。这一部分我们就说到这里。

  第四部分:

  这一部分主要说如何上传一个Internet上的资源到服务器。

  首先需要引用 System.Net 这个命名空间,然后操作如下:
HttpWebRequest hwq = (HttpWebRequest)WebRequest.Create("http://localhost/pwtest/webform1.aspx");

HttpWebResponse hwr = (HttpWebResponse)hwq.GetResponse();

byte[] bytes = new byte[hwr.ContentLength];

Stream stream = hwr.GetResponseStream();

stream.Read(bytes,0,Convert.ToInt32(hwr.ContentLength));

//HttpContext.Current.Response.BinaryWrite(bytes);

  HttpWebRequest 可以从Internet上读取文件,因此可以很好的解决这个问题。
  第五部分:总结

  今天简单的介绍了几种文件上传与下载的方法,都是在实际的项目开发中经常需要用到的,可能还有不完善的地方,希望大家可以互相交流一下项目开发中的经验。

  这次在项目中,用到了大文件上传,要上传的文件有100多m,于是研究现在国内使用的大文件上传的  

  组件发现用的比较多的有两个控件AspnetUpload   2.0和Lion.Web.UpLoadModule,另外还有思归在它的博客堂中所说的办法   http://blog.joycode.com/saucer/archi cf49
ve/2004/03/16/16225.aspx,两个控件的方法是:利用隐含的HttpWorkerRequest,用它的GetPreloadedEntityBody   和   ReadEntityBody方法从IIS为ASP.NET建立的pipe里分块读取数据。Chris Hynes为我们提供了这样的一个方案(用HttpModule),该方案除了允许你上传大文件外,还能实时显示上传进度。
   

  Lion.Web.UpLoadModule和AspnetUpload两个.NET组件都是利用的这个方案。    

  当上传单文件时,两个软件的方法是一样的,继承HttpModule    

  HttpApplication   application1   =   sender   as   HttpApplication;    

  HttpWorkerRequest   request1   =   (HttpWorkerRequest)   ((IServiceProvider)   HttpContext.Current).GetService(typeof(HttpWorkerRequest));    

  try    

  {    

  if   (application1.Context.Request.ContentType.IndexOf("multipart/form-data")   <=   -1)    

  {    

  return;    

  }    

  //Check   The   HasEntityBody    

  if   (!request1.HasEntityBody())    

  {    

  return;    

  }    

   

  int   num1   =   0;    

  TimeSpan   span1   =   DateTime.Now.Subtract(this.beginTime);    

   

  string   text1   =   application1.Context.Request.ContentType.ToLower();    

   

  byte[]   buffer1   =   Encoding.ASCII.GetBytes(("\r\n--"   +   text1.Substring(text1.IndexOf("boundary=")   +   9)).ToCharArray());    

  int   num2   =   Convert.ToInt32(request1.GetKnownRequestHeader(11));    

  Progress   progress1   =   new   Progress();    

   

  application1.Context.Items.Add("FileList",   new   Hashtable());    

   

  byte[]   buffer2   =   request1.GetPreloadedEntityBody();    

  num1   +=   buffer2.Length;    

   

  string   text2   =   this.AnalysePreloadedEntityBody(buffer2,   "UploadGUID");    

  if   (text2   !=   string.Empty)    

  {    

  application1.Context.Items.Add("LionSky_UpLoadModule_UploadGUID",   text2);    

  }    

  bool   flag1   =   true;    

  if   ((num2   >   this.UpLoadFileLength())   &&   ((0   >   span1.TotalHours)   ||   (span1.TotalHours   >   3)))    

  {    

  flag1   =   false;    

  }    

  if   ((0   >   span1.TotalHours)   ||   (span1.TotalHours   >   3))    

  {    

  flag1   =   false;    

  }    

  string   text3   =   this.AnalysePreloadedEntityBody(buffer2,   "UploadFolder");    

  ArrayList   list1   =   new   ArrayList();    

  RequestStream   stream1   =   new   RequestStream(buffer2,   buffer1,   null,   RequestStream.FileStatus.Close,   RequestStream.ReadStatus.NoRead,   text3,   flag1,   application1.Context,   string.Empty);    

  list1.AddRange(stream1.ReadBody);    

  if   (text2   !=   string.Empty)    

  {    

  progress1.FileLength   =   num2;    

  progress1.ReceivedLength   =   num1;    

  progress1.FileName   =   stream1.OriginalFileName;    

  progress1.FileCount   =   ((Hashtable)   application1.Context.Items["FileList"]).Count;    

  application1.Application["_UploadGUID_"   +   text2]   =   progress1;    

  }    

  if   (!request1.IsEntireEntityBodyIsPreloaded())    

  {    

  byte[]   buffer4;    

  ArrayList   list2;    

  int   num3   =   204800;    

  byte[]   buffer3   =   new   byte[num3];    

  while   ((num2   -   num1)   >=   num3)    

  {    

  if   (!application1.Context.Response.IsClientConnected)    

  {    

  this.ClearApplication(application1);    

  }    

  num3   =   request1.ReadEntityBody(buffer3,   buffer3.Length);    

  num1   +=   num3;    

  list2   =   stream1.ContentBody;    

  if   (list2.Count   >   0)    

  {    

  buffer4   =   new   byte[list2.Count   +   buffer3.Length];    

  list2.CopyTo(buffer4,   0);    

  buffer3.CopyTo(buffer4,   list2.Count);    

  stream1   =   new   RequestStream(buffer4,   buffer1,   stream1.FileStream,   stream1.FStatus,   stream1.RStatus,   text3,   flag1,   application1.Context,   stream1.OriginalFileName);    

  }    

  else    

  {    

  stream1   =   new   RequestStream(buffer3,   buffer1,   stream1.FileStream,   stream1.FStatus,   stream1.RStatus,   text3,   flag1,   application1.Context,   stream1.OriginalFileName);    

  }    

  list1.AddRange(stream1.ReadBody);    

  if   (text2   !=   string.Empty)    

  {    

  progress1.ReceivedLength   =   num1;    

  progress1.FileName   =   stream1.OriginalFileName;    

  progress1.FileCount   =   ((Hashtable)   application1.Context.Items["FileList"]).Count;    

  application1.Application["_UploadGUID_"   +   text2]   =   progress1;    

  }    

  }    

  buffer3   =   new   byte[num2   -   num1];    

  if   (!application1.Context.Response.IsClientConnected   &&   (stream1.FStatus   ==   RequestStream.FileStatus.Open))    

  {    

  this.ClearApplication(application1);    

  }    

  num3   =   request1.ReadEntityBody(buffer3,   buffer3.Length);    

  list2   =   stream1.ContentBody;    

  if   (list2.Count   >   0)    

  {    

  buffer4   =   new   byte[list2.Count   +   buffer3.Length];    

  list2.CopyTo(buffer4,   0);    

  buffer3.CopyTo(buffer4,   list2.Count);    

  stream1   =   new   RequestStream(buffer4,   buffer1,   stream1.FileStream,   stream1.FStatus,   stream1.RStatus,   text3,   flag1,   application1.Context,   stream1.OriginalFileName);    

  }    

  else    

  {    

  stream1   =   new   RequestStream(buffer3,   buffer1,   stream1.FileStream,   stream1.FStatus,   stream1.RStatus,   text3,   flag1,   application1.Context,   stream1.OriginalFileName);    

  }    

  list1.AddRange(stream1.ReadBody);    

  if   (text2   !=   string.Empty)    

  {    

  progress1.ReceivedLength   =   num1   +   buffer3.Length;    

  progress1.FileName   =   stream1.OriginalFileName;    

  progress1.FileCount   =   ((Hashtable)   application1.Context.Items["FileList"]).Count;    

  if   (flag1)    

  {    

  progress1.UploadStatus   =   Progress.UploadStatusEnum.Uploaded;    

  }    

  else    

  {    

  application1.Application.Remove("_UploadGUID_"   +   text2);    

  }    

  }    

  }    

  byte[]   buffer5   =   new   byte[list1.Count];    

  list1.CopyTo(buffer5);    

  this.PopulateRequestData(request1,   buffer5);    

  }    

  catch   (Exception   exception1)    

  {    

  this.ClearApplication(application1);    

  throw   exception1;    

  }    
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  C#