How to: Use a Custom Action to Upload a File in a Popup Window
- 2 minutes to read
The following example shows a popup window to upload a file and save it to a stream.
Note
For more information on how to download a file in XAF Blazor apps, refer to the following topic: How to: Download a File in XAF Blazor Applications.
Add the File Attachments Module to your application as described in the following section: Add File Attachments Module to an XAF Application
Create the UploadFileParameters class with the File property of the FileData type. XAF shows FileDataPropertyEditor for this property type. For more information, see the following help topic: File Attachment Properties.
Use an instance of the PopupWindowShowAction class to show a popup window.
Handle the Execute event and call the SaveToStream(Stream) method to save the file to a stream. Note that the gzip compression is applied to the saved byte array.
using System.IO;
using DevExpress.ExpressApp;
using DevExpress.ExpressApp.Actions;
using DevExpress.ExpressApp.DC;
using DevExpress.Persistent.Base;
using DevExpress.Persistent.BaseImpl.EF; // For XPO, replace with 'using DevExpress.Persistent.BaseImpl;'.
using DevExpress.Persistent.Validation;
namespace XafUploadButton.Module.Controllers {
public partial class UploadInPopupController : ViewController {
public UploadInPopupController() {
var customUploadFileAction = new PopupWindowShowAction(this,"CustomUploadFileAction", PredefinedCategory.View){
Caption = "Custom Upload",
ImageName = "AddFile"
};
customUploadFileAction.Execute += CustomUploadFileAction_Execute;
customUploadFileAction.CustomizePopupWindowParams += CustomUploadFileAction_CustomizePopupWindowParams;
}
private void CustomUploadFileAction_Execute(object sender, PopupWindowShowActionExecuteEventArgs e) {
FileData fileData = ((UploadFileParameters)e.PopupWindowViewCurrentObject).File;
using(var stream = new MemoryStream()) {
fileData.SaveToStream(stream);
stream.Position = 0;
// Perform your operations with the stream.
}
}
private void CustomUploadFileAction_CustomizePopupWindowParams(object sender, CustomizePopupWindowParamsEventArgs e) {
NonPersistentObjectSpace os = (NonPersistentObjectSpace)e.Application.CreateObjectSpace(typeof(UploadFileParameters));
os.PopulateAdditionalObjectSpaces(Application);
e.DialogController.SaveOnAccept = false;
e.View = e.Application.CreateDetailView(os, os.CreateObject<UploadFileParameters>());
}
}
[DomainComponent]
public class UploadFileParameters : NonPersistentObjectImpl {
private FileData file;
[ExpandObjectMembers(ExpandObjectMembers.Never)]
[RuleRequiredField("Save", "File should be assigned")]
public FileData File {
get { return file; }
set { SetPropertyValue(ref file, value); }
}
}
}