Asp.net文件的上传与下载

在我们实际的应用项目开发中,文件的上传和下载都是是必不可少的,可以说是随处可见。对于DotNet下的文件上传已经是一件再简单不过的事情了,.NET提供了强大的类库System.IO 来实现IO的所有操作。对于文件的下载也是非常简单的事情,下面我将总结一些我的实际经验,希望能给初学者们一点帮助。

首先是文件的上传,文件上传必须要保证ASP.NET用户对你所要操作的服务器端文件夹有写的权限,这样剩下的事情就非常easy了,示例代码如下:

ExpandedBlockStart.gif ContractedBlock.gif   /**/ /// <summary>
InBlock.gif        
/// 文件上传
InBlock.gif        
/// </summary>
ExpandedBlockEnd.gif        
/// <param name="filename">上传文件路径</param>

None.gif    private   bool  SaveFile(filename)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {            
InBlock.gif            
string filePath=Request.PhysicalApplicationPath+"\\"+filename;                 
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{                 
InBlock.gif                
this.excelfilepath.PostedFile.SaveAs(filePath);  
InBlock.gif                
return true;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch(UnauthorizedAccessException ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                Response.Write(
"<script>alert(\"没有写入权限,访问失败!详情:"+ex.ToString()+"\");</script>");
InBlock.gif                
return false;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch(Exception ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                Response.Write(
"<script>alert(\"文件上传失败!详情:"+ex.ToString()+"建议:文件可能太大!\");</script>");
InBlock.gif                
return false;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
ExpandedBlockEnd.gif        }

None.gif

对于文件的下载我总结了两种方法,其实都是大同小异,示例代码如下:
方法一:  

ExpandedBlockStart.gif ContractedBlock.gif /**/ /// <summary>
InBlock.gif        
/// 文件下载
InBlock.gif        
/// </summary>
ExpandedBlockEnd.gif        
/// <param name="filename">下载文件物理路径</param>

None.gif          public   static   void  DownLoadFile( string  path)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif
InBlock.gif            System.Web.HttpContext contxt
=System.Web.HttpContext.Current;
InBlock.gif            
if (path != "")
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
//string path = HttpContext.Current.Server.MapPath(filename);
InBlock.gif
                System.IO.FileInfo file = new System.IO.FileInfo(path);
InBlock.gif                
if (file.Exists)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{                    
InBlock.gif                    contxt.Response.Clear();
InBlock.gif                    contxt.Response.AddHeader(
"Content-Disposition""attachment; filename=" + contxt.Server.UrlEncode(file.Name));
InBlock.gif                    contxt.Response.AddHeader(
"Content-Length", file.Length.ToString());
InBlock.gif                    contxt.Response.ContentEncoding
=System.Text.Encoding.Default;
InBlock.gif                    contxt.Response.Charset
="gb2312";
InBlock.gif                    contxt.Response.ContentType 
= "application/octet-stream";
InBlock.gif                    contxt.Response.Filter.Close();
InBlock.gif                    contxt.Response.WriteFile(file.FullName);               
InBlock.gif                    contxt.Response.End();
ExpandedSubBlockEnd.gif                }

InBlock.gif                
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif
InBlock.gif                    contxt.Response.Write(
"This file does not exist.");
InBlock.gif
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

ExpandedBlockEnd.gif        }

None.gif

方法二:
None.gif
ExpandedBlockStart.gifContractedBlock.gif    
/**/ /// <summary>
InBlock.gif        
/// 文件下载
InBlock.gif        
/// </summary>
ExpandedBlockEnd.gif        
/// <param name="filepath"></param>

None.gif          public   static   void  DownLoadFileStream( string  filepath)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            System.Web.HttpContext contxt
=System.Web.HttpContext.Current;
InBlock.gif            
if(!File.Exists(filepath))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
throw new Exception("该文件不存在!");
ExpandedSubBlockEnd.gif            }

InBlock.gif            
//读取文件
InBlock.gif
            FileStream DownFiles = File.OpenRead(filepath);  
InBlock.gif            
byte[] fileCont = new byte[DownFiles.Length];
InBlock.gif            DownFiles.Read(fileCont,
0,(int)DownFiles.Length);
InBlock.gif            contxt.Response.Clear();
InBlock.gif            contxt.Response.AddHeader(
"Content-Disposition""attachment; filename=" + contxt.Server.UrlEncode(DownFiles.Name));
InBlock.gif            contxt.Response.AddHeader(
"Content-Length", DownFiles.Length.ToString());
InBlock.gif            contxt.Response.ContentEncoding
=System.Text.Encoding.Default;
InBlock.gif            contxt.Response.Charset
="gb2312";
InBlock.gif            contxt.Response.BinaryWrite(fileCont);
InBlock.gif            contxt.Response.End();
ExpandedBlockEnd.gif        }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值