DevExpress社のASPxFileManagerでは、ファイルの上書きアップロードではエラーが発生する。ファイルを削除してアップロードすれば問題ないが、手間がかかるので、上書きアップロードさせる際のメモ
ASPxFileManagerでは、任意のCustomFileSystemProviderが定義でき、そこでファイルの存在するExists()があるので、そこで、ファイルアップロード時のみ、ファイルがないと詐称させて、上書きを可能にする。
UploadFileSystemProvider.cs
ファイルの上書きアップロード用のプロバイダークラスを作成する。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
public class UploadFileSystemProvider : PhysicalFileSystemProvider { public UploadFileProviderOptions ProviderOptions { get; set; } public UploadFileSystemProvider(string rootFolder, UploadFileProviderOptions providerOptions) : base(rootFolder) { this.ProviderOptions = providerOptions; } public override bool Exists(FileManagerFile file) { if (this.ProviderOptions.AllowOverwrite) { return false; } return base.Exists(file); } public override void UploadFile(FileManagerFolder folder, string fileName, System.IO.Stream content) { base.UploadFile(folder, fileName, content); this.ProviderOptions.AllowOverwrite = false; } } public class UploadFileProviderOptions { public bool AllowOverwrite { get; set; } } |
PhotosFileControl.ascx
ASPxFileManagerコントロールで、OnInitとOnFileUploadingのイベント処理を作成する
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
<dx:ASPxFileManager ID="PhotosFileManager" runat="server" Width="100%" Height="100%" ClientInstanceName="PhotosFileManager" OnInit="OnInit" OnFileUploading="OnFileUploading" Theme="Moderno"> <ClientSideEvents SelectedFileOpened="OnSelectedPhotoFileOpened"></ClientSideEvents> <Settings RootFolder="~/Photos" ThumbnailFolder="~/Photos/Thumbnails" AllowedFileExtensions=".jpg,.jpeg" InitialFolder="" /> <SettingsEditing AllowCreate="false" AllowDelete="false" AllowMove="true" AllowRename="true" AllowCopy="true" AllowDownload="true" /> <SettingsFileList> <ThumbnailsViewSettings ThumbnailWidth="100" ThumbnailHeight="100" /> </SettingsFileList> <SettingsPermissions> <AccessRules> <dx:FileManagerFolderAccessRule Path="System" Edit="Deny" /> <dx:FileManagerFileAccessRule Path="System\*" Download="Deny" /> </AccessRules> </SettingsPermissions> <SettingsFileList ShowFolders="true" ShowParentFolder="true" /> <SettingsBreadcrumbs Visible="true" ShowParentFolderButton="true" Position="Top" /> <SettingsUpload Enabled="true" /> <SettingsAdaptivity Enabled="true" /> </dx:ASPxFileManager> |
AllowOverwriteで初期値をfalseにしているのは、ダウンロード処理などアップロード以外の処理でエラーが発生するため。アップロードの場合は、UploadFileSystemProvider.Exists()で、ファイルがないと詐称して上書きを可能にさせている
1 2 3 4 5 6 7 8 9 10 11 12 13 |
public partial class PhotosFileControl : System.Web.UI.UserControl { public void OnInit(object sender, EventArgs e) { var providerOptions = new UploadFileProviderOptions() { AllowOverwrite = false }; this.PhotosFileManager.CustomFileSystemProvider = new UploadFileSystemProvider(this.PhotosFileManager.Settings.RootFolder, providerOptions); } protected void OnFileUploading(object source, FileManagerFileUploadEventArgs e) { ((UploadFileSystemProvider)this.PhotosFileManager.CustomFileSystemProvider).ProviderOptions.AllowOverwrite = true; } } |
修正後の画面イメージ