ASP.NET中文件上传下载方法集合(2)
点击次数:19 次 发布日期:2008-11-26 13:58:29 作者:源代码网
|
源代码网推荐 源代码网推荐 这里我们主要说一下如何以二进制的形式上传文件以及下载。首先说上传,方法如下: 源代码网推荐 源代码网推荐 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 源代码网推荐 { 源代码网推荐 /**//// <summary> 源代码网推荐 /// NetUFile 的摘要说明。 源代码网推荐 /// </summary> 源代码网推荐 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上读取文件,因此可以很好的解决这个问题。 源代码网推荐 源代码网推荐 第五部分:总结 源代码网推荐 源代码网推荐 今天简单的介绍了几种文件上传与下载的方法,都是在实际的项目开发中经常需要用到的,可能还有不完善的地方,希望大家可以互相交流一下项目开发中的经验。 做人要厚道,请注明转自酷网动力(www.ASPCOOL.COM)。 源代码网推荐 源代码网供稿. |
