public void UpdateContent(ImageProperty image) { image.NullCheck("image"); IMaterialContentPersistManager manager = MaterialContentSettings.GetConfig().PersistManager; image.EnsureMaterialContent(); using (TransactionScope scope = TransactionScopeFactory.Create()) { if (image.Content.IsEmpty()) { manager.DeleteMaterialContent(image.Content); } else { manager.DestFileInfo = GetPhysicalDestinationImagePath(image.Content.FileName); manager.SourceFileInfo = image.Content.PhysicalSourceFilePath; manager.SaveMaterialContent(image.Content); image.FilePath = string.Empty; image.Changed = false; } scope.Complete(); } }
private void EnsureNotChangedMaterial() { if (this._Content.ContentData == null) { if (this.TempPhysicalFilePath.IsNotEmpty()) { FileInfo tempFile = new FileInfo(this.TempPhysicalFilePath); if (tempFile.Exists) { this._Content.ContentData = tempFile.OpenRead().ToBytes(); } } if (this._Content.ContentData == null) { IMaterialContentPersistManager manager = MaterialContentSettings.GetConfig().PersistManager; //在数据库存储附件的模式下,这个参数其实没用 manager.DestFileInfo = new FileInfo(GetTempPhysicalFilePath(string.Empty)); this._Content.ContentData = manager.GetMaterialContent(this._Content.ContentID).ToBytes(); } } }
private void EnsureChangedImage() { if (this.SourceImageID.IsNullOrEmpty()) { this._Content.PhysicalSourceFilePath = ImagePropertyAdapter.Instance.GetPhysicalUploadImagePath(this._Content.FileName); } else { IMaterialContentPersistManager manager = MaterialContentSettings.GetConfig().PersistManager; manager.DestFileInfo = ImagePropertyAdapter.Instance.GetPhysicalUploadImagePath(this._Content.FileName); if (!File.Exists(manager.DestFileInfo.DirectoryName)) { Directory.CreateDirectory(manager.DestFileInfo.DirectoryName); } using (Stream srcContent = manager.GetMaterialContent(this.SourceImageID)) { using (FileStream fs = new FileStream(manager.DestFileInfo.ToString(), FileMode.Create, FileAccess.Write)) { srcContent.CopyTo(fs); } this._Content.PhysicalSourceFilePath = manager.DestFileInfo; } } }
public void DoModifyFileOperations(string rootPathName, MaterialFileOeprationInfo fileOp, MaterialContent content) { string uploadRootPath = AppPathConfigSettings.GetConfig().Paths[rootPathName].Dir; ExceptionHelper.CheckStringIsNullOrEmpty(uploadRootPath, "uploadRootPath"); IMaterialContentPersistManager persistManager = GetMaterialContentPersistManager(rootPathName, fileOp); if (content == null) { content = fileOp.Material.GenerateMaterialContent(); } switch (fileOp.Operation) { case FileOperation.Add: case FileOperation.Update: persistManager.SaveMaterialContent(content); break; case FileOperation.Delete: persistManager.DeleteMaterialContent(content); break; default: break; } }
public static HttpResponseMessage ProcessMaterialDownload(this MaterialModel material, string rootPathName = MaterialUploadModel.DefaultRootPathName) { material.NullCheck("material"); HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK); FileInfo destFileInfo = null; if (rootPathName.IsNullOrEmpty()) { rootPathName = MaterialUploadModel.DefaultRootPathName; } if (material.Status == MaterualModelStatus.Inserted) { destFileInfo = GetTempUploadFilePath(material.ID, rootPathName, material.OriginalName); } IMaterialContentPersistManager persistManager = GetMaterialContentPersistManager(material.ID, destFileInfo); Stream stream = persistManager.GetMaterialContent(material.ID); result.Content = new StreamContent(stream); result.Content.Headers.ContentType = new MediaTypeHeaderValue(GetFileContentType(material.OriginalName)); result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("Attachment") { FileName = HttpContext.Current.EncodeFileNameByBrowser(material.OriginalName) }; return(result); }
private static IMaterialContentPersistManager GetMaterialContentPersistManager(string contentID, FileInfo destFile) { IMaterialContentPersistManager persistManager = MaterialContentSettings.GetConfig().PersistManager; persistManager.DestFileInfo = destFile; return(persistManager); }
private Stream GetImageContent() { IMaterialContentPersistManager materialManager = MaterialContentSettings.GetConfig().PersistManager; //ImageProperty prop = ImagePropertyAdapter.Instance.Load(contentId); MemoryStream stream = (MemoryStream)materialManager.GetMaterialContent("14773f2f-2adf-4ddc-926f-eda3b1b47b11"); return(stream); }
private void EnsureNotChangedImage() { if (this._Content.ContentData == null) { IMaterialContentPersistManager manager = MaterialContentSettings.GetConfig().PersistManager; manager.DestFileInfo = ImagePropertyAdapter.Instance.GetPhysicalDestinationImagePath(this._Content.FileName); this._Content.ContentData = manager.GetMaterialContent(this._Content.ContentID).ToBytes(); } }
public static MaterialModelCollection ProcessMaterialUpload(this HttpRequestMessage request) { MaterialModelCollection materials = new MaterialModelCollection(); request.Content.IsMimeMultipartContent("form-data").FalseThrow("上传请求的格式不正确"); InMemoryMultipartFormDataStreamProvider provider = InMemoryMultipartFormDataStreamProvider.GetProvider(request); string mumJson = provider.FormData.GetValue("materialUploadModel", string.Empty); MaterialUploadModel uploadModel = null; if (mumJson.IsNotEmpty()) { JsonSerializerSettings settings = new JsonSerializerSettings(); settings.ContractResolver = new CamelCasePropertyNamesContractResolver(); uploadModel = JsonConvert.DeserializeObject <MaterialUploadModel>(mumJson); } else { uploadModel = new MaterialUploadModel(); } foreach (StreamContent fileContent in provider.Files) { if (uploadModel.OriginalName.IsNullOrEmpty()) { ContentDispositionHeaderValue contentDisposition = fileContent.Headers.ContentDisposition; if (contentDisposition != null) { string fileName = contentDisposition.FileName.Replace("\"", string.Empty); uploadModel.OriginalName = Path.GetFileName(fileName); } } MaterialModel material = uploadModel.GenerateMaterial(); IMaterialContentPersistManager persistManager = GetMaterialContentPersistManager(material.ID, GetTempUploadFilePath(material.ID, uploadModel.RootPathName, uploadModel.OriginalName)); persistManager.SaveTempMaterialContent(material.ID, fileContent.ReadAsStreamAsync().Result); materials.Add(material); } return(materials); }
public static void Save(MaterialContent content) { IMaterialContentPersistManager manager = MaterialContentSettings.GetConfig().PersistManager; string path = GetUploadRootPath("ImageUploadRootPath"); string fileName = Path.Combine(path + @"Temp\", content.FileName); if (manager is FileMaterialContentPersistManager) { manager.DestFileInfo = new System.IO.FileInfo(Path.Combine(path, content.FileName)); } manager.SourceFileInfo = new System.IO.FileInfo(fileName); manager.SaveMaterialContent(content); }
private static IMaterialContentPersistManager GetMaterialContentPersistManager(string contentID, FileInfo destFile) { IMaterialContentPersistManager persistManager = MaterialContentSettings.GetConfig().PersistManager; if ((persistManager is FileMaterialContentPersistManager) == false) { if (persistManager.ExistsContent(contentID) == false) { persistManager = new FileMaterialContentPersistManager(); } } persistManager.DestFileInfo = destFile; return(persistManager); }
private static IMaterialContentPersistManager GetMaterialContentPersistManager(string rootPathName, MaterialFileOeprationInfo fileOp) { string uploadRootPath = AppPathConfigSettings.GetConfig().Paths[rootPathName].Dir; FileInfo sourceFile = new FileInfo(uploadRootPath + @"Temp\" + Path.GetFileName(fileOp.Material.RelativeFilePath)); FileInfo destFile = new FileInfo(uploadRootPath + fileOp.Material.RelativeFilePath); IMaterialContentPersistManager persistManager = MaterialContentSettings.GetConfig().PersistManager; persistManager.SourceFileInfo = sourceFile; persistManager.DestFileInfo = destFile; if (fileOp.Operation == FileOperation.Update) { persistManager.CheckSourceFileExists = false; } return(persistManager); }
/// <summary> /// 下载模板或已存在的文件 /// </summary> public static void Download(object sender, BeforeFileDownloadHandler beforeDownloadHandler, PrepareDownloadStreamHandler prepareDownloadStreamHandler, bool raiseEvent) { ObjectContextCache.Instance["FileUploadProcessed"] = true; HttpResponse response = HttpContext.Current.Response; bool fileReadonly = WebUtility.GetRequestQueryValue <bool>("fileReadonly", false); string materialID = string.Empty; try { string controlID = WebUtility.GetRequestQueryValue("controlID", string.Empty); FileDownloadInfo downloadInfo = FileDownloadInfo.CollectInfoFromRequest(); if (File.Exists(downloadInfo.FileFullPath) == false) { //是否需要启用映射机制(归档后,文件根目录的映射) string mappedRootPath = AppPathMappingContext.GetMappedPathName(downloadInfo.RootPathName); downloadInfo.FileFullPath = GetFileFullPath(downloadInfo.PathType, mappedRootPath, downloadInfo.FilePath); } response.ContentType = FileConfigHelper.GetFileContentType(downloadInfo.FileName); materialID = WebUtility.GetRequestQueryValue("materialID", string.Empty); if (materialID.IsNullOrEmpty()) { materialID = UuidHelper.NewUuidString(); } if (fileReadonly) { WebFileOpenMode openMode = FileConfigHelper.GetFileOpenMode(downloadInfo.FileName, downloadInfo.UserID); response.AppendHeader("content-disposition", string.Format("{0};filename={1}", openMode == WebFileOpenMode.Inline ? "inline" : "attachment", response.EncodeFileNameInContentDisposition(downloadInfo.FileName))); } else { string fileIconPath = FileConfigHelper.GetFileIconPath(downloadInfo.FileName); response.AppendHeader("fileIconPath", HttpUtility.UrlEncode("message=" + fileIconPath)); response.AppendHeader("content-disposition", "attachment;fileName=" + response.EncodeFileNameInContentDisposition(downloadInfo.FileName)); if (downloadInfo.PathType != PathType.relative) { response.AppendHeader("materialID", "message=" + materialID); } } bool responseFile = true; if (beforeDownloadHandler != null && raiseEvent) { responseFile = beforeDownloadHandler(sender, downloadInfo); } if (responseFile) { IMaterialContentPersistManager persistManager = GetMaterialContentPersistManager(materialID, new FileInfo(downloadInfo.FileFullPath)); using (Stream stream = persistManager.GetMaterialContent(materialID)) { if (prepareDownloadStreamHandler != null && raiseEvent) { PrepareDownloadStreamEventArgs args = new PrepareDownloadStreamEventArgs(); args.DownloadInfo = downloadInfo; args.InputStream = stream; args.OutputStream = response.OutputStream; prepareDownloadStreamHandler(sender, args); } else { stream.CopyTo(response.OutputStream); } } } Control control = null; if (controlID.IsNotEmpty()) { control = WebControlUtility.FindControlByID((Page)HttpContext.Current.CurrentHandler, controlID, true); } if (control != null && control is MaterialControl) { DownloadEventArgs args = new DownloadEventArgs(materialID, downloadInfo.FilePath); ((MaterialControl)control).OnDownloadFile(args); } } catch (Exception ex) { if (fileReadonly) { response.Write(ex.Message); } else { GenerateErrorInformation(ex.ToString()); } } finally { try { response.End(); } catch { } } }
/// <summary> /// 上传 /// </summary> public static void Upload() { 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 = GetUploadRootPath(); AutoCreateUploadPath(uploadPath); Control control = null; if (string.IsNullOrEmpty(controlID) == false) { control = WebControlUtility.FindControlByID((Page)HttpContext.Current.CurrentHandler, controlID, true); } string newID = UuidHelper.NewUuidString(); if (fileName.IndexOf(NewMaterialID) == 0) { fileName = fileName.Replace(NewMaterialID, newID); } else { newID = Path.GetFileNameWithoutExtension(fileName); } if (control != null && control is MaterialControl) { UploadEventArgs args = new UploadEventArgs(fileName); ((MaterialControl)control).OnBeforeUploadFile(args); } IMaterialContentPersistManager persistManager = GetMaterialContentPersistManager(newID, new FileInfo(uploadPath + @"Temp\" + fileName)); if (request.QueryString["upmethod"] == "new") { var dialogControlID = WebUtility.GetRequestQueryValue("dialogControlID", string.Empty); persistManager.SaveTempMaterialContent(newID, request.Files[0].InputStream); //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 { //request.SaveAs(uploadPath + @"Temp\" + fileName, false); persistManager.SaveTempMaterialContent(newID, request.InputStream); } if (control != null && control is MaterialControl) { UploadEventArgs args = new UploadEventArgs(fileName); ((MaterialControl)control).OnAfterUploadFile(args); } string fileIconPath = FileConfigHelper.GetFileIconPath(fileName); response.AppendHeader("fileIconPath", HttpUtility.UrlEncode("message=" + fileIconPath)); string dateTimeNow = SNTPClient.AdjustedTime.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.ToString(); if (ex is ExternalException && ((ExternalException)(ex)).ErrorCode == FileTooLargeError && ex.Source == "System.Web") { errorMessage = "您上传的文件过大"; } GenerateErrorInformation(errorMessage); } finally { try { response.End(); } catch { } } }