private void UploadImage() { HttpResponse response = HttpContext.Current.Response; try { ImageProperty imageProperty = GetPostedImageProperty(); string opText = WebUtility.GetRequestFormString("clientOPHidden", string.Empty); string op = !string.IsNullOrEmpty(opText) ? opText.Split(',')[0] : ""; //WebUtility.GetRequestFormString("clientOPHidden", string.Empty).Split(',')[0]; string inputName = !string.IsNullOrEmpty(opText) ? opText.Split(',')[2] : ""; //WebUtility.GetRequestFormString("clientOPHidden", string.Empty).Split(',')[1]; HttpPostedFile file = GetPostFile(inputName); if (file != null) { if (this.FileMaxSize > 0 && file.ContentLength > this.FileMaxSize) { response.Write(GetResponseTextScript(string.Format("您上传的文件超过了{0}字节。", this.FileMaxSize))); response.Write(GetClientControlInvokeStript(GetPostedControlID(), "uploadFail", "document.getElementById('responseInfo').value", "")); return; } string filePath = string.Empty; ImageUploadHelper.UploadFile(this, file, imageProperty.OriginalName, imageProperty.NewName, out filePath); imageProperty.FilePath = filePath.Encrypt(); string imgPropJsonStr = JSONSerializerExecute.Serialize(imageProperty); string uploadImageShowenUrl = CurrentPageUrl + string.Format("?imagePropID={0}&filePath={1}", imageProperty.ID, filePath.Encrypt()); response.Write(GetResponseTextScript(imgPropJsonStr)); response.Write(GetUploadImageUrlByFile(uploadImageShowenUrl)); string paramsData = string.Format("['{0}','{1}']", "document.getElementById('responseInfo').value", uploadImageShowenUrl); response.Write(GetClientControlInvokeStript(GetPostedControlID(), "uploadSuccess", "document.getElementById('responseInfo').value", "document.getElementById('uploadImageUrlByFile').value")); } } catch (System.Exception ex) { response.Write(GetResponseTextScript(ex.Message)); response.Write(GetClientControlInvokeStript(GetPostedControlID(), "uploadFail", "document.getElementById('responseInfo').value", "")); } finally { response.End(); } }
private byte[] GetImageBytesFromFilePath(string fileName) { byte[] content = null; string filePath = ImageUploadHelper.GetUploadRootPath(this.RootPathName) + "Temp\\" + fileName; if (File.Exists(filePath)) { using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read)) { content = fs.ToBytes(); } } return(content); }
protected override void LoadClientState(string clientState) { object[] state = (object[])JSONSerializerExecute.DeserializeObject(clientState); object tempState; if (state[0] is object[]) { object[] tempResult = (object[])state[0]; tempState = tempResult[0]; } else { tempState = state[0]; } this.imgProp = JSONSerializerExecute.Deserialize <ImageProperty>(tempState); string postedControlID = GetPostedControlID(); if (postedControlID.IsNullOrEmpty()) { postedControlID = ((string)state[1]).Replace('_', '$'); } HttpPostedFile file = GetPostFile(postedControlID); if (file != null) { if (this.imgProp.ID.IsNullOrEmpty()) { this.imgProp.ID = UuidHelper.NewUuidString(); } this.imgProp.NewName = UuidHelper.NewUuidString() + Path.GetExtension(imgProp.OriginalName); this.imgProp.Changed = true; string filePath; ImageUploadHelper.UploadFile(this, file, this.imgProp.OriginalName, this.imgProp.NewName, out filePath); } }
private void UploadImage() { const int FileTooLargeError = -2147467259; ObjectContextCache.Instance["FileUploadProcessed"] = true; HttpRequest request = HttpContext.Current.Request; HttpResponse response = HttpContext.Current.Response; string lockID = WebUtility.GetRequestQueryValue <string>("lockID", string.Empty); string userID = WebUtility.GetRequestQueryValue <string>("userID", string.Empty); string fileName = WebUtility.GetRequestQueryValue <string>("fileName", string.Empty); int fileMaxSize = WebUtility.GetRequestQueryValue <int>("fileMaxSize", 0); string controlID = WebUtility.GetRequestQueryValue("controlID", string.Empty); ExceptionHelper.CheckStringIsNullOrEmpty(fileName, "fileName"); try { if (fileMaxSize > 0 && request.ContentLength > fileMaxSize) { GenerateErrorInformation(string.Format("文件超过了上传大小的限制{0}字节", fileMaxSize)); } else { //不检查锁,沈峥修改 //CheckLock(lockID, userID); string uploadPath = ImageUploadHelper.GetUploadRootPath(this.RootPathName); AutoCreateUploadPath(uploadPath); string newID = UuidHelper.NewUuidString(); fileName = newID + fileName.Substring(fileName.LastIndexOf('.')); if (WebUtility.GetRequestQueryString("upmethod", "") == "new") { var dialogControlID = WebUtility.GetRequestQueryValue("dialogControlID", string.Empty); request.Files[0].SaveAs(uploadPath + @"Temp\" + fileName); string output = "<script type='text/javascript'>"; output += "window.parent.$find('" + dialogControlID + "').onUploadFinish(1)"; output += "</script>"; response.Write(output); } else if (WebUtility.GetRequestQueryString("upmethod", "") == "ue") { request.Files[0].SaveAs(uploadPath + @"Temp\" + fileName); string output = "<script type='text/javascript'>"; output += string.Format("window.parent.onUploadFinish('{0}','{1}')", newID, fileName); output += "</script>"; response.Write(output); } else { request.SaveAs(uploadPath + @"Temp\" + fileName, false); } string fileIconPath = FileConfigHelper.GetFileIconPath(fileName); response.AppendHeader("fileIconPath", HttpUtility.UrlEncode("message=" + fileIconPath)); string dateTimeNow = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"); response.AppendHeader("lastUploadTag", "message=" + dateTimeNow); response.AppendHeader("newMaterialID", "message=" + HttpUtility.UrlEncode(newID)); } } catch (Exception ex) { string errorMessage = ex.Message; if (ex is ExternalException && ((ExternalException)(ex)).ErrorCode == FileTooLargeError && ex.Source == "System.Web") { errorMessage = "您上传的文件过大"; } GenerateErrorInformation(errorMessage); } finally { try { response.End(); } catch { } } }