try { HttpPostedFile file = HttpContext.Current.Request.Files["uploadfile"]; if (file != null) { //文件扩展类型判断 if (IsAllowedExtension(file)) { string returnurl = UpLoad(file); if (string.IsNullOrEmpty(returnurl)) { context.Put("json", LitJson.JsonMapper.ToJson(new { code = 1, msg = "上传失败" })); } else { context.Put("json", LitJson.JsonMapper.ToJson(new { code = 0, data = returnurl })); } } else { context.Put("json", LitJson.JsonMapper.ToJson(new { code = 1, msg = "文件格式不正确" })); } } else { context.Put("json", LitJson.JsonMapper.ToJson(new { code = 1, msg = "文件对象为null" })); } } catch (Exception e) { context.Put("json", LitJson.JsonMapper.ToJson(new { code = 1, msg = e.Message })); }
private bool IsAllowedExtension(HttpPostedFile hifile) { bool ret = false; String newFile = System.Web.HttpContext.Current.Server.MapPath("~/" + System.Guid.NewGuid().ToString("D") + ".tmp"); hifile.SaveAs(newFile); System.IO.FileStream fs = new System.IO.FileStream(newFile, System.IO.FileMode.Open, System.IO.FileAccess.Read); System.IO.BinaryReader r = new System.IO.BinaryReader(fs); string fileclass = ""; byte buffer; try { buffer = r.ReadByte(); fileclass = buffer.ToString(); buffer = r.ReadByte(); fileclass += buffer.ToString(); } catch { return false; } r.Close(); fs.Close(); /*文件扩展名说明 *7173 gif *255216 jpg *13780 png *6677 bmp *239187 txt,aspx,asp,sql *208207 xls.doc.ppt *6063 xml *6033 htm,html *4742 js *8075 xlsx,zip,pptx,mmap,zip *8297 rar *01 accdb,mdb *7790 exe,dll *5666 psd *255254 rdp *10056 bt种子 *64101 bat * PDF = 3780, */ String[] fileType = { "255216", "7173", "6677", "13780", "8297", "5549", "208207","8075","207208","3780" }; for (int i = 0; i < fileType.Length; i++) { if (fileclass == fileType[i]) { ret = true; break; } } System.IO.File.Delete(newFile); return ret; }
protected string uploadFiles(HttpPostedFile userPostedFile) { string fileName, fileExtension;//文件名,文件类型 fileName = System.IO.Path.GetFileName(userPostedFile.FileName); fileExtension = System.IO.Path.GetExtension(fileName).ToLower(); int FileLen = userPostedFile.ContentLength; Byte[] FileData = new Byte[FileLen]; Stream sr = userPostedFile.InputStream;//创建数据流对象 sr.Read(FileData, 0, FileLen); sr.Close(); WebReference.PicUpload pu = new WebReference.PicUpload(); //localhost.PicUpload pu = new localhost.PicUpload(); string currentpath = "/upfiles/"; //这是上传后的图片路径 //string filepath = Server.MapPath(currentpath); //文件夹名,以日期为文件夹名,图片所在这个地方 string filepath = "D:\\dzt\\CommodityPic\\"; //HttpPostedFile userPostedFile = Request.Files["File1"]; if (FileLen > 0 && FileLen / 1024 < 8192) { string file_name = System.DateTime.Now.ToString("yyyyMMddHHmmssfff"); //新文件名 string sPath = "MerchantPic"; //创建日期文件夹 string file_ext = System.IO.Path.GetFileName(userPostedFile.FileName).Split('.')[1]; string ext = userPostedFile.ContentType; //获取文件类型 if (ext == "image/pjpeg" || ext == "image/jpeg" || ext == "image/png" || ext == "image/jpg") { string sPath2 = filepath + sPath; //if (!Directory.Exists(sPath2)) //判断日期文件夹是否存在 //{ // Directory.CreateDirectory(sPath2);//创建日期文件夹 //} string fpath = filepath + sPath + "\\" + file_name + "." + file_ext; if (File.Exists(fpath)) { file_name = file_name + "1"; fpath = filepath + sPath + "\\" + file_name + "." + file_ext; } string uploadpath = sPath + "/" + file_name + "." + file_ext; bool result = pu.uploadFiles(FileData, FileLen, fpath, sPath2); //userPostedFile.SaveAs(fpath); if (result) { return uploadpath; } else { return ""; } } else { return "1"; } } else { return "2"; } //string currentpath = "/upfiles/"; //这是上传后的图片路径 ////string filepath = Server.MapPath(currentpath); //文件夹名,以日期为文件夹名,图片所在这个地方 //string filepath = "E:\\dzt\\CommodityPic\\"; ////HttpPostedFile userPostedFile = Request.Files["File1"]; //if (userPostedFile.ContentLength / 1024 > 0 && userPostedFile.ContentLength / 1024 < 8192) //{ // string file_name = System.DateTime.Now.ToString("yyyyMMddHHmmssfff"); //新文件名 // string sPath = "MerchantPic"; //创建日期文件夹 // string file_ext = System.IO.Path.GetFileName(userPostedFile.FileName).Split('.')[1]; // string ext = userPostedFile.ContentType; //获取文件类型 // if (ext == "image/pjpeg" || ext == "image/jpeg" || ext == "image/png" || ext == "image/jpg") // { // string sPath2 = filepath + sPath; // if (!Directory.Exists(sPath2)) //判断日期文件夹是否存在 // { // Directory.CreateDirectory(sPath2);//创建日期文件夹 // } // string fpath = filepath + sPath + "\\" + file_name + "." + file_ext; // if (File.Exists(fpath)) // { // file_name = file_name + "1"; // fpath = filepath + sPath + "\\" + file_name + "." + file_ext; // } // string uploadpath = sPath + "/" + file_name + "." + file_ext; // userPostedFile.SaveAs(fpath); // return uploadpath; // } // else // { // return "1"; // } //} //else //{ // return "2"; //} }
public bool uploadFiles(byte[] FileByteArray, int FileLength, string SaveToUrl,string sPath2) { try { if (!Directory.Exists(sPath2)) //判断日期文件夹是否存在 { Directory.CreateDirectory(sPath2);//创建日期文件夹 } FileStream fs = new FileStream(SaveToUrl, FileMode.OpenOrCreate, FileAccess.Write); fs.Write(FileByteArray, 0, FileLength); fs.Close(); } catch(Exception ex) { LogUtils.WriteLog(LogUtils.LogType.ERROR, ex.Message); return false; } return true; }
浙公网安备 33010602011771号