文件的上传下载是我们在实际项目开发过程中经常需要用到的技术,这里给出几种常见的方法,本文主要内容包括:
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里分块读取数据。实现方法如下:
<httpRuntime executionTimeout="300" maxRequestLength="40960" useFullyQualifiedRedirectUrl="false"/>这样上传文件的最大值就变成了4M,但这样并不能让我们无限的扩大MaxRequestLength的值,因为ASP.NET会将全部文件载入内存后,再加以处理。解决的方法是利用隐含的HttpWorkerRequest,用它的GetPreloadedEntityBody和ReadEntityBody方法从IIS为ASP.NET建立的pipe里分块读取数据。实现方法如下:
 IServiceProviderprovider=(IServiceProvider)HttpContext.Current;
IServiceProviderprovider=(IServiceProvider)HttpContext.Current;
 HttpWorkerRequestwr=(HttpWorkerRequest)provider.GetService(typeof(HttpWorkerRequest));
HttpWorkerRequestwr=(HttpWorkerRequest)provider.GetService(typeof(HttpWorkerRequest));
 byte[]bs=wr.GetPreloadedEntityBody();
byte[]bs=wr.GetPreloadedEntityBody();

 .
.
 if(!wr.IsEntireEntityBodyIsPreloaded())
if(!wr.IsEntireEntityBodyIsPreloaded())
 {
{
 intn=1024;
intn=1024;
 byte[]bs2=newbyte[n];
byte[]bs2=newbyte[n];
 while(wr.ReadEntityBody(bs2,n)>0)
while(wr.ReadEntityBody(bs2,n)>0)
 {
{

 ..
..
 }
}
 }这样就可以解决了大文件的上传问题了。
}这样就可以解决了大文件的上传问题了。
第二部分:
下面我们来介绍如何以文件形式将客户端的一个文件上传到服务器并返回上传文件的一些基本信息
首先我们定义一个类,用来存储上传的文件的信息(返回时需要)。
 public class FileUpLoad
public class FileUpLoad
 {
{
 public FileUpLoad()
   public FileUpLoad()
 {
   {

 }
   }
 /// <summary>
/// <summary>
 /// 上传文件名称
        /// 上传文件名称
 /// </summary>
        /// </summary>
 public string FileName
        public string FileName
 {
        {
 get
            get
 {
            {
 return fileName;
                return fileName;
 }
            }
 set
            set
 {
            {
 fileName = value;
                fileName = value;
 }
            }
 }
        }
 private string fileName;
        private string fileName;

 /// <summary>
        /// <summary>
 /// 上传文件路径
        /// 上传文件路径
 /// </summary>
        /// </summary>
 public string FilePath
        public string FilePath
 {
        {
 get
            get
 {
            {
 return filepath;
                return filepath;
 }
            }
 set
            set
 {
            {
 filepath = value;
                filepath = value;
 }
            }
 }
        }
 private string filepath;
        private string filepath;

 
        
 /// <summary>
        /// <summary>
 /// 文件扩展名
        /// 文件扩展名
 /// </summary>
        /// </summary>
 public string FileExtension
        public string FileExtension
 {
        {
 get
            get
 {
            {
 return fileExtension;
                return fileExtension;
 }
            }
 set
            set
 {
            {
 
            
 fileExtension = value;
                fileExtension = value;
 }
            }
 
                
 }
        }
 private string fileExtension;
        private string fileExtension;
 }
}
 <?xml version="1.0" encoding="gb2312" ?>
<?xml version="1.0" encoding="gb2312" ?>
 <Application>
<Application>    
 <FileUpLoad>
    <FileUpLoad>
 <Format>.jpg|.gif|.png|.bmp</Format>
       <Format>.jpg|.gif|.png|.bmp</Format>
 </FileUpLoad>
    </FileUpLoad>
 </Application>
</Application>
这样我们就可以开始写我们的上传文件的方法了,如下:
 public FileUpLoad UpLoadFile(HtmlInputFile InputFile,string filePath,string myfileName,bool isRandom)
