Created
March 28, 2022 19:49
-
-
Save jvanhoesen/a7f83bda81022b2aa4580afaff5af77e to your computer and use it in GitHub Desktop.
Opens a zip file attached to an 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> OpenZip; | |
[PXButton(CommitChanges = false)] | |
[PXUIField(DisplayName = "Open .Zip", MapEnableRights = PXCacheRights.Update, MapViewRights = PXCacheRights.Update, Enabled = true)] | |
protected virtual IEnumerable openZip(PXAdapter pxAdapter) | |
{ | |
if(Base.Document.Current != null) | |
{ | |
//Retrieves the sample .Zip file from the invoice | |
Guid zipFile = PXNoteAttribute.GetFileNotes(Base.Document.Cache, Base.Document.Current).First(); | |
if(zipFile != null) | |
{ | |
UploadFileMaintenance uploadFileMaint = PXGraph.CreateInstance<UploadFileMaintenance>(); | |
PX.SM.FileInfo fileInfo = uploadFileMaint.GetFile(zipFile); | |
if(fileInfo != null) | |
{ | |
using (MemoryStream stream = new MemoryStream(fileInfo.BinData)) | |
{ | |
//Create new instance of ZipArchive to interact with .ZIP file | |
using (ZipArchive zip = ZipArchive.CreateFrom(stream, true)) | |
{ | |
//Iterate through all files and retrieve their ItemInfo | |
foreach(ZipArchive.ItemInfo info in zip.GetFiles()) | |
{ | |
//Creates a stream from the specified file within the .ZIP file | |
using (Stream fileStream = zip.OpenRead(info.Name)) | |
{ | |
using(MemoryStream innerStream = new MemoryStream()) | |
{ | |
fileStream.CopyTo(innerStream); | |
//Create Acumatica file from .Zip file content | |
fileInfo = new PX.SM.FileInfo(info.Name, null, innerStream.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