public Hashtable upScrawl(HttpContext cxt, string base64Data) { FileStream fs = null; try { var fileExtension = ".png"; var localDirectoryPath = PathUtility.GetUploadDirectoryPath(publishmentSystemInfo, fileExtension); var fileName = Guid.NewGuid() + fileExtension; var localFilePath = PathUtils.Combine(localDirectoryPath, fileName); fs = File.Create(localFilePath); var bytes = Convert.FromBase64String(base64Data); fs.Write(bytes, 0, bytes.Length); URL = PageUtility.GetPublishmentSystemUrlByPhysicalPath(publishmentSystemInfo, localFilePath); } catch { state = "未知错误"; URL = ""; } finally { fs.Close(); } return(getUploadInfo()); }
public Hashtable upFile(HttpContext cxt) { try { uploadFile = cxt.Request.Files[0]; originalName = uploadFile.FileName; currentType = PathUtils.GetExtension(originalName); var localDirectoryPath = PathUtility.GetUploadDirectoryPath(publishmentSystemInfo, uploadType); var localFileName = PathUtility.GetUploadFileName(publishmentSystemInfo, originalName); var localFilePath = PathUtils.Combine(localDirectoryPath, localFileName); //格式验证 if (!PathUtility.IsUploadExtenstionAllowed(uploadType, publishmentSystemInfo, currentType)) { state = "不允许的文件类型"; } //大小验证 if (!PathUtility.IsUploadSizeAllowed(uploadType, publishmentSystemInfo, uploadFile.ContentLength)) { state = "文件大小超出网站限制"; } //保存图片 if (state == "SUCCESS") { uploadFile.SaveAs(localFilePath); URL = PageUtility.GetPublishmentSystemUrlByPhysicalPath(publishmentSystemInfo, localFilePath); //URL = pathbase + filename; if (uploadType == EUploadType.Image) { //添加水印 FileUtility.AddWaterMark(publishmentSystemInfo, localFilePath); } } } catch (Exception e) { state = e.Message; URL = ""; } return(getUploadInfo()); }
//将编辑器中图片上传至本机 public static string SaveImage(PublishmentSystemInfo publishmentSystemInfo, string content) { var originalImageSrcs = RegexUtils.GetOriginalImageSrcs(content); foreach (var originalImageSrc in originalImageSrcs) { if (!PageUtils.IsProtocolUrl(originalImageSrc) || StringUtils.StartsWithIgnoreCase(originalImageSrc, WebConfigUtils.ApplicationPath) || StringUtils.StartsWithIgnoreCase(originalImageSrc, publishmentSystemInfo.PublishmentSystemUrl)) { continue; } var fileExtName = PageUtils.GetExtensionFromUrl(originalImageSrc); if (!EFileSystemTypeUtils.IsImageOrFlashOrPlayer(fileExtName)) { continue; } var fileName = GetUploadFileName(publishmentSystemInfo, originalImageSrc); var directoryPath = GetUploadDirectoryPath(publishmentSystemInfo, fileExtName); var filePath = PathUtils.Combine(directoryPath, fileName); try { if (!FileUtils.IsFileExists(filePath)) { WebClientUtils.SaveRemoteFileToLocal(originalImageSrc, filePath); if (EFileSystemTypeUtils.IsImage(PathUtils.GetExtension(fileName))) { FileUtility.AddWaterMark(publishmentSystemInfo, filePath); } } var fileUrl = PageUtility.GetPublishmentSystemUrlByPhysicalPath(publishmentSystemInfo, filePath); content = content.Replace(originalImageSrc, fileUrl); } catch { // ignored } } return(content); }