/// <summary>保存附件信息</summary> public static void Upload(IAttachmentFileInfo file) { // ------------------------------------------------------- // 保存 数据库 // 数据库 支持数据库集群 // ------------------------------------------------------- if (AttachmentStorageConfigurationView.Instance.DistributedFileStorageMode == "ON") { DistributedFileInfo param = new DistributedFileInfo(); param.Id = file.Id; param.VirtualPath = file.VirtualPath; param.FileData = file.FileData; AttachmentStorageContext.Instance.AttachmentDistributedFileService.Save(param); } // ------------------------------------------------------- // 保存 二进制数据 // ------------------------------------------------------- string path = UploadPathHelper.CombinePhysicalPath(file.Parent.AttachmentFolder, string.Format("{0}{1}", file.Id, file.FileType)); if (!File.Exists(path)) { UploadPathHelper.TryCreateDirectory(path); ByteHelper.ToFile(file.FileData, path); } }
public ActionResult Read(string pathInfo) { UploadPathHelper.CheckFileNameSecurity(pathInfo); if (!UploadStorage.FileExists(pathInfo)) { return(new NotFoundResult()); } var mimeType = KnownMimeTypes.Get(pathInfo); var stream = UploadStorage.OpenFile(pathInfo); return(new FileStreamResult(stream, mimeType)); }
// ------------------------------------------------------- // 查询 // ------------------------------------------------------- #region 函数:FindOne(string id) /// <summary>查询某条记录</summary> /// <param name="id">AccountInfo Id号</param> /// <returns>返回一个 实例<see cref="IAttachmentFileInfo"/>的详细信息</returns> public IAttachmentFileInfo FindOne(string id) { AttachmentFileInfo param = (AttachmentFileInfo)provider.FindOne(id); IAttachmentParentObject parent = new AttachmentParentObject(); parent.EntityId = param.EntityId; parent.EntityClassName = param.EntityClassName; parent.AttachmentEntityClassName = KernelContext.ParseObjectType(typeof(AttachmentFileInfo)); parent.AttachmentFolder = UploadPathHelper.GetAttachmentFolder(param.VirtualPath, param.FolderRule); param.Parent = parent; return(param); }
/// <summary>物理复制全部附件信息到实体类</summary> /// <param name="param"><see cref="IAttachmentFileInfo" />实例详细信息</param> /// <param name="entityId">实体标识</param> /// <param name="entityClassName">实体类名称</param> /// <returns>新的<see cref="IAttachmentFileInfo" />实例详细信息</returns> public IAttachmentFileInfo Copy(IAttachmentFileInfo param, string entityClassName, string entityId) { IAttachmentParentObject parent = new AttachmentParentObject(); parent.EntityId = entityId; parent.EntityClassName = entityClassName; parent.AttachmentEntityClassName = KernelContext.ParseObjectType(typeof(AttachmentFileInfo)); parent.AttachmentFolder = UploadPathHelper.GetAttachmentFolder(param.VirtualPath, param.FolderRule); IAttachmentFileInfo attachment = UploadFileHelper.CreateAttachmentFile(parent, param.AttachmentName, param.FileType, param.FileSize, param.FileData); attachment.Save(); return(attachment); }
public void TestCombineVirtualPath() { string attachmentFolder = "test"; string fileName = "123.doc"; DateTime datetime = DateTime.Now; string path1 = AttachmentStorageConfigurationView.Instance.VirtualUploadFolder + attachmentFolder + "/" + datetime.Year + "/" + (((datetime.Month - 1) / 3) + 1) + "Q/" + datetime.Month + "/" + fileName; string path2 = UploadPathHelper.CombineVirtualPath(attachmentFolder, fileName); Assert.AreEqual(path1, path2); }
/// <summary>获取附件信息</summary> public static byte[] Download(IAttachmentFileInfo file) { DistributedFileInfo param = AttachmentStorageContext.Instance.AttachmentDistributedFileService.FindOne(file.Id); if (param == null) { return(null); } else { string path = file.VirtualPath.Replace("{uploads}", AttachmentStorageConfigurationView.Instance.PhysicalUploadFolder); UploadPathHelper.TryCreateDirectory(path); ByteHelper.ToFile(param.FileData, path); return(param.FileData); } }
/// <summary>保存附件信息</summary> public void Save() { AttachmentStorageContext.Instance.AttachmentFileService.Save(this); // ------------------------------------------------------- // 保存 二进制数据 // ------------------------------------------------------- string path = UploadPathHelper.CombinePhysicalPath(Parent.AttachmentFolder, string.Format("{0}{1}", Id, FileType)); UploadPathHelper.TryCreateDirectory(path); ByteHelper.ToFile(FileData, path); // ------------------------------------------------------- // 保存 二进制信息 // ------------------------------------------------------- DistributedFileStorage.Upload(this); }
public void TestGetVirtualPathFormat() { string attachmentFolder = "test"; AttachmentFileInfo attachment = new AttachmentFileInfo(); attachment.Id = StringHelper.ToGuid(); attachment.FileType = ".doc"; attachment.AttachmentName = "123.doc"; attachment.CreatedDate = new DateTime(2001, 1, 1); DateTime datetime = attachment.CreatedDate; string path1 = "{uploads}" + attachmentFolder + "/" + datetime.Year + "/" + (((datetime.Month - 1) / 3) + 1) + "Q/" + datetime.Month + "/" + attachment.Id + attachment.FileType; string path2 = UploadPathHelper.GetVirtualPathFormat(attachmentFolder, attachment); Assert.AreEqual(path1, path2); }
public ExcelImportResponse ExcelImport(IUnitOfWork uow, ExcelImportRequest request, [FromServices] IUploadStorage uploadStorage) { if (request is null) { throw new ArgumentNullException(nameof(request)); } if (string.IsNullOrWhiteSpace(request.FileName)) { throw new ArgumentNullException(nameof(request.FileName)); } if (uploadStorage is null) { throw new ArgumentNullException(nameof(uploadStorage)); } UploadPathHelper.CheckFileNameSecurity(request.FileName); if (!request.FileName.StartsWith("temporary/")) { throw new ArgumentOutOfRangeException(nameof(request.FileName)); } ExcelPackage ep = new ExcelPackage(); using (var fs = uploadStorage.OpenFile(request.FileName)) ep.Load(fs); var p = ProductRow.Fields; var s = SupplierRow.Fields; var c = CategoryRow.Fields; var response = new ExcelImportResponse(); response.ErrorList = new List <string>(); var worksheet = ep.Workbook.Worksheets[0]; for (var row = 2; row <= worksheet.Dimension.End.Row; row++) { try { var productName = Convert.ToString(worksheet.Cells[row, 1].Value ?? ""); if (productName.IsTrimmedEmpty()) { continue; } var product = uow.Connection.TryFirst <ProductRow>(q => q .Select(p.ProductID) .Where(p.ProductName == productName)); if (product == null) { product = new ProductRow { ProductName = productName } } ; else { // avoid assignment errors product.TrackWithChecks = false; } var supplierName = Convert.ToString(worksheet.Cells[row, 2].Value ?? ""); if (!string.IsNullOrWhiteSpace(supplierName)) { var supplier = uow.Connection.TryFirst <SupplierRow>(q => q .Select(s.SupplierID) .Where(s.CompanyName == supplierName)); if (supplier == null) { response.ErrorList.Add("Error On Row " + row + ": Supplier with name '" + supplierName + "' is not found!"); continue; } product.SupplierID = supplier.SupplierID.Value; } else { product.SupplierID = null; } var categoryName = Convert.ToString(worksheet.Cells[row, 3].Value ?? ""); if (!string.IsNullOrWhiteSpace(categoryName)) { var category = uow.Connection.TryFirst <CategoryRow>(q => q .Select(c.CategoryID) .Where(c.CategoryName == categoryName)); if (category == null) { response.ErrorList.Add("Error On Row " + row + ": Category with name '" + categoryName + "' is not found!"); continue; } product.CategoryID = category.CategoryID.Value; } else { product.CategoryID = null; } product.QuantityPerUnit = Convert.ToString(worksheet.Cells[row, 4].Value ?? ""); product.UnitPrice = Convert.ToDecimal(worksheet.Cells[row, 5].Value ?? 0); product.UnitsInStock = Convert.ToInt16(worksheet.Cells[row, 6].Value ?? 0); product.UnitsOnOrder = Convert.ToInt16(worksheet.Cells[row, 7].Value ?? 0); product.ReorderLevel = Convert.ToInt16(worksheet.Cells[row, 8].Value ?? 0); if (product.ProductID == null) { new ProductRepository(Context).Create(uow, new SaveRequest <MyRow> { Entity = product }); response.Inserted = response.Inserted + 1; } else { new ProductRepository(Context).Update(uow, new SaveRequest <MyRow> { Entity = product, EntityId = product.ProductID.Value }); response.Updated = response.Updated + 1; } } catch (Exception ex) { response.ErrorList.Add("Exception on Row " + row + ": " + ex.Message); } } return(response); }