public FileUpLoad UpLoadFile(HtmlInputFile InputFile,string filePath,string myfileName,bool isRandom)
 {
        {
 
            
 FileUpLoad fp = new FileUpLoad();
            FileUpLoad fp = new FileUpLoad();

 string fileName,fileExtension;
            string fileName,fileExtension;
 string saveName;
            string saveName;
 
            
 //
            //
 //建立上传对象
            //建立上传对象
 //
            //
 HttpPostedFile postedFile = InputFile.PostedFile;
            HttpPostedFile postedFile = InputFile.PostedFile;

 fileName        = System.IO.Path.GetFileName(postedFile.FileName);
            fileName        = System.IO.Path.GetFileName(postedFile.FileName);
 fileExtension    = System.IO.Path.GetExtension(fileName);
            fileExtension    = System.IO.Path.GetExtension(fileName);
 
            
 //
            //
 //根据类型确定文件格式
            //根据类型确定文件格式
 //
            //
 AppConfig app = new AppConfig();
            AppConfig app = new AppConfig();
 string format = app.GetPath("FileUpLoad/Format");
            string format = app.GetPath("FileUpLoad/Format");


 //
            //
 //如果格式都不符合则返回
            //如果格式都不符合则返回
 //
            //
 if(format.IndexOf(fileExtension)==-1)
            if(format.IndexOf(fileExtension)==-1)
 {
            {
 throw new ApplicationException("上传数据格式不合法");
                throw new ApplicationException("上传数据格式不合法");
 }
            }
 
                
 //
            //
 //根据日期和随机数生成随机的文件名
            //根据日期和随机数生成随机的文件名
 //
            //
 if(myfileName != string.Empty)
            if(myfileName != string.Empty)
 {
            {
 fileName = myfileName;
                fileName = myfileName;            
 }
            }

 if(isRandom)
            if(isRandom)
 {
            {
 Random objRand = new Random();
                Random objRand = new Random();
 System.DateTime date = DateTime.Now;
                System.DateTime date = DateTime.Now;
 //生成随机文件名
                //生成随机文件名
 saveName = date.Year.ToString() + date.Month.ToString() + date.Day.ToString() + date.Hour.ToString() + date.Minute.ToString()
                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);
                    + date.Second.ToString() + Convert.ToString(objRand.Next(99)*97 + 100);
 fileName = saveName + fileExtension;
                fileName = saveName + fileExtension;
 }
            }
 
            
 string phyPath = HttpContext.Current.Request.MapPath(filePath);
            string phyPath = HttpContext.Current.Request.MapPath(filePath);


 //判断路径是否存在,若不存在则创建路径
            //判断路径是否存在,若不存在则创建路径
 DirectoryInfo upDir = new DirectoryInfo(phyPath);
            DirectoryInfo upDir = new DirectoryInfo(phyPath);
 if(!upDir.Exists)
            if(!upDir.Exists)
 {
            {
 upDir.Create();
                upDir.Create();
 }
            }

 //
            //
 //保存文件
            //保存文件
 //
            //
 try
            try
 {
            {
 postedFile.SaveAs(phyPath + fileName);
                postedFile.SaveAs(phyPath + fileName);

 fp.FilePath =  filePath  + fileName;
                fp.FilePath =  filePath  + fileName;
 fp.FileExtension = fileExtension;
                fp.FileExtension = fileExtension;
 fp.FileName = fileName;
                fp.FileName = fileName;
 }
            }
 catch
            catch
 {
            {
 throw new ApplicationException("上传失败!");
                throw new ApplicationException("上传失败!");
 }
            }


 //返回上传文件的信息
            //返回上传文件的信息
 return fp;
            return fp;
 
        
 
        
 }
        }
 public byte[] UpLoadFile(HtmlInputFile f_IFile)
