1.描述
插件支持将文件或图像上传至活字格,在IBuilderContext中有一个文件夹路径,可以包含上传的文件或图像,用户甚至可以基于它将其插件文件夹组合在一起。因此,当您将活字格文件导入到另一个文档时,除非插件实现了接口INeedUploadFileByUser,否则文件或图像不会被导入到新文档中。
2.示例代码
下面的示例中使用了INeedUploadFileByUser接口。
namespace Carousel { [Designer("Carousel.CarouselDesigner,Carousel")] public class Carousel : CellType, INeedUploadFileByUser { public List<ImageInfo> ImageInfos { get; set; } /// <summary> /// 当导入forguncy文件时,将调用该方法。 /// 并且会将源文件上传文件夹中的所有文件复制到当前文档上传文件夹中; /// 因此,您应该返回源文档中的所有文件路径和当前文档中的所有文件。 /// </summary> /// <param name="context"></param> /// <returns></returns> public List<FileCopyInfo> GetAllFileSourceAndTargetPathsWhenImportForguncyFile(IFileUploadContext context) { var list = new List<FileCopyInfo>(); if (ImageInfos != null) { foreach (var imageInfo in ImageInfos) { if (string.IsNullOrEmpty(imageInfo.Name)) { continue; } list.Add(new FileCopyInfo() { //based the source document upload folder, then combine the plugin folders you custom defined together. SourceFileFolder = Path.Combine(context.GetForguncyUploadFilesRelativePath(context.SourceDocumentFolder), "CarouselCellType", "Images"), SourceFileName = imageInfo.ImagePath, //current document upload folder, then combine the plugin folders you custom defined together. TargetFileFolder = Path.Combine(context.GetForguncyUploadFilesFolderLocalPath(), "CarouselCellType", "Images"), TargetFileName = imageInfo.ImagePath }); } } return list; } /// <summary> /// 当保存forguncy文档时,将调用该方法。 /// 并且在保存文档时将删除未使用的文件,以便在这里返回所有使用过的文件路径。 /// </summary> /// <param name="context"></param> /// <returns></returns> public FileUploadInfo GetUploadFileInfosWhenSaveFile(IFileUploadContext context) { var result = new FileUploadInfo() { //current document upload folder, then combine the plugin folders you custom defined together. UploadFileFolder = Path.Combine(context.GetForguncyUploadFilesFolderLocalPath(), "CarouselCellType", "Images") }; if (ImageInfos != null) { foreach (var imageInfo in ImageInfos) { if (string.IsNullOrEmpty(imageInfo.ImagePath)) { continue; } result.AllUsedUploadFilePaths.Add(imageInfo.ImagePath); } } return result; } } public class ImageInfo { public string Name { get; set; } public string ImagePath { get; set; } } }