服务器获取上传来的文件

public class FileUpload : IHttpHandler {
   
public void ProcessRequest (HttpContext context) {
        context.Response.ContentType
= "text/plain";
        HttpPostedFile f
=context.Request.Files[0];//接收浏览器端传递过来的文件。
        string filePath = "Files";
       
if (f.ContentLength > 0)//判断文件的大小
        {
          
string fileName= System.IO.Path.GetFileName(f.FileName);//获得具体的文件名称.
           filePath = context.Server.MapPath(filePath + "/" + fileName);//指定服务端保存文件的路径。(对文件操作一定是物理路径)
           f.SaveAs(filePath);//将上传的文件保存到指定的目录。
           context.Response.Write("文件上传成功");
        }
    }
   
public bool IsReusable {
       
get {
           
return false;
        }
    }
}      

posted @ 2012-08-16 16:47  Cn.Ruyi  阅读(280)  评论(0编辑  收藏  举报