public byte[] UpLoadFile(HtmlInputFile f_IFile)
 {
{
 //获取由客户端指定的上传文件的访问
         //获取由客户端指定的上传文件的访问
 HttpPostedFile upFile=f_IFile.PostedFile;
         HttpPostedFile upFile=f_IFile.PostedFile;
 //得到上传文件的长度
         //得到上传文件的长度
 int upFileLength=upFile.ContentLength;
                int upFileLength=upFile.ContentLength; 
 //得到上传文件的客户端MIME类型
         //得到上传文件的客户端MIME类型
 string contentType = upFile.ContentType;
                string contentType = upFile.ContentType;
 byte[] FileArray=new Byte[upFileLength];
                byte[] FileArray=new Byte[upFileLength];
 
 
 Stream fileStream=upFile.InputStream;
                Stream fileStream=upFile.InputStream; 
 
                
 fileStream.Read(FileArray,0,upFileLength);
                fileStream.Read(FileArray,0,upFileLength);
 
        
 return FileArray;
        return FileArray;

 }
}
这个方法返回的就是上传的文件的二进制字节流,这样我们就可以将它保存到数据库了。下面说一下这种形式的下载,也许你会想到这种方式的下载就是新建一个aspx页面,然后在它的Page_Load()事件里取出二进制字节流,然后再读出来就可以了,其实这种方法是不可取的,在实际的运用中也许会出现无法打开某站点的错误,我一般采用下面的方法:
首先,在Web.config中加入:
 <add verb="*" path="openfile.aspx" type="RuixinOA.Web.BaseClass.OpenFile, RuixinOA.Web"/>
<add verb="*" path="openfile.aspx" type="RuixinOA.Web.BaseClass.OpenFile, RuixinOA.Web"/>
这表示我打开openfile.aspx这个页面时,系统就会自动转到执行RuixinOA.Web.BaseClass.OpenFile 这个类里的方法,具体实现如下:
 using System;
using System;
 using System.Data;
using System.Data;
 using System.Web;
using System.Web;
 using System.IO;
using System.IO;
 using Ruixin.WorkFlowDB;
using Ruixin.WorkFlowDB;
 using RXSuite.Base;
using RXSuite.Base;
 using RXSuite.Component;
using RXSuite.Component;
 using RuixinOA.BusinessFacade;
using RuixinOA.BusinessFacade;

 namespace RuixinOA.Web.BaseClass
namespace RuixinOA.Web.BaseClass
 {
{
 /// <summary>
    /// <summary>
 /// NetUFile 的摘要说明。
    /// NetUFile 的摘要说明。
 /// </summary>
    /// </summary>
 public class OpenFile : IHttpHandler
    public class OpenFile : IHttpHandler
 {
    {
 public void ProcessRequest(HttpContext context)
        public void ProcessRequest(HttpContext context) 
 {
        {
 
            
 //从数据库中取出要下载的文件信息
            //从数据库中取出要下载的文件信息
 RuixinOA.BusinessFacade.RX_OA_FileManager os = new RX_OA_FileManager();
            RuixinOA.BusinessFacade.RX_OA_FileManager os = new RX_OA_FileManager();
 EntityData data = os.GetFileDetail(id);
            EntityData data = os.GetFileDetail(id);

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

 context.Response.Buffer = true;
                context.Response.Buffer = true;
 context.Response.Clear();
                context.Response.Clear();
 context.Response.ContentType = dr["CContentType"].ToString();
                context.Response.ContentType = dr["CContentType"].ToString(); 
 context.Response.AddHeader("Content-Disposition","attachment;filename=" + HttpUtility.UrlEncode(dr["CTitle"].ToString()));
                context.Response.AddHeader("Content-Disposition","attachment;filename=" + HttpUtility.UrlEncode(dr["CTitle"].ToString()));
 context.Response.BinaryWrite((Byte[])dr["CContent"]);
                context.Response.BinaryWrite((Byte[])dr["CContent"]);
 context.Response.Flush();
                context.Response.Flush();
 context.Response.End();
                context.Response.End();
 }
            }
 
                        

 }
        }

 public bool IsReusable
        public bool IsReusable 
 {
        { 

 get { return true;}
            get { return true;} 
 }
        }
 }
    }
 }
}

