文件上传asp.net后台

来源:互联网 发布:唐能通选股软件 编辑:程序博客网 时间:2024/05/17 02:25


 using System;
 3: using System.Web;
 4: using System.IO;
 5: using System.Web.Script.Serialization;
 6:  
 7: namespace MyApplication
 8: {
 9: public class FileUploadHandler : IHttpHandler
 10: {
 11: public void ProcessRequest(HttpContext context)
 12: {
 13: int iTotal = context.Request.Files.Count;
 14:  
 15: if (iTotal == 0) return;
 16:  
 17: HttpPostedFile file = context.Request.Files[0];
 18: int len = file.ContentLength;
 19: 
 20: if (len > 0 && !string.IsNullOrEmpty(file.FileName))
 21: {
 22: string parentPath = HttpContext.Current.Server.MapPath("./upload/");
 23:  
 24: if (!Directory.Exists(parentPath))
 25: {
 26: Directory.CreateDirectory(parentPath);
 27: }
 28:  
 29: string guidPath = Path.Combine(parentPath, System.Guid.NewGuid().ToString());
 30:  
 31: Directory.CreateDirectory(guidPath);
 32:  
 33: //保存文件
 34: file.SaveAs(Path.Combine(guidPath, Path.GetFileName(file.FileName)));
 35: 
 36: FileInfo info = new FileInfo();
 37: info.path = Path.Combine(guidPath, Path.GetFileName(file.FileName));
 38: info.name = Path.GetFileName(file.FileName);
 39: info.tp = Path.GetExtension(file.FileName).ToUpper();
 40: info.size = len.ToString ();
 41: 
 42: //序列化
 43: JavaScriptSerializer j = new JavaScriptSerializer();
 44:  
 45: context.Response.Write(j.Serialize(info));
 46: context.Response.End();
 47: }
 48: }
 49:  
 50: public bool IsReusable
 51: {
 52: get
 53: {
 54: return false;
 55: }
 56: }
 57: }
 58:  
 59: public class FileInfo
 60: {
 61: public string name;
 62: public string path;
 63: public string size;
 64: public string tp;
 65: }
 66: }



09年新晋3D主流网游《天下贰》,网易六年亿资打造
原创粉丝点击