1 /// <summary> 2 /// 文件上传 3 /// </summary> 4 public class FileUpload 5 { 6 /// <summary> 7 /// 单个文件上传(只获取第一个文件,返回的文件名是文件的md5值),返回为json数据格式,成功返回{status:"success",website:"a.jpg"},失败,返回{status:"error",website:"error"} 8 /// </summary> 9 /// <param name="context">上下文</param> 10 /// <param name="FilePath">文件路径</param> 11 /// <param name="outFileName">返回文件的md5</param> 12 /// <returns>返回json状态信息</returns> 13 public static string FileUploadSingle(HttpContext context, string FilePath,out string outFileName) 14 { 15 string json=""; 16 //找到目标文件对象 17 HttpFileCollection hfc = context.Request.Files; 18 HttpPostedFile hpf = hfc[0]; 19 20 if (hpf.ContentLength > 0) 21 { 22 //根据文件的md5的hash值做文件名,防止文件的重复和图片的浪费 23 string FileName = CreateFileForFileNameByMd5(hpf.FileName); //CreateDateTimeForFileName(hpf.FileName);//自动生成文件名 24 25 string file = System.IO.Path.Combine(FilePath, 26 FileName); 27 if (!Directory.Exists(Path.GetDirectoryName(file))) 28 { 29 Directory.CreateDirectory(file); 30 } 31 hpf.SaveAs(file); 32 json = "{status:"success",website:"" + FileName + ""}"; 33 outFileName = FileName; 34 } 35 else { 36 json = "{status:"error",website:"error"}"; 37 outFileName = FilePath; 38 } 39 40 return json; 41 42 } 43 /// <summary> 44 /// 单个文件上传(只获取第一个文件,返回的文件名是文件的md5值),返回为json数据格式,成功返回{status:"success",website:"a.jpg"},失败,返回{status:"error",website:"error"} 45 /// </summary> 46 /// <param name="context">上下文</param> 47 /// <param name="FilePath">文件路径</param> 48 /// <param name="outFileName">返回文件的md5</param> 49 /// <returns>返回json状态信息</returns> 50 public static string FileUploadMulti(HttpContext context, string FilePath, out string[] outFileName) 51 { 52 53 string json = ""; 54 //找到目标文件对象 55 HttpFileCollection hfc = context.Request.Files; 56 outFileName=new string[hfc.Count]; 57 for (int i=0;i<hfc.Count;i++) 58 { 59 if (hfc[i].ContentLength > 0) 60 { 61 //根据文件的md5的hash值做文件名,防止文件的重复和图片的浪费 62 string FileName = CreateFileForFileNameByMd5(hfc[i].FileName); //CreateDateTimeForFileName(hpf.FileName);//自动生成文件名 63 64 string file = System.IO.Path.Combine(FilePath, 65 FileName); 66 if (!Directory.Exists(Path.GetDirectoryName(file))) 67 { 68 Directory.CreateDirectory(file); 69 } 70 hfc[i].SaveAs(file); 71 outFileName[i]= FileName; 72 } 73 74 } 75 //如果文件非空,就返回json数据 76 if (outFileName.Count() > 0) 77 json = "{status:"success",website:"" + string.Join(",",outFileName) + ""}"; 78 else 79 json = "{status:"error",website:"error"}"; 80 81 82 return json; 83 84 } 85 /// <summary> 86 /// 根据文件的md5值当作文件的文件名( 87 /// </summary> 88 /// <param name="FileName">文件名</param> 89 /// <returns>文件的md5值</returns> 90 public static string CreateFileForFileNameByMd5(string FileName) 91 { 92 //return TZHSWEET.Common.MD5Provider.Hash(FileName)+Path.GetExtension(FileName); 93 return ""; 94 } 95 96 /// <summary> 97 /// 根据当前时间生成文件名 98 /// </summary> 99 /// <returns></returns> 100 public static string CreateDateTimeForFileName(string FileName) 101 { 102 string datetime=DateTime.Now.ToString ("yyyyMMddhhmmssffff"); 103 string FilePath=datetime+Path.GetExtension(FileName); 104 return FilePath; 105 } 106 }