执行上面的方法后,系统会提示用户选择直接打开还是下载。这一部分我们就说到这里。
第四部分:
这一部分主要说如何上传一个Internet上的资源到服务器。前面我们有一篇文章详细介绍了使用方法,这里我不再多说。
请参考:将动态页面转化成二进制字节流
第五部分:总结
今天简单的介绍了几种文件上传与下载的方法,都是在实际的项目开发中经常需要用到的,可能还有不完善的地方,希望大家可以互相交流一下项目开发中的经验。写的不好的地方,请指正,谢谢!
1、如何解决文件上传大小的限制
2、以文件形式保存到服务器
3、转换成二进制字节流保存到数据库以及下载方法
4、上传Internet上的资源
第一部分:
首先我们来说一下如何解决ASP.NET中的文件上传大小限制的问题,我们知道在默认情况下ASP.NET的文件上传大小限制为2M,一般情况下,我们可以采用更改WEB.Config文件来自定义最大文件大小,如下:
 <httpRuntime executionTimeout="300" maxRequestLength="40960" useFullyQualifiedRedirectUrl="false"/>
<httpRuntime executionTimeout="300" maxRequestLength="40960" useFullyQualifiedRedirectUrl="false"/> IServiceProviderprovider=(IServiceProvider)HttpContext.Current;
IServiceProviderprovider=(IServiceProvider)HttpContext.Current; HttpWorkerRequestwr=(HttpWorkerRequest)provider.GetService(typeof(HttpWorkerRequest));
HttpWorkerRequestwr=(HttpWorkerRequest)provider.GetService(typeof(HttpWorkerRequest)); byte[]bs=wr.GetPreloadedEntityBody();
byte[]bs=wr.GetPreloadedEntityBody();
 .
. if(!wr.IsEntireEntityBodyIsPreloaded())
if(!wr.IsEntireEntityBodyIsPreloaded()) {
{ intn=1024;
intn=1024; byte[]bs2=newbyte[n];
byte[]bs2=newbyte[n]; while(wr.ReadEntityBody(bs2,n)>0)
while(wr.ReadEntityBody(bs2,n)>0) {
{
 ..
.. }
} }
}第二部分:
下面我们来介绍如何以文件形式将客户端的一个文件上传到服务器并返回上传文件的一些基本信息
首先我们定义一个类,用来存储上传的文件的信息(返回时需要)。
 public class FileUpLoad
public class FileUpLoad {
{ public FileUpLoad()
   public FileUpLoad() {
   {
 }
   } /// <summary>
/// <summary> /// 上传文件名称
        /// 上传文件名称 /// </summary>
        /// </summary> public string FileName
        public string FileName {
        { get
            get {
            { return fileName;
                return fileName; }
            } set
            set {
            { fileName = value;
                fileName = value; }
            } }
        } private string fileName;
        private string fileName;
 /// <summary>
        /// <summary> /// 上传文件路径
        /// 上传文件路径 /// </summary>
        /// </summary> public string FilePath
        public string FilePath {
        { get
            get {
            { return filepath;
                return filepath; }
            } set
            set {
            { filepath = value;
                filepath = value; }
            } }
        } private string filepath;
        private string filepath;
 
         /// <summary>
        /// <summary> /// 文件扩展名
        /// 文件扩展名 /// </summary>
        /// </summary> public string FileExtension
        public string FileExtension {
        { get
            get {
            { return fileExtension;
                return fileExtension; }
            } set
            set {
            { 
             fileExtension = value;
                fileExtension = value; }
            } 
                 }
        } private string fileExtension;
        private string fileExtension; }
}另外我们还可以在配置文件中限制上传文件的格式(App.Config):
 <?xml version="1.0" encoding="gb2312" ?>
<?xml version="1.0" encoding="gb2312" ?> <Application>
<Application>     <FileUpLoad>
    <FileUpLoad> <Format>.jpg|.gif|.png|.bmp</Format>
       <Format>.jpg|.gif|.png|.bmp</Format> </FileUpLoad>
    </FileUpLoad> </Application>
</Application>这样我们就可以开始写我们的上传文件的方法了,如下:
 public FileUpLoad UpLoadFile(HtmlInputFile InputFile,string filePath,string myfileName,bool isRandom)
