Button to create .Zip file from files attached to Acumatica document
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public PXAction<ARInvoice> CreateZip; | |
[PXButton(CommitChanges = false)] | |
[PXUIField(DisplayName = "Create .Zip", MapEnableRights = PXCacheRights.Update, MapViewRights = PXCacheRights.Update, Enabled = true)] | |
protected virtual IEnumerable createZip(PXAdapter pxAdapter) | |
{ | |
if (Base.Document.Current != null) | |
{ | |
UploadFileMaintenance uploadFileMaint = PXGraph.CreateInstance<UploadFileMaintenance>(); | |
PX.SM.FileInfo fileInfo; | |
using (MemoryStream zipStream = new MemoryStream()) | |
{ | |
using(ZipArchive zipArchive = ZipArchive.CreateFrom(zipStream, false)) | |
{ | |
//Retrieves files from the invoice | |
foreach (Guid file in PXNoteAttribute.GetFileNotes(Base.Document.Cache, Base.Document.Current)) | |
{ | |
fileInfo = uploadFileMaint.GetFile(file); | |
if(fileInfo != null) | |
{ | |
zipArchive.AddFile(fileInfo.Name, fileInfo.BinData); | |
} | |
} | |
} | |
//Create Acumatica file from .Zip file content | |
fileInfo = new PX.SM.FileInfo("Test.Zip", null, zipStream.ToArray()); | |
//Saves file to Invoice | |
if (uploadFileMaint.SaveFile(fileInfo, FileExistsAction.CreateVersion)) | |
{ | |
PXNoteAttribute.AttachFile(Base.Document.Cache, Base.Document.Current, fileInfo); | |
} | |
} | |
} | |
return pxAdapter.Get(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment