Asp.net文件的上传与下载

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

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

 /// <summary>
        
/// 文件上传
        
/// </summary>
        
/// <param name="filename">上传文件路径</param>

  private bool SaveFile(filename)
        
{            
            
string filePath=Request.PhysicalApplicationPath+"\\"+filename;                 
            
try
            
{                 
                
this.excelfilepath.PostedFile.SaveAs(filePath);  
                
return true;
            }

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

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

            
        }

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

/// <summary>
        
/// 文件下载
        
/// </summary>
        
/// <param name="filename">下载文件物理路径</param>

        public static void DownLoadFile(string path)
        

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

                
else
                
{

                    contxt.Response.Write(
"This file does not exist.");

                }

            }

        }


方法二:

    
/// <summary>
        
/// 文件下载
        
/// </summary>
        
/// <param name="filepath"></param>

        public static void DownLoadFileStream(string filepath)
        
{
            System.Web.HttpContext contxt
=System.Web.HttpContext.Current;
            
if(!File.Exists(filepath))
            
{
                
throw new Exception("该文件不存在!");
            }

            
//读取文件
            FileStream DownFiles = File.OpenRead(filepath);  
            
byte[] fileCont = new byte[DownFiles.Length];
            DownFiles.Read(fileCont,
0,(int)DownFiles.Length);
            contxt.Response.Clear();
            contxt.Response.AddHeader(
"Content-Disposition""attachment; filename=" + contxt.Server.UrlEncode(DownFiles.Name));
            contxt.Response.AddHeader(
"Content-Length", DownFiles.Length.ToString());
            contxt.Response.ContentEncoding
=System.Text.Encoding.Default;
            contxt.Response.Charset
="gb2312";
            contxt.Response.BinaryWrite(fileCont);
            contxt.Response.End();
        }
posted @ 2007-02-13 14:10  Hermes.Liu  阅读(1334)  评论(6编辑  收藏  举报