public FileUpLoad UpLoadFile(HtmlInputFile InputFile,string filePath,string myfileName,bool isRandom) {
        { 
             FileUpLoad fp = new FileUpLoad();
            FileUpLoad fp = new FileUpLoad();
 string fileName,fileExtension;
            string fileName,fileExtension; string saveName;
            string saveName; 
             //
            // //建立上传对象
            //建立上传对象 //
            // HttpPostedFile postedFile = InputFile.PostedFile;
            HttpPostedFile postedFile = InputFile.PostedFile;
 fileName        = System.IO.Path.GetFileName(postedFile.FileName);
            fileName        = System.IO.Path.GetFileName(postedFile.FileName); fileExtension    = System.IO.Path.GetExtension(fileName);
            fileExtension    = System.IO.Path.GetExtension(fileName); 
             //
            // //根据类型确定文件格式
            //根据类型确定文件格式 //
            // AppConfig app = new AppConfig();
            AppConfig app = new AppConfig(); string format = app.GetPath("FileUpLoad/Format");
            string format = app.GetPath("FileUpLoad/Format");

 //
            // //如果格式都不符合则返回
            //如果格式都不符合则返回 //
            // if(format.IndexOf(fileExtension)==-1)
            if(format.IndexOf(fileExtension)==-1) {
            { throw new ApplicationException("上传数据格式不合法");
                throw new ApplicationException("上传数据格式不合法"); }
            } 
                 //
            // //根据日期和随机数生成随机的文件名
            //根据日期和随机数生成随机的文件名 //
            // if(myfileName != string.Empty)
            if(myfileName != string.Empty) {
            { fileName = myfileName;
                fileName = myfileName;             }
            }
 if(isRandom)
            if(isRandom) {
            { Random objRand = new Random();
                Random objRand = new Random(); System.DateTime date = DateTime.Now;
                System.DateTime date = DateTime.Now; //生成随机文件名
                //生成随机文件名 saveName = date.Year.ToString() + date.Month.ToString() + date.Day.ToString() + date.Hour.ToString() + date.Minute.ToString()
                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);
                    + date.Second.ToString() + Convert.ToString(objRand.Next(99)*97 + 100); fileName = saveName + fileExtension;
                fileName = saveName + fileExtension; }
            } 
             string phyPath = HttpContext.Current.Request.MapPath(filePath);
            string phyPath = HttpContext.Current.Request.MapPath(filePath);

 //判断路径是否存在,若不存在则创建路径
            //判断路径是否存在,若不存在则创建路径 DirectoryInfo upDir = new DirectoryInfo(phyPath);
            DirectoryInfo upDir = new DirectoryInfo(phyPath); if(!upDir.Exists)
            if(!upDir.Exists) {
            { upDir.Create();
                upDir.Create(); }
            }
 //
            // //保存文件
            //保存文件 //
            // try
            try {
            { postedFile.SaveAs(phyPath + fileName);
                postedFile.SaveAs(phyPath + fileName);
 fp.FilePath =  filePath  + fileName;
                fp.FilePath =  filePath  + fileName; fp.FileExtension = fileExtension;
                fp.FileExtension = fileExtension; fp.FileName = fileName;
                fp.FileName = fileName; }
            } catch
            catch {
            { throw new ApplicationException("上传失败!");
                throw new ApplicationException("上传失败!"); }
            }

 //返回上传文件的信息
            //返回上传文件的信息 return fp;
            return fp; 
         
         }
        }然后我们在上传文件的时候就可以调用这个方法了,将返回的文件信息保存到数据库中,至于下载,就直接打开那个路径就OK了。
第三部分:
    这里我们主要说一下如何以二进制的形式上传文件以及下载。首先说上传,方法如下:
 public byte[] UpLoadFile(HtmlInputFile f_IFile)
public byte[] UpLoadFile(HtmlInputFile f_IFile) {
{ //获取由客户端指定的上传文件的访问
         //获取由客户端指定的上传文件的访问 HttpPostedFile upFile=f_IFile.PostedFile;
         HttpPostedFile upFile=f_IFile.PostedFile; //得到上传文件的长度
         //得到上传文件的长度 int upFileLength=upFile.ContentLength;
                int upFileLength=upFile.ContentLength;  //得到上传文件的客户端MIME类型
         //得到上传文件的客户端MIME类型 string contentType = upFile.ContentType;
                string contentType = upFile.ContentType; byte[] FileArray=new Byte[upFileLength];
                byte[] FileArray=new Byte[upFileLength]; 
  Stream fileStream=upFile.InputStream;
                Stream fileStream=upFile.InputStream;  
                 fileStream.Read(FileArray,0,upFileLength);
                fileStream.Read(FileArray,0,upFileLength); 
         return FileArray;
        return FileArray;
 }
}这个方法返回的就是上传的文件的二进制字节流,这样我们就可以将它保存到数据库了。下面说一下这种形式的下载,也许你会想到这种方式的下载就是新建一个aspx页面,然后在它的Page_Load()事件里取出二进制字节流,然后再读出来就可以了,其实这种方法是不可取的,在实际的运用中也许会出现无法打开某站点的错误,我一般采用下面的方法:
首先,在Web.config中加入:
 <add verb="*" path="openfile.aspx" type="RuixinOA.Web.BaseClass.OpenFile, RuixinOA.Web"/>
