Skip to content

Instantly share code, notes, and snippets.

@jvanhoesen
Created March 28, 2022 20:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jvanhoesen/afc4a07b8f8aeb389be02772769e3980 to your computer and use it in GitHub Desktop.
Save jvanhoesen/afc4a07b8f8aeb389be02772769e3980 to your computer and use it in GitHub Desktop.
Button to create .Zip file from files attached to Acumatica document
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