private static string uperror(HtmlInputFile ControlId, float FileSize) { string output = ""; string filename = ControlId.PostedFile.FileName; if (filename == "") { output = "请选择图片..."; } else { float FileLength = float.Parse((float.Parse(ControlId.PostedFile.ContentLength.ToString()) / 1024 / 1024).ToString("#0.00")); if (FileLength > FileSize) { output = "文件大小不得超过" + FileSize + "M" + "<font style=\"color:#000;\">( 当前文件大小约为 " + FileLength + "M )</font>"; } FileExtension[] fe = { FileExtension.GIF, FileExtension.JPG, FileExtension.BMP }; if (!FileValidation.IsAllowedExtension(ControlId, fe)) { string style = FileNodeName(fe); output = "只支持" + style + "格式图片"; } else { output = ""; } } return(output); }
public JsonResult UploadImage(int type) { if (HttpContext.Request.Files.Count > 0) { string mongodbName = MongoDbNameArray[type]; if (string.IsNullOrEmpty(mongodbName)) { return(Json(new JsonBaseEntity { ErrorCode = "10000", ErrorMessage = "上传参数错误!" })); } try { var file = HttpContext.Request.Files[0]; FileExtension[] fileEx = { FileExtension.BMP, FileExtension.JPG, FileExtension.GIF, FileExtension.PNG }; if (!FileValidation.IsAllowedExtension(file, fileEx)) { return(Json(new JsonBaseEntity { ErrorCode = "10000", ErrorMessage = "请上传JPG、JPEG、PNG、BMP格式的图片!" })); } string newFileName = Guid.NewGuid() + Path.GetExtension(file.FileName); newFileName = newFileName.ToUpper(); if (file.InputStream.Length > 1024 * 1024 * 1) //大于1M,就等比压缩 { file.InputStream.Seek(0, SeekOrigin.Begin); //将当前流的位置设置为初始位置。很重要,很重要,很重要!!! Image uploaderImg = Image.FromStream(file.InputStream); Image newImage = ImageHelper.UniformScaleDeflate(uploaderImg, 600, 600); // 压缩图片,保证图片的最大宽度为800px,高度按照比例等比压缩。 Stream newStream = ImageToByteHelper.ImgToStream(newImage); MongoDBHelper.SetFileByName(mongodbName, newStream, newFileName); } else { MongoDBHelper.SetFileByName(mongodbName, file.InputStream, newFileName); } return(Json(new JsonUploadFileEntity { ErrorCode = "10001", ErrorMessage = "上传成功", FileUrl = string.Format("{0}/TuPian/Images/{1}/Show/{2}", "", type, newFileName), FileName = newFileName })); } catch (Exception ex) { logger.ErrorFormat("function:{0},location:{1},params:type={2},Message={3},StackTrace={4},Source={5}", "UploadImage", "MongoDBLegacyHelper.SetFileByName", type, ex.Message, ex.StackTrace, ex.Source); return(Json(new JsonBaseEntity { ErrorCode = "10000", ErrorMessage = "服务端异常" })); } } else { return(Json(new JsonBaseEntity { ErrorCode = "10002", ErrorMessage = "请选择图片" })); } }
protected virtual void Page_Load(object sender, EventArgs e) { this.DataType = HTTPRequest.GetString("DataType"); this.Act = HTTPRequest.GetString("Act"); this.Do = HTTPRequest.GetString("Do"); DataTable dt = new DataTable(); string filename = ""; if (ispost) { string _PathStr = HttpContext.Current.Server.MapPath("/") + PathStr; if (!Directory.Exists(_PathStr)) { Directory.CreateDirectory(_PathStr); } HttpFileCollection files = HttpContext.Current.Request.Files; try { if (files.Count > 0) { FileExtension[] fe = { FileExtension.XLS, FileExtension.XLSX }; HttpPostedFile postedFile = files[0]; int fileLen = postedFile.ContentLength; byte[] fileArray = new byte[fileLen]; postedFile.InputStream.Read(fileArray, 0, fileLen); System.IO.MemoryStream fs = new System.IO.MemoryStream(fileArray); try { if (FileValidation.IsAllowedExtension(fs, fe)) { filename = _PathStr + '/' + postedFile.FileName; postedFile.SaveAs(filename); dt = Public.Excels.ExcelToTableForXLSX(filename); } } finally { fs.Close(); } } else { this.AddErrLine("没有文件数据"); } } catch (Exception ex) { this.AddErrLine(ex.Message); } } switch (this.DataType) { case "Products": if (CheckUserPopedoms("2-2-2-1") || CheckUserPopedoms("X")) { this.Products_do(this.Act, this.Do, dt); } else { AddErrLine("权限不足!"); } break; case "Supplier": if (CheckUserPopedoms("2-2-3-1") || CheckUserPopedoms("X")) { this.Supplier_do(this.Act, this.Do, dt); } else { AddErrLine("权限不足!"); } break; case "Stores": if (CheckUserPopedoms("2-2-4-1") || CheckUserPopedoms("X")) { this.Stores_do(this.Act, this.Do, dt); } else { AddErrLine("权限不足!"); } break; case "Storage": if (CheckUserPopedoms("2-2-1-1") || CheckUserPopedoms("X")) { this.Storage_do(this.Act, this.Do, dt); } else { AddErrLine("权限不足!"); } break; } }
private UploadMessage _uploadImage(string companyID, bool convertToThumbnail = true) { if (HttpContext.Current.Request.Files.AllKeys.Any()) { FileExtension[] allowFileExtension = { FileExtension.BMP, FileExtension.GIF, FileExtension.JPG, FileExtension.PNG }; if (!FileValidation.IsAllowedExtension(HttpContext.Current.Request.Files["file"], allowFileExtension)) { return(errorMsg("不接受的文件格式")); } // DEFINE THE PATH WHERE WE WANT TO SAVE THE FILES. string folderPath = WebApiApplication.UploadFolderPath; if (folderPath.StartsWith("~") || folderPath.StartsWith(".")) //相对路径 { folderPath = HttpContext.Current.Server.MapPath(folderPath + "/images/" + companyID); } else { folderPath = folderPath + "/images/" + companyID; } string originalFolderPath = Path.Combine(folderPath, DateTime.Now.ToString("yyyyMM") + "/original"); string thumbnailFolderPath = Path.Combine(folderPath, DateTime.Now.ToString("yyyyMM") + "/thumbnail"); bool folderExists = Directory.Exists(originalFolderPath); if (!folderExists) { Directory.CreateDirectory(originalFolderPath); } folderExists = Directory.Exists(thumbnailFolderPath); if (!folderExists) { Directory.CreateDirectory(thumbnailFolderPath); } HttpPostedFile httpPostedFile = HttpContext.Current.Request.Files["file"]; string fileType = httpPostedFile.FileName.Substring(httpPostedFile.FileName.LastIndexOf(".")); string fileName = DateTime.Now.ToString("ddHHmmssff"); string fileOriginalSavePath = Path.Combine(originalFolderPath, fileName + fileType); try { httpPostedFile.SaveAs(fileOriginalSavePath); } catch (ApplicationException ex) { return(errorMsg(ex.Message)); } if (!convertToThumbnail) { return(successfulMsg(httpPostedFile.FileName, fileName, httpPostedFile.ContentLength, fileType, fileOriginalSavePath, WebApiApplication.UploadFolderVirualPath + "/images/" + companyID + "/" + DateTime.Now.ToString("yyyyMM") + "/original/" + fileName + fileType)); } try { Image originalImage = StreamHelper.ImagePath2Img(fileOriginalSavePath); string fileThumbnailSavePath = Path.Combine(thumbnailFolderPath, fileName + fileType); Image newImage = ImageHelper.GetThumbNailImage(originalImage, 160, 160); newImage.Save(fileThumbnailSavePath); return(successfulMsg(httpPostedFile.FileName, fileName, httpPostedFile.ContentLength, fileType, fileOriginalSavePath, WebApiApplication.UploadFolderVirualPath + "/images/" + companyID + "/" + DateTime.Now.ToString("yyyyMM") + "/thumbnail/" + fileName + fileType)); } catch (ApplicationException ex) { return(errorMsg(ex.Message)); } } return(errorMsg("没有上传文件")); }
private UploadMessage _uploadFile(string companyID, string fileFolderName) { if (HttpContext.Current.Request.Files.AllKeys.Any()) { string fileSubFolder = "/files/" + fileFolderName + "/"; FileExtension[] allowFileExtension = { FileExtension.BMP, FileExtension.DOC, FileExtension.DOCX, FileExtension.XLS,FileExtension.XLSX, FileExtension.PDF, FileExtension.GIF, FileExtension.JPG,FileExtension.PNG }; if (!FileValidation.IsAllowedExtension(HttpContext.Current.Request.Files["file"], allowFileExtension)) { return(errorMsg("不接受的文件格式")); } bool isImage = FileValidation.IsImageExtension(HttpContext.Current.Request.Files["file"]); // DEFINE THE PATH WHERE WE WANT TO SAVE THE FILES. string folderPath = WebApiApplication.UploadFolderPath; if (folderPath.StartsWith("~") || folderPath.StartsWith(".")) //相对路径 { folderPath = HttpContext.Current.Server.MapPath(folderPath + "/files/" + companyID); } else { folderPath = folderPath + fileSubFolder + companyID; } string yearMonth = DateTime.Now.ToString("yyyyMM"); folderPath = Path.Combine(folderPath, yearMonth); //folderPath = folderPath.Replace(".WebAPI", ".Web").Replace("UploadedDocuments", "content/images"); bool folderExists = Directory.Exists(folderPath); if (!folderExists) { Directory.CreateDirectory(folderPath); } HttpPostedFile httpPostedFile = HttpContext.Current.Request.Files["file"]; string fileType = httpPostedFile.FileName.Substring(httpPostedFile.FileName.LastIndexOf(".")); string fileName = Guid.NewGuid() + "-" + DateTime.Now.ToString("ddHHmmssff"); string fileSavePath = Path.Combine(folderPath, fileName + fileType); try { httpPostedFile.SaveAs(fileSavePath); } catch (ApplicationException ex) { return(errorMsg(ex.Message)); } if (isImage) { string originalFolderPath = folderPath; string thumbnailFolderPath = Path.Combine(folderPath, "thumbnail"); Image originalImage = StreamHelper.ImagePath2Img(fileSavePath); string fileThumbnailSavePath = Path.Combine(thumbnailFolderPath, fileName + fileType); folderExists = Directory.Exists(thumbnailFolderPath); if (!folderExists) { Directory.CreateDirectory(thumbnailFolderPath); } Image newImage = ImageHelper.GetThumbNailImage(originalImage, 320, 320); newImage.Save(fileThumbnailSavePath); UploadMessage uploadMessageImg = new UploadMessage() { OriginName = httpPostedFile.FileName, FileName = fileName, FileSize = httpPostedFile.ContentLength, FileType = fileType, DiskUploadPath = fileThumbnailSavePath, UploadFilePath = WebApiApplication.UploadFolderVirualPath + fileSubFolder + companyID + "/" + yearMonth + "/" + "/thumbnail/" + fileName + fileType, IsImage = true, HasThumbnail = true, OriginDiskUploadPath = fileSavePath, OriginFilePath = WebApiApplication.UploadFolderVirualPath + fileSubFolder + companyID + "/" + yearMonth + "/" + fileName + fileType, }; return(uploadMessageImg); } UploadMessage uploadMessage = new UploadMessage() { OriginName = httpPostedFile.FileName, FileName = fileName, FileSize = httpPostedFile.ContentLength, FileType = fileType, DiskUploadPath = fileSavePath, UploadFilePath = WebApiApplication.UploadFolderVirualPath + fileSubFolder + companyID + "/" + yearMonth + "/" + fileName + fileType, IsImage = false, HasThumbnail = false, OriginDiskUploadPath = fileSavePath, OriginFilePath = WebApiApplication.UploadFolderVirualPath + fileSubFolder + companyID + "/" + yearMonth + "/" + fileName + fileType, }; return(uploadMessage); } return(errorMsg("没有上传文件")); }
protected void Page_Load(object sender, EventArgs e) { string Act = HTTPRequest.GetString("Act"); int ReCode = 0; int CertificateID = HTTPRequest.GetInt("CertificateID", 0); string uCode_tem = HTTPRequest.GetString("c"); if (uCode_tem.Trim() != "") { //username|pwd uCode_tem = DES.Decode(uCode_tem, config.Passwordkey); if (uCode_tem.Trim() != "") { string[] uCode_temArr = Utils.SplitString(uCode_tem, "|"); if (uCode_temArr != null) { this.userid = tbUserInfo.CheckPassword(uCode_temArr[0], uCode_temArr[1], false); } } } if (this.userid > 0) { if (Page.Request.InputStream != null) { string PathStr = "/ufile/" + DateTime.Now.Year + "-" + DateTime.Now.Month; string PicData = HTTPRequest.GetString("PicData"); string filestr; int picsize = 0; int picID = 0; string sCode = Utils.GetRanDomCode(); string _PathStr = HttpContext.Current.Server.MapPath("/") + PathStr; FileExtension[] fe = { FileExtension.GIF, FileExtension.JPG, FileExtension.PNG, FileExtension.BMP }; if (!Directory.Exists(_PathStr)) { Directory.CreateDirectory(_PathStr); } filestr = _PathStr + "/" + sCode + ".jpg"; try { if (FileValidation.IsAllowedExtension(new MemoryStream(Convert.FromBase64String(PicData)), fe)) { Thumbnail.SaveBmp(Thumbnail.BytesToImage(Convert.FromBase64String(PicData)), filestr); FileStream stream = new FileStream(filestr, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); try { picsize = int.Parse(stream.Length.ToString()); } finally { stream.Close(); stream.Dispose(); } CertificatePicInfo cpi = new CertificatePicInfo(); try { cpi.UserID = userid; cpi.cCode = Utils.GetRanDomCode(); cpi.CertificateID = CertificateID; cpi.cPic = PathStr + "/" + sCode + ".jpg";; cpi.cAppendTime = DateTime.Now; picID = Certificates.AddCertificatePicInfo(cpi); if (picID > 0) { eStr = "<re picid=\"" + picID.ToString() + "\" />"; ReCode = 0; } } finally { cpi = null; } } else { eStr = "<error>数据错误</error>"; ReCode = 4; } } catch { try { HttpFileCollection files = HttpContext.Current.Request.Files; try { if (files.Count > 0) { HttpPostedFile postedFile = files[0]; int upLength = postedFile.ContentLength; byte[] upArray = new Byte[upLength]; Stream upStream = postedFile.InputStream; upStream.Read(upArray, 0, upLength); if (FileValidation.IsAllowedExtension(new MemoryStream(upArray), fe)) { Thumbnail.SaveBmp(Thumbnail.BytesToImage(upArray), filestr); FileStream stream = new FileStream(filestr, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); try { picsize = int.Parse(stream.Length.ToString()); } finally { stream.Close(); stream.Dispose(); } CertificatePicInfo cpi = new CertificatePicInfo(); try { cpi.UserID = userid; cpi.cCode = Utils.GetRanDomCode(); cpi.CertificateID = CertificateID; cpi.cPic = PathStr + "/" + sCode + ".jpg";; cpi.cAppendTime = DateTime.Now; picID = Certificates.AddCertificatePicInfo(cpi); if (picID > 0) { eStr = "<re picid=\"" + picID.ToString() + "\" />"; ReCode = 0; } } finally { cpi = null; } } } } finally { files = null; } } catch (Exception ex) { eStr = "<error>" + ex.Message.ToString() + "</error>"; ReCode = 3; } } } else { eStr = "<error>数据校验失败</error>"; ReCode = 4; } } else { eStr = "<error>鉴权失败</error>"; ReCode = 4; } ReMsg(ReCode); }