[转]ASP.NET中常用的文件上传下载方法
2009-03-16 14:58
627 查看
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())
public class FileUpLoad
<?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)
public byte[] UpLoadFile(HtmlInputFile f_IFile)
<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
执行上面的方法后,系统会提示用户选择直接打开还是下载。
第四部分:
将动态页面转换为二进制流:
由于我们一般的文件上传都是一个物理上存在的文件,而这里的流程标签页面是根据ID号动态生成的,刚开始我用了一个比较笨的方法,思路是:先将这个页面保存到服务器的硬盘上,然后再上传到数据库中。方法实现如下:
这样页面就以Word格式保存到计算机上了,然后执行上传到数据库的操作,这样也能实现客户的需求,但给人的感觉总是怪怪的,肯定是个下下策了。
首先需要引用 System.Net 这个命名空间,然后操作如下:
HttpWebRequest 可以从Internet上读取文件,因此可以很好的解决这个问题。
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())
public class FileUpLoad
<?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)
public byte[] UpLoadFile(HtmlInputFile f_IFile)
<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
执行上面的方法后,系统会提示用户选择直接打开还是下载。
第四部分:
将动态页面转换为二进制流:
由于我们一般的文件上传都是一个物理上存在的文件,而这里的流程标签页面是根据ID号动态生成的,刚开始我用了一个比较笨的方法,思路是:先将这个页面保存到服务器的硬盘上,然后再上传到数据库中。方法实现如下:
HttpContext.Current.Response.AppendHeader("Content-Disposition","attachment;filename=upload.doc"); HttpContext.Current.Response.Charset =""; HttpContext.Current.Response.ContentEncoding =System.Text.Encoding.GetEncoding("utf-8"); HttpContext.Current.Response.ContentType ="application/msword"; pn_upload.Page.EnableViewState =false; System.IO.StringWriter tw = new System.IO.StringWriter() ; System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw); pn_upload.RenderControl(hw); HttpContext.Current.Response.Write(tw.ToString()); HttpContext.Current.Response.End();
这样页面就以Word格式保存到计算机上了,然后执行上传到数据库的操作,这样也能实现客户的需求,但给人的感觉总是怪怪的,肯定是个下下策了。
首先需要引用 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上读取文件,因此可以很好的解决这个问题。
引自: http://pw.cnblogs.com/archive/2006/05/24/408427.html
相关文章推荐
- ASP.NET中常用的文件上传下载方法
- ASP.NET中常用的文件上传下载方法
- ASP.NET中常用的文件上传下载方法
- ASP.NET中常用的文件上传下载方法 (转)
- [转载]ASP.NET中常用的文件上传下载方法
- .NET技术 ASP.NET中常用的文件上传下载方法(多文件上传)
- ASP.NET中常用的文件上传下载方法
- ASP.NET中常用的文件上传下载方法
- ASP.NET中常用的文件上传下载方法
- ASP.NET中常用的文件上传下载方法
- ASP.NET中常用的文件上传下载方法
- ASP.NET中常用的文件上传下载方法
- ASP.NET中常用的文件上传下载方法
- ASP.NET中常用的文件上传下载方法
- ASP.NET中常用的文件上传下载方法
- ASP.NET中常用的文件上传下载方法
- ASP.NET中常用的文件上传下载方法 (转)
- ASP.NET中常用的文件上传下载方法
- ASP.NET中常用的文件上传下载方法
- ASP.NET中常用的文件上传下载方法