<add verb="*" path="openfile.aspx" type="RuixinOA.Web.BaseClass.OpenFile, RuixinOA.Web"/>这表示我打开openfile.aspx这个页面时,系统就会自动转到执行RuixinOA.Web.BaseClass.OpenFile 这个类里的方法,具体实现如下:
 using System;
using System; using System.Data;
using System.Data; using System.Web;
using System.Web; using System.IO;
using System.IO; using Ruixin.WorkFlowDB;
using Ruixin.WorkFlowDB; using RXSuite.Base;
using RXSuite.Base; using RXSuite.Component;
using RXSuite.Component; using RuixinOA.BusinessFacade;
using RuixinOA.BusinessFacade;
 namespace RuixinOA.Web.BaseClass
namespace RuixinOA.Web.BaseClass {
{ /// <summary>
    /// <summary> /// NetUFile 的摘要说明。
    /// NetUFile 的摘要说明。 /// </summary>
    /// </summary> public class OpenFile : IHttpHandler
    public class OpenFile : IHttpHandler {
    { public void ProcessRequest(HttpContext context)
        public void ProcessRequest(HttpContext context)  {
        { 
             //从数据库中取出要下载的文件信息
            //从数据库中取出要下载的文件信息 RuixinOA.BusinessFacade.RX_OA_FileManager os = new RX_OA_FileManager();
            RuixinOA.BusinessFacade.RX_OA_FileManager os = new RX_OA_FileManager(); EntityData data = os.GetFileDetail(id);
            EntityData data = os.GetFileDetail(id);
 if(data != null && data.Tables["RX_OA_File"].Rows.Count > 0)
            if(data != null && data.Tables["RX_OA_File"].Rows.Count > 0) {
            { DataRow dr = (DataRow)data.Tables["RX_OA_File"].Rows[0];
                DataRow dr = (DataRow)data.Tables["RX_OA_File"].Rows[0];
 context.Response.Buffer = true;
                context.Response.Buffer = true; context.Response.Clear();
                context.Response.Clear(); context.Response.ContentType = dr["CContentType"].ToString();
                context.Response.ContentType = dr["CContentType"].ToString();  context.Response.AddHeader("Content-Disposition","attachment;filename=" + HttpUtility.UrlEncode(dr["CTitle"].ToString()));
                context.Response.AddHeader("Content-Disposition","attachment;filename=" + HttpUtility.UrlEncode(dr["CTitle"].ToString())); context.Response.BinaryWrite((Byte[])dr["CContent"]);
                context.Response.BinaryWrite((Byte[])dr["CContent"]); context.Response.Flush();
                context.Response.Flush(); context.Response.End();
                context.Response.End(); }
            } 
                        
 }
        }
 public bool IsReusable
        public bool IsReusable  {
        { 
 get { return true;}
            get { return true;}  }
        } }
    } }
}
执行上面的方法后,系统会提示用户选择直接打开还是下载。这一部分我们就说到这里。
第四部分:
这一部分主要说如何上传一个Internet上的资源到服务器。前面我们有一篇文章详细介绍了使用方法,这里我不再多说。
请参考:将动态页面转化成二进制字节流
第五部分:总结
今天简单的介绍了几种文件上传与下载的方法,都是在实际的项目开发中经常需要用到的,可能还有不完善的地方,希望大家可以互相交流一下项目开发中的经验。写的不好的地方,请指正,谢谢!
 
                    
                

 
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号