private bool BeforeUpload(UploadFileItem file) { var isJpgOrPng = file.Type == "image/jpeg" || file.Type == "image/png"; if (!isJpgOrPng) { MessageService.Error("You can only upload JPG/PNG file!"); } var isLt2M = file.Size / 1024 / 1024 < 2; if (!isLt2M) { MessageService.Error("Image must smaller than 2MB!"); } return(isJpgOrPng && isLt2M); }
private static void UploadFileTaskProcess(UploadTask uploadTask) { ThreadPool.QueueUserWorkItem((o) => { try { UploadFileItem uploadFileItem = new UploadFileItem(uploadTask); uploadFileItem.Upload(); } catch (Exception ex) { System.Threading.Monitor.Enter(_UploadingIDs); if (_UploadingIDs.Contains(uploadTask.UniqueID)) { _UploadingIDs.Remove(uploadTask.UniqueID); } System.Threading.Monitor.Exit(_UploadingIDs); _logger.Error("UploadFile error:" + ex.ToString()); } }); }
public void Uploader(string token) { QueryDataCollection queryData = new QueryDataCollection(Request["queryData"]); HttpPostedFileBase file=null; if (Request.Files.Count > 0) { file = Request.Files[0]; } else if (Request.InputStream.Length > 0) { string fileName= Server.UrlDecode(Request.Headers["fileName"]); file = new HttpPostedFileFromRequest(fileName, Request.ContentType, Request.InputStream); } UploadFileItem uploadItem = new UploadFileItem(); //UserServiceClient userclient = new UserServiceClient(); token = token ?? CurrentToken; //var logined = userclient.CheckLogin(token); //userclient.Close(); //userclient = null; //if (!logined.Data) //{ // uploadItem.Saved = false; // uploadItem.Msg = "登录后方可上传文件!"; //} if (file != null) { string extention = file.FileName.Substring(file.FileName.LastIndexOf('.')).ToLower(); uploadItem.Saved = true; if (file.ContentLength > 1024 * 2 * 1024) { uploadItem.Saved = false; uploadItem.Msg = "请上传2M以下大小图片!"; } if (".jpg;.bmp;.gif;.png;.jpeg".IndexOf(extention) < 0) { uploadItem.Saved = false; uploadItem.Msg = "上传图片格式必须为.jpg;.bmp;.gif;.png;.jpeg!"; } System.Drawing.Image upImage = null; try { upImage = System.Drawing.Image.FromStream(file.InputStream); } catch (Exception ex) { uploadItem.Saved = false; uploadItem.Msg = "图片识别失败!" + ex.Message; } if (uploadItem.Saved) { try { MemoryStream ms = new MemoryStream(); byte[] fileByteArray = null; upImage.Save(ms, System.Drawing.Imaging.ImageFormat.Png); fileByteArray = ms.GetBuffer(); FileServiceClient client = new FileServiceClient(); AdvancedResult<ResPic> response = client.UploadPic(fileByteArray, upImage.Height, upImage.Width, token); //string type = Request["type"] ?? queryData["type"]; //switch (type) //{ // case "g": // response = client.UploadBBPic(fileByteArray, upImage.Height, upImage.Width, CurrentToken); // break; // case "h": // response = client.UploadUserImage(fileByteArray, upImage.Height, upImage.Width, CurrentToken); // break; // case "b": // response = client.UploadPic(fileByteArray, upImage.Height, upImage.Width, CurrentToken); // break; // default: // response = new AdvancedResult<ResPic>(); // response.Error = DataStructure.AppError.ERROR_FAILED; // response.ExMessage = "上传失败,不明确的图片使用类型!"; // break; //} if (response.Error == DataStructure.AppError.ERROR_SUCCESS) { uploadItem.PicId = response.Data.PicId; uploadItem.Pic = new PicModel().Bind(response.Data); int maxWidth = 0; int maxHeight = 0; string width = Request["width"] ?? queryData["width"] ?? ""; string height = Request["height"] ?? queryData["height"] ?? ""; if (int.TryParse(width, out maxWidth) && int.TryParse(height, out maxHeight)) { uploadItem.Pic.DisplaySeting(maxWidth, maxHeight); } } else { uploadItem.Saved = false; uploadItem.Msg = response.ExMessage ?? "保存图片失败!请稍后再试!"; } } catch (Exception ex) { uploadItem.Saved = false; uploadItem.Msg = "保存图片失败!" + ex.Message; } } } else { uploadItem.Saved = false; uploadItem.Msg = "上传文件为空!"; } Response.ContentType = "text/html; charset=utf-8"; Response.AppendHeader("Cache-Control", "no-cache"); Response.Write(JsonHelper.Serialize(uploadItem)); }
private UploadFileItem CreateUploadFileItem(string fileKey) { var item = new UploadFileItem(); var extension = Path.GetExtension(fileKey); if (extension != null) { var ext = extension.ToLower(); item.FileKey = fileKey; item.Name = Path.GetFileName(fileKey); item.FileType = (ext == ".jpg" || ext == ".gif" || ext == ".png" || ext == ".bmp") ? UploadFileType.Graphic : UploadFileType.Flash; } item.Url = _hostUrl + fileKey; return item; }