private string UploadFile(HttpPostedFile myFile) { var fileUrl = string.Empty; if (myFile != null && !string.IsNullOrEmpty(myFile.FileName)) { var filePath = myFile.FileName; try { var fileExtName = PathUtils.GetExtension(filePath); var localDirectoryPath = PathUtility.GetUploadDirectoryPath(PublishmentSystemInfo, fileExtName); var localFileName = PathUtility.GetUploadFileName(PublishmentSystemInfo, filePath); var localFilePath = PathUtils.Combine(localDirectoryPath, localFileName); if (!PathUtility.IsFileExtenstionAllowed(PublishmentSystemInfo, fileExtName)) { return(string.Empty); } if (!PathUtility.IsFileSizeAllowed(PublishmentSystemInfo, myFile.ContentLength)) { return(string.Empty); } myFile.SaveAs(localFilePath); FileUtility.AddWaterMark(PublishmentSystemInfo, localFilePath); fileUrl = PageUtility.GetPublishmentSystemUrlByPhysicalPath(PublishmentSystemInfo, localFilePath); fileUrl = PageUtility.GetVirtualUrl(PublishmentSystemInfo, fileUrl); } catch { } } return(fileUrl); }
public bool UploadContentPhotoImage(out string message, out string url, out string smallUrl, out string middleUrl, out string largeUrl) { message = url = smallUrl = middleUrl = largeUrl = string.Empty; if (Request.Files["ImageUrl"] == null) { return(false); } var postedFile = Request.Files["ImageUrl"]; try { var fileName = PathUtility.GetUploadFileName(PublishmentSystemInfo, postedFile.FileName); var fileExtName = PathUtils.GetExtension(fileName).ToLower(); var directoryPath = PathUtility.GetUploadDirectoryPath(PublishmentSystemInfo, fileExtName); var fileNameSmall = "small_" + fileName; var fileNameMiddle = "middle_" + fileName; var filePath = PathUtils.Combine(directoryPath, fileName); var filePathSamll = PathUtils.Combine(directoryPath, fileNameSmall); var filePathMiddle = PathUtils.Combine(directoryPath, fileNameMiddle); if (EFileSystemTypeUtils.IsImageOrFlashOrPlayer(fileExtName)) { if (!PathUtility.IsImageSizeAllowed(PublishmentSystemInfo, postedFile.ContentLength)) { message = "上传失败,上传图片超出规定文件大小!"; return(false); } postedFile.SaveAs(filePath); FileUtility.AddWaterMark(PublishmentSystemInfo, filePath); var widthSmall = PublishmentSystemInfo.Additional.PhotoSmallWidth; var heightSamll = PublishmentSystemInfo.Additional.PhotoSmallHeight; ImageUtils.MakeThumbnail(filePath, filePathSamll, widthSmall, heightSamll, true); var widthMiddle = PublishmentSystemInfo.Additional.PhotoMiddleWidth; var heightMiddle = PublishmentSystemInfo.Additional.PhotoMiddleHeight; ImageUtils.MakeThumbnail(filePath, filePathMiddle, widthMiddle, heightMiddle, true); url = PageUtility.GetPublishmentSystemUrlByPhysicalPath(PublishmentSystemInfo, filePathSamll); smallUrl = PageUtility.GetVirtualUrl(PublishmentSystemInfo, url); middleUrl = PageUtility.GetVirtualUrl(PublishmentSystemInfo, PageUtility.GetPublishmentSystemUrlByPhysicalPath(PublishmentSystemInfo, filePathMiddle)); largeUrl = PageUtility.GetVirtualUrl(PublishmentSystemInfo, PageUtility.GetPublishmentSystemUrlByPhysicalPath(PublishmentSystemInfo, filePath)); return(true); } message = "您必须上传图片文件!"; } catch (Exception ex) { message = ex.Message; } return(false); }
public bool UploadContentPhotoSwfUpload(out string message, out string url, out string smallUrl, out string largeUrl) { message = url = smallUrl = largeUrl = string.Empty; if (Request.Files != null && Request.Files["Filedata"] != null) { var postedFile = Request.Files["Filedata"]; try { var fileName = PathUtility.GetUploadFileName(PublishmentSystemInfo, postedFile.FileName); var fileExtName = PathUtils.GetExtension(fileName).ToLower(); var directoryPath = PathUtility.GetUploadDirectoryPath(PublishmentSystemInfo, fileExtName); var fileNameSmall = "small_" + fileName; var filePath = PathUtils.Combine(directoryPath, fileName); var filePathSmall = PathUtils.Combine(directoryPath, fileNameSmall); if (EFileSystemTypeUtils.IsImageOrFlashOrPlayer(fileExtName)) { if (!PathUtility.IsImageSizeAllowed(PublishmentSystemInfo, postedFile.ContentLength)) { FailMessage("上传失败,上传图片超出规定文件大小!"); return(false); } postedFile.SaveAs(filePath); FileUtility.AddWaterMark(PublishmentSystemInfo, filePath); var widthSmall = 200; ImageUtils.MakeThumbnail(filePath, filePathSmall, widthSmall, 0, true); url = PageUtility.GetPublishmentSystemUrlByPhysicalPath(PublishmentSystemInfo, filePathSmall); smallUrl = PageUtility.GetVirtualUrl(PublishmentSystemInfo, url); largeUrl = PageUtility.GetVirtualUrl(PublishmentSystemInfo, PageUtility.GetPublishmentSystemUrlByPhysicalPath(PublishmentSystemInfo, filePath)); return(true); } else { message = "您必须上传图片文件!"; } } catch (Exception ex) { message = ex.Message; } } return(false); }
public void AddWaterMark(int siteId, string filePath) { var siteInfo = SiteManager.GetSiteInfo(siteId); FileUtility.AddWaterMark(siteInfo, filePath); }
public override void Submit_OnClick(object sender, EventArgs e) { if (CbIsTitleImage.Checked && string.IsNullOrEmpty(TbTitleImageWidth.Text) && string.IsNullOrEmpty(TbTitleImageHeight.Text)) { FailMessage("缩略图尺寸不能为空!"); return; } if (CbIsSmallImage.Checked && string.IsNullOrEmpty(TbSmallImageWidth.Text) && string.IsNullOrEmpty(TbSmallImageHeight.Text)) { FailMessage("缩略图尺寸不能为空!"); return; } ConfigSettings(false); if (HifUpload.PostedFile == null || "" == HifUpload.PostedFile.FileName) { return; } var filePath = HifUpload.PostedFile.FileName; try { var fileExtName = PathUtils.GetExtension(filePath).ToLower(); var localDirectoryPath = PathUtility.GetUploadDirectoryPath(SiteInfo, fileExtName); var localFileName = PathUtility.GetUploadFileName(SiteInfo, filePath); var localTitleFileName = Constants.TitleImageAppendix + localFileName; var localSmallFileName = Constants.SmallImageAppendix + localFileName; var localFilePath = PathUtils.Combine(localDirectoryPath, localFileName); var localTitleFilePath = PathUtils.Combine(localDirectoryPath, localTitleFileName); var localSmallFilePath = PathUtils.Combine(localDirectoryPath, localSmallFileName); if (!PathUtility.IsImageExtenstionAllowed(SiteInfo, fileExtName)) { FailMessage("上传失败,上传图片格式不正确!"); return; } if (!PathUtility.IsImageSizeAllowed(SiteInfo, HifUpload.PostedFile.ContentLength)) { FailMessage("上传失败,上传图片超出规定文件大小!"); return; } HifUpload.PostedFile.SaveAs(localFilePath); var isImage = EFileSystemTypeUtils.IsImage(fileExtName); //处理上半部分 if (isImage) { FileUtility.AddWaterMark(SiteInfo, localFilePath); if (CbIsTitleImage.Checked) { var width = TranslateUtils.ToInt(TbTitleImageWidth.Text); var height = TranslateUtils.ToInt(TbTitleImageHeight.Text); ImageUtils.MakeThumbnail(localFilePath, localTitleFilePath, width, height, true); } } var imageUrl = PageUtility.GetSiteUrlByPhysicalPath(SiteInfo, localFilePath, true); if (CbIsTitleImage.Checked) { imageUrl = PageUtility.GetSiteUrlByPhysicalPath(SiteInfo, localTitleFilePath, true); } var textBoxUrl = PageUtility.GetVirtualUrl(SiteInfo, imageUrl); var script = $@" if (parent.document.getElementById('{_textBoxClientId}')) {{ parent.document.getElementById('{_textBoxClientId}').value = '{textBoxUrl}'; }} "; //处理下半部分 if (CbIsShowImageInTextEditor.Checked && isImage) { imageUrl = PageUtility.GetSiteUrlByPhysicalPath(SiteInfo, localFilePath, true); var smallImageUrl = imageUrl; if (CbIsSmallImage.Checked) { smallImageUrl = PageUtility.GetSiteUrlByPhysicalPath(SiteInfo, localSmallFilePath, true); } if (CbIsSmallImage.Checked) { var width = TranslateUtils.ToInt(TbSmallImageWidth.Text); var height = TranslateUtils.ToInt(TbSmallImageHeight.Text); ImageUtils.MakeThumbnail(localFilePath, localSmallFilePath, width, height, true); } var insertHtml = CbIsLinkToOriginal.Checked ? $@"<a href=""{imageUrl}"" target=""_blank""><img src=""{smallImageUrl}"" border=""0"" /></a>" : $@"<img src=""{smallImageUrl}"" border=""0"" />"; script += "if(parent." + UEditorUtils.GetEditorInstanceScript() + ") parent." + UEditorUtils.GetInsertHtmlScript("Content", insertHtml); } LtlScript.Text = $@" <script type=""text/javascript"" language=""javascript""> {script} {LayerUtils.CloseScript} </script>"; } catch (Exception ex) { FailMessage(ex, ex.Message); } }
public override void Process() { byte[] uploadFileBytes = null; string uploadFileName = null; if (UploadConfig.Base64) { uploadFileName = UploadConfig.Base64Filename; uploadFileBytes = Convert.FromBase64String(Request[UploadConfig.UploadFieldName]); } else { var file = Request.Files[UploadConfig.UploadFieldName]; uploadFileName = file.FileName; if (!CheckFileType(uploadFileName)) { Result.State = UploadState.TypeNotAllow; WriteResult(); return; } if (!CheckFileSize(file.ContentLength)) { Result.State = UploadState.SizeLimitExceed; WriteResult(); return; } uploadFileBytes = new byte[file.ContentLength]; try { file.InputStream.Read(uploadFileBytes, 0, file.ContentLength); } catch (Exception) { Result.State = UploadState.NetworkError; WriteResult(); } } Result.OriginFileName = uploadFileName; //var savePath = PathFormatter.Format(uploadFileName, UploadConfig.PathFormat); //var localPath = Server.MapPath(savePath); var currentType = PathUtils.GetExtension(Result.OriginFileName); var siteInfo = SiteManager.GetSiteInfo(SiteId); var localDirectoryPath = PathUtility.GetUploadDirectoryPath(siteInfo, UploadType); var localFileName = PathUtility.GetUploadFileName(siteInfo, uploadFileName); var localFilePath = PathUtils.Combine(localDirectoryPath, localFileName); try { //格式验证 if (!PathUtility.IsUploadExtenstionAllowed(UploadType, siteInfo, currentType)) { Result.State = UploadState.FileAccessError; Result.ErrorMessage = "不允许的文件类型"; } else { if (!Directory.Exists(Path.GetDirectoryName(localFilePath))) { Directory.CreateDirectory(Path.GetDirectoryName(localFilePath)); } File.WriteAllBytes(localFilePath, uploadFileBytes); if (UploadType == EUploadType.Image) { //添加水印 FileUtility.AddWaterMark(siteInfo, localFilePath); } Result.Url = PageUtility.GetSiteUrlByPhysicalPath(siteInfo, localFilePath, true); Result.State = UploadState.Success; } } catch (Exception e) { Result.State = UploadState.FileAccessError; Result.ErrorMessage = e.Message; } finally { WriteResult(); } }
public override void Submit_OnClick(object sender, EventArgs e) { if (HifUpload.PostedFile == null || "" == HifUpload.PostedFile.FileName) { return; } var filePath = HifUpload.PostedFile.FileName; try { var fileExtName = PathUtils.GetExtension(filePath).ToLower(); var localDirectoryPath = PathUtility.GetUploadDirectoryPath(SiteInfo, fileExtName); if (!string.IsNullOrEmpty(_currentRootPath)) { localDirectoryPath = PathUtility.MapPath(SiteInfo, _currentRootPath); DirectoryUtils.CreateDirectoryIfNotExists(localDirectoryPath); } var localFileName = PathUtility.GetUploadFileName(SiteInfo, filePath); var localFilePath = PathUtils.Combine(localDirectoryPath, localFileName); if (!PathUtility.IsImageExtenstionAllowed(SiteInfo, fileExtName)) { FailMessage("上传失败,上传图片格式不正确!"); return; } if (!PathUtility.IsImageSizeAllowed(SiteInfo, HifUpload.PostedFile.ContentLength)) { FailMessage("上传失败,上传图片超出规定文件大小!"); return; } HifUpload.PostedFile.SaveAs(localFilePath); var isImage = EFileSystemTypeUtils.IsImage(fileExtName); if (isImage && _isNeedWaterMark) { FileUtility.AddWaterMark(SiteInfo, localFilePath); } if (string.IsNullOrEmpty(_textBoxClientId)) { LayerUtils.Close(Page); } else { var imageUrl = PageUtility.GetSiteUrlByPhysicalPath(SiteInfo, localFilePath, true); var textBoxUrl = PageUtility.GetVirtualUrl(SiteInfo, imageUrl); LtlScript.Text = $@" <script type=""text/javascript"" language=""javascript""> if (parent.document.getElementById('{_textBoxClientId}') != null) {{ parent.document.getElementById('{_textBoxClientId}').value = '{textBoxUrl}'; }} {LayerUtils.CloseScript} </script>"; } } catch (Exception ex) { FailMessage(ex, "图片上传失败!"); } }
public IHttpActionResult Upload() { try { var request = new RequestImpl(); var siteId = request.GetQueryInt("siteId"); var channelId = request.GetQueryInt("channelId"); if (!request.IsUserLoggin || !request.UserPermissionsImpl.HasChannelPermissions(siteId, channelId, ConfigManager.ChannelPermissions.ContentAdd)) { return(Unauthorized()); } var siteInfo = SiteManager.GetSiteInfo(siteId); if (siteInfo == null) { return(BadRequest("无法确定内容对应的站点")); } var path = string.Empty; var url = string.Empty; var contentLength = 0; if (request.HttpRequest.Files.Count > 0) { var file = request.HttpRequest.Files[0]; var filePath = file.FileName; var fileExtName = PathUtils.GetExtension(filePath).ToLower(); var localDirectoryPath = PathUtility.GetUploadDirectoryPath(siteInfo, fileExtName); var localFileName = PathUtility.GetUploadFileName(siteInfo, filePath); path = PathUtils.Combine(localDirectoryPath, localFileName); contentLength = file.ContentLength; if (!PathUtility.IsImageExtenstionAllowed(siteInfo, fileExtName)) { return(BadRequest("上传失败,上传图片格式不正确!")); } if (!PathUtility.IsImageSizeAllowed(siteInfo, contentLength)) { return(BadRequest("上传失败,上传图片超出规定文件大小!")); } file.SaveAs(path); FileUtility.AddWaterMark(siteInfo, path); url = PageUtility.GetSiteUrlByPhysicalPath(siteInfo, path, true); } return(Ok(new { Path = path, Url = url, ContentLength = contentLength })); } catch (Exception ex) { LogUtils.AddErrorLog(ex); return(InternalServerError(ex)); } }
public override void Submit_OnClick(object sender, EventArgs e) { if (HifUpload.PostedFile == null || "" == HifUpload.PostedFile.FileName) { return; } var filePath = HifUpload.PostedFile.FileName; try { var fileExtName = PathUtils.GetExtension(filePath).ToLower(); var localDirectoryPath = PathUtility.GetUploadDirectoryPath(SiteInfo, fileExtName); if (!string.IsNullOrEmpty(_realtedPath)) { localDirectoryPath = PathUtility.MapPath(SiteInfo, _realtedPath); DirectoryUtils.CreateDirectoryIfNotExists(localDirectoryPath); } var localFileName = PathUtility.GetUploadFileName(SiteInfo, filePath, TranslateUtils.ToBool(DdlIsFileUploadChangeFileName.SelectedValue)); var localFilePath = PathUtils.Combine(localDirectoryPath, localFileName); if (_uploadType == EUploadType.Image && !EFileSystemTypeUtils.IsImageOrFlashOrPlayer(fileExtName)) { FailMessage("此格式不允许上传,此文件夹只允许上传图片以及音视频文件!"); return; } if (_uploadType == EUploadType.Video && !EFileSystemTypeUtils.IsImageOrFlashOrPlayer(fileExtName)) { FailMessage("此格式不允许上传,此文件夹只允许上传图片以及音视频文件!"); return; } if (_uploadType == EUploadType.File && !PathUtility.IsFileExtenstionAllowed(SiteInfo, fileExtName)) { FailMessage("此格式不允许上传,请选择有效的文件!"); return; } if (!PathUtility.IsFileSizeAllowed(SiteInfo, HifUpload.PostedFile.ContentLength)) { FailMessage("上传失败,上传文件超出规定文件大小!"); return; } HifUpload.PostedFile.SaveAs(localFilePath); FileUtility.AddWaterMark(SiteInfo, localFilePath); var fileUrl = PageUtility.GetSiteUrlByPhysicalPath(SiteInfo, localFilePath, true); var textBoxUrl = PageUtility.GetVirtualUrl(SiteInfo, fileUrl); if (string.IsNullOrEmpty(_textBoxClientId)) { LayerUtils.Close(Page); } else { LtlScript.Text = $@" <script type=""text/javascript"" language=""javascript""> if (parent.document.getElementById('{_textBoxClientId}') != null) {{ parent.document.getElementById('{_textBoxClientId}').value = '{textBoxUrl}'; }} {LayerUtils.CloseScript} </script>"; } } catch (Exception ex) { FailMessage(ex, "文件上传失败"); } }
public override void Submit_OnClick(object sender, EventArgs e) { if (hifUpload.PostedFile != null && "" != hifUpload.PostedFile.FileName) { var filePath = hifUpload.PostedFile.FileName; try { var fileExtName = PathUtils.GetExtension(filePath).ToLower(); var localDirectoryPath = PathUtility.GetUploadDirectoryPath(PublishmentSystemInfo, fileExtName); if (!string.IsNullOrEmpty(_realtedPath)) { localDirectoryPath = PathUtility.MapPath(PublishmentSystemInfo, _realtedPath); DirectoryUtils.CreateDirectoryIfNotExists(localDirectoryPath); } var localFileName = PathUtility.GetUploadFileName(PublishmentSystemInfo, filePath, DateTime.Now, TranslateUtils.ToBool(rblIsFileUploadChangeFileName.SelectedValue)); var localFilePath = PathUtils.Combine(localDirectoryPath, localFileName); if (_uploadType == EUploadType.Image && !EFileSystemTypeUtils.IsImageOrFlashOrPlayer(fileExtName)) { FailMessage("此格式不允许上传,此文件夹只允许上传图片以及音视频文件!"); return; } if (_uploadType == EUploadType.Video && !EFileSystemTypeUtils.IsImageOrFlashOrPlayer(fileExtName)) { FailMessage("此格式不允许上传,此文件夹只允许上传图片以及音视频文件!"); return; } if (_uploadType == EUploadType.File && !PathUtility.IsFileExtenstionAllowed(PublishmentSystemInfo, fileExtName)) { FailMessage("此格式不允许上传,请选择有效的文件!"); return; } if (!PathUtility.IsFileSizeAllowed(PublishmentSystemInfo, hifUpload.PostedFile.ContentLength)) { FailMessage("上传失败,上传文件超出规定文件大小!"); return; } hifUpload.PostedFile.SaveAs(localFilePath); FileUtility.AddWaterMark(PublishmentSystemInfo, localFilePath); var fileUrl = PageUtility.GetPublishmentSystemUrlByPhysicalPath(PublishmentSystemInfo, localFilePath); var textBoxUrl = PageUtility.GetVirtualUrl(PublishmentSystemInfo, fileUrl); if (string.IsNullOrEmpty(_textBoxClientId)) { PageUtils.CloseModalPage(Page); } else { ltlScript.Text += $@" if (parent.document.getElementById('{_textBoxClientId}') != null) {{ parent.document.getElementById('{_textBoxClientId}').value = '{textBoxUrl}'; }} "; ltlScript.Text += PageUtils.HidePopWin; } } catch (Exception ex) { FailMessage(ex, "文件上传失败"); } } }
public override void Submit_OnClick(object sender, EventArgs e) { if (cbIsTitleImage.Checked && string.IsNullOrEmpty(tbTitleImageWidth.Text) && string.IsNullOrEmpty(tbTitleImageHeight.Text)) { FailMessage("缩略图尺寸不能为空!"); return; } if (cbIsSmallImage.Checked && string.IsNullOrEmpty(tbSmallImageWidth.Text) && string.IsNullOrEmpty(tbSmallImageHeight.Text)) { FailMessage("缩略图尺寸不能为空!"); return; } ConfigSettings(false); if (hifUpload.PostedFile != null && "" != hifUpload.PostedFile.FileName) { var filePath = hifUpload.PostedFile.FileName; try { var fileExtName = PathUtils.GetExtension(filePath).ToLower(); var localDirectoryPath = PathUtility.GetUploadDirectoryPath(PublishmentSystemInfo, fileExtName); var localFileName = PathUtility.GetUploadFileName(PublishmentSystemInfo, filePath); var localTitleFileName = StringUtils.Constants.TitleImageAppendix + localFileName; var localSmallFileName = StringUtils.Constants.SmallImageAppendix + localFileName; var localFilePath = PathUtils.Combine(localDirectoryPath, localFileName); var localTitleFilePath = PathUtils.Combine(localDirectoryPath, localTitleFileName); var localSmallFilePath = PathUtils.Combine(localDirectoryPath, localSmallFileName); if (!PathUtility.IsImageExtenstionAllowed(PublishmentSystemInfo, fileExtName)) { FailMessage("上传失败,上传图片格式不正确!"); return; } if (!PathUtility.IsImageSizeAllowed(PublishmentSystemInfo, hifUpload.PostedFile.ContentLength)) { FailMessage("上传失败,上传图片超出规定文件大小!"); return; } hifUpload.PostedFile.SaveAs(localFilePath); var isImage = EFileSystemTypeUtils.IsImage(fileExtName); //处理上半部分 if (isImage) { FileUtility.AddWaterMark(PublishmentSystemInfo, localFilePath); if (cbIsTitleImage.Checked) { var width = TranslateUtils.ToInt(tbTitleImageWidth.Text); var height = TranslateUtils.ToInt(tbTitleImageHeight.Text); ImageUtils.MakeThumbnail(localFilePath, localTitleFilePath, width, height, cbIsTitleImageLessSizeNotThumb.Checked); } } var imageUrl = PageUtility.GetPublishmentSystemUrlByPhysicalPath(PublishmentSystemInfo, localFilePath); if (cbIsTitleImage.Checked) { imageUrl = PageUtility.GetPublishmentSystemUrlByPhysicalPath(PublishmentSystemInfo, localTitleFilePath); } var textBoxUrl = PageUtility.GetVirtualUrl(PublishmentSystemInfo, imageUrl); ltlScript.Text += $@" if (parent.document.getElementById('{_textBoxClientId}')) {{ parent.document.getElementById('{_textBoxClientId}').value = '{textBoxUrl}'; }} "; //处理下半部分 if (cbIsShowImageInTextEditor.Checked && isImage) { imageUrl = PageUtility.GetPublishmentSystemUrlByPhysicalPath(PublishmentSystemInfo, localFilePath); var smallImageUrl = imageUrl; if (cbIsSmallImage.Checked) { smallImageUrl = PageUtility.GetPublishmentSystemUrlByPhysicalPath(PublishmentSystemInfo, localSmallFilePath); } if (cbIsSmallImage.Checked) { var width = TranslateUtils.ToInt(tbSmallImageWidth.Text); var height = TranslateUtils.ToInt(tbSmallImageHeight.Text); ImageUtils.MakeThumbnail(localFilePath, localSmallFilePath, width, height, cbIsSmallImageLessSizeNotThumb.Checked); } var insertHtml = string.Empty; if (cbIsLinkToOriginal.Checked) { insertHtml = $@"<a href=""{imageUrl}"" target=""_blank""><img src=""{smallImageUrl}"" border=""0"" /></a>"; } else { insertHtml = $@"<img src=""{smallImageUrl}"" border=""0"" />"; } ltlScript.Text += "if(parent." + ETextEditorTypeUtils.GetEditorInstanceScript() + ") parent." + ETextEditorTypeUtils.GetInsertHtmlScript("Content", insertHtml); } ltlScript.Text += PageUtils.HidePopWin; } catch (Exception ex) { FailMessage(ex, ex.Message); } } }
public HttpResponseMessage Main() { var body = new RequestBody(); var siteId = TranslateUtils.ToInt(body.GetQueryString("siteId")); var uploadType = EInputTypeUtils.GetEnumType(body.GetQueryString("uploadType")); var errorMessage = string.Empty; var fileUrls = new List <string>(); var publishmentSystemInfo = PublishmentSystemManager.GetPublishmentSystemInfo(siteId); if (body.IsUserLoggin && publishmentSystemInfo != null) { try { if (HttpContext.Current.Request.Files.Count > 0) { for (var i = 0; i < HttpContext.Current.Request.Files.Count; i++) { var postedFile = HttpContext.Current.Request.Files[i]; var filePath = postedFile.FileName; var fileExtName = PathUtils.GetExtension(filePath).ToLower(); var localDirectoryPath = PathUtility.GetUploadDirectoryPath(publishmentSystemInfo, fileExtName); var localFileName = PathUtility.GetUploadFileName(publishmentSystemInfo, filePath); var localFilePath = PathUtils.Combine(localDirectoryPath, localFileName); if (uploadType == EInputType.Image) { if (!PathUtility.IsImageExtenstionAllowed(publishmentSystemInfo, fileExtName)) { errorMessage = "上传图片格式不正确!"; break; } else if (!PathUtility.IsImageSizeAllowed(publishmentSystemInfo, postedFile.ContentLength)) { errorMessage = "上传失败,上传图片超出规定文件大小!"; break; } postedFile.SaveAs(localFilePath); FileUtility.AddWaterMark(publishmentSystemInfo, localFilePath); var imageUrl = PageUtility.GetPublishmentSystemUrlByPhysicalPath(publishmentSystemInfo, localFilePath); fileUrls.Add(imageUrl); } else if (uploadType == EInputType.Video) { if (!PathUtility.IsVideoExtenstionAllowed(publishmentSystemInfo, fileExtName)) { errorMessage = "上传视频格式不正确!"; break; } if (!PathUtility.IsVideoSizeAllowed(publishmentSystemInfo, postedFile.ContentLength)) { errorMessage = "上传失败,上传视频超出规定文件大小!"; break; } postedFile.SaveAs(localFilePath); var videoUrl = PageUtility.GetPublishmentSystemUrlByPhysicalPath(publishmentSystemInfo, localFilePath); fileUrls.Add(videoUrl); } else if (uploadType == EInputType.File) { if (!PathUtility.IsFileExtenstionAllowed(publishmentSystemInfo, fileExtName)) { errorMessage = "此格式不允许上传,请选择有效的文件!"; break; } if (!PathUtility.IsFileSizeAllowed(publishmentSystemInfo, postedFile.ContentLength)) { errorMessage = "上传失败,上传文件超出规定文件大小!"; break; } postedFile.SaveAs(localFilePath); FileUtility.AddWaterMark(publishmentSystemInfo, localFilePath); var fileUrl = PageUtility.GetPublishmentSystemUrlByPhysicalPath(publishmentSystemInfo, localFilePath); fileUrls.Add(fileUrl); } } } } catch (Exception ex) { //errorMessage = ex.Message; errorMessage = "程序错误"; } } var builder = new StringBuilder(); if (fileUrls.Count == 0) { builder.Append("{\"errorMessage\":\""); builder.Append(!string.IsNullOrEmpty(errorMessage) ? errorMessage : "未知错误"); builder.Append("\"}"); } else { builder.Append("{\"fileUrls\":["); foreach (var fileUrl in fileUrls) { builder.Append("\""); builder.Append(fileUrl); builder.Append("\","); } builder.Length--; builder.Append("]}"); } var resp = new HttpResponseMessage(); if (!string.IsNullOrEmpty(HttpContext.Current.Request.Headers.Get("X-Access-Token"))) { resp.Content = new StringContent(builder.ToString(), Encoding.UTF8, "application/json"); } else { resp.Content = new StringContent(builder.ToString(), Encoding.UTF8, "text/plain"); } return(resp); }
public override void Submit_OnClick(object sender, EventArgs e) { if (hifUpload.PostedFile != null && "" != hifUpload.PostedFile.FileName) { var filePath = hifUpload.PostedFile.FileName; try { var fileExtName = PathUtils.GetExtension(filePath).ToLower(); var localDirectoryPath = PathUtility.GetUploadDirectoryPath(PublishmentSystemInfo, fileExtName); if (!string.IsNullOrEmpty(_currentRootPath)) { localDirectoryPath = PathUtility.MapPath(PublishmentSystemInfo, _currentRootPath); DirectoryUtils.CreateDirectoryIfNotExists(localDirectoryPath); } var localFileName = PathUtility.GetUploadFileName(PublishmentSystemInfo, filePath); var localFilePath = PathUtils.Combine(localDirectoryPath, localFileName); if (!PathUtility.IsImageExtenstionAllowed(PublishmentSystemInfo, fileExtName)) { FailMessage("上传失败,上传图片格式不正确!"); return; } if (!PathUtility.IsImageSizeAllowed(PublishmentSystemInfo, hifUpload.PostedFile.ContentLength)) { FailMessage("上传失败,上传图片超出规定文件大小!"); return; } hifUpload.PostedFile.SaveAs(localFilePath); var isImage = EFileSystemTypeUtils.IsImage(fileExtName); if (isImage && _isNeedWaterMark) { FileUtility.AddWaterMark(PublishmentSystemInfo, localFilePath); } if (string.IsNullOrEmpty(_textBoxClientId)) { PageUtils.CloseModalPage(Page); } else { var imageUrl = PageUtility.GetPublishmentSystemUrlByPhysicalPath(PublishmentSystemInfo, localFilePath); var textBoxUrl = PageUtility.GetVirtualUrl(PublishmentSystemInfo, imageUrl); ltlScript.Text += $@" if (parent.document.getElementById('{_textBoxClientId}') != null) {{ parent.document.getElementById('{_textBoxClientId}').value = '{textBoxUrl}'; }} "; ltlScript.Text += PageUtils.HidePopWin; } } catch (Exception ex) { FailMessage(ex, "图片上传失败!"); } } }