ASP.NET中常用的文件上传下载方法
文件的上传下载是我们在实际项目开发过程中经常需要用到的技术,这里给出几种常见的方法,本文主要内容包括:
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();
![](http://pw.cnblogs.com/Images/OutliningIndicators/None.gif)
.
if
(
!
wr.IsEntireEntityBodyIsPreloaded())
![](http://pw.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
intn=1024;
byte[]bs2=newbyte[n];
while(wr.ReadEntityBody(bs2,n)>0)
![](http://pw.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://pw.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
![](http://pw.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
..
}
}
这样就可以解决了大文件的上传问题了。
第二部分:
下面我们来介绍如何以文件形式将客户端的一个文件上传到服务器并返回上传文件的一些基本信息
首先我们定义一个类,用来存储上传的文件的信息(返回时需要)。
public
class
FileUpLoad
![](http://pw.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
public FileUpLoad()
![](http://pw.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://pw.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](http://pw.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 上传文件名称
/// </summary>
public string FileName
![](http://pw.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![](http://pw.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return fileName;
}
set
![](http://pw.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
fileName = value;
}
}
private string fileName;
![](http://pw.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://pw.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 上传文件路径
/// </summary>
public string FilePath
![](http://pw.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![](http://pw.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return filepath;
}
set
![](http://pw.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
filepath = value;
}
}
private string filepath;
![](http://pw.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://pw.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 文件扩展名
/// </summary>
public string FileExtension
![](http://pw.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![](http://pw.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return fileExtension;
}
set
![](http://pw.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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)
![](http://pw.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
FileUpLoad fp = new FileUpLoad();
![](http://pw.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
string fileName,fileExtension;
string saveName;
//
//建立上传对象
//
HttpPostedFile postedFile = InputFile.PostedFile;
![](http://pw.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
fileName = System.IO.Path.GetFileName(postedFile.FileName);
fileExtension = System.IO.Path.GetExtension(fileName);
//
//根据类型确定文件格式
//
AppConfig app = new AppConfig();
string format = app.GetPath("FileUpLoad/Format");
![](http://pw.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://pw.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//
//如果格式都不符合则返回
//
if(format.IndexOf(fileExtension)==-1)
![](http://pw.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
throw new ApplicationException("上传数据格式不合法");
}
//
//根据日期和随机数生成随机的文件名
//
if(myfileName != string.Empty)
![](http://pw.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
fileName = myfileName;
}
![](http://pw.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if(isRandom)
![](http://pw.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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()
![](http://pw.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
+ date.Second.ToString() + Convert.ToString(objRand.Next(99)*97 + 100);
fileName = saveName + fileExtension;
}
string phyPath = HttpContext.Current.Request.MapPath(filePath);
![](http://pw.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://pw.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//判断路径是否存在,若不存在则创建路径
DirectoryInfo upDir = new DirectoryInfo(phyPath);
if(!upDir.Exists)
![](http://pw.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
upDir.Create();
}
![](http://pw.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//
//保存文件
//
try
![](http://pw.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
postedFile.SaveAs(phyPath + fileName);
![](http://pw.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
fp.FilePath = filePath + fileName;
fp.FileExtension = fileExtension;
fp.FileName = fileName;
}
catch
![](http://pw.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
throw new ApplicationException("上传失败!");
}
![](http://pw.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://pw.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//返回上传文件的信息
return fp;
}
然后我们在上传文件的时候就可以调用这个方法了,将返回的文件信息保存到数据库中,至于下载,就直接打开那个路径就OK了。
第三部分:
这里我们主要说一下如何以二进制的形式上传文件以及下载。首先说上传,方法如下:
public
byte
[] UpLoadFile(HtmlInputFile f_IFile)
![](http://pw.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
//获取由客户端指定的上传文件的访问
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;
![](http://pw.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
这个方法返回的就是上传的文件的二进制字节流,这样我们就可以将它保存到数据库了。下面说一下这种形式的下载,也许你会想到这种方式的下载就是新建一个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;
![](http://pw.cnblogs.com/Images/OutliningIndicators/None.gif)
namespace
RuixinOA.Web.BaseClass
![](http://pw.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
![](http://pw.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// NetUFile 的摘要说明。
/// </summary>
public class OpenFile : IHttpHandler
![](http://pw.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
public void ProcessRequest(HttpContext context)
![](http://pw.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//从数据库中取出要下载的文件信息
RuixinOA.BusinessFacade.RX_OA_FileManager os = new RX_OA_FileManager();
EntityData data = os.GetFileDetail(id);
![](http://pw.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if(data != null && data.Tables["RX_OA_File"].Rows.Count > 0)
![](http://pw.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
DataRow dr = (DataRow)data.Tables["RX_OA_File"].Rows[0];
![](http://pw.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
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();
}
![](http://pw.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](http://pw.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public bool IsReusable
![](http://pw.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://pw.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://pw.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return true;}
}
}
}
执行上面的方法后,系统会提示用户选择直接打开还是下载。这一部分我们就说到这里。
第四部分:
这一部分主要说如何上传一个Internet上的资源到服务器。前面我们有一篇文章详细介绍了使用方法,这里我不再多说。