Tuesday, 29 March 2016

Processing File In Memory

Hi All,

Sometimes you want to upload file to Acumatica but you do not want to save it to the database. This may requires for in-place data processing or uploading data from specific format.
Today I want to show you one way, how you can allow user to upload file in the browser and handle it with your server logic.
In high level, to achieve it you need to do 3 steps
  1. Add special PXUploadDialog panel to your page. This control will handle file upload and save somewhere. By specifying configuration properties, you can control where Acumatica will save a file. In our case we want to store it in session.
  2. Defile dummy data member that will be associated with upload dialog. Buy calling Ask() method here, you will trigger panel and user will be able to upload file.
  3. Add button and call dummy data member from previous step. After uploading Acumatica will automatically call your method second time, and then you will be able to get file from session.
Now lets go and implement all these steps together.

Adding Panel
First of all we need to add a panel to our screen. Unfortunately there is no way to add it through the designer so you have to edit ASPX code. But fortunately you can do it in Visual Studio and Browser as well. IF you want to use VS, just open page and add required code. If you are using browser, that you need to go to page, click Actions->Edit ASPX and past code there. 
  <px:PXUploadDialog ID="pnlNewRev"runat="server" Height="120px" 
    Style="position: static" Width="560px" Caption="Statement File Upload"
    Key="NewFilePanel" SessionKey="MyFileImportSessionKey" 
    AutoSaveFile="false" RenderCheckIn="false" />

Please note, that if you edit existing page in Acumatica, you may need to "generate upgrade script" after you change ASPX. This action will generate a special script that will have difference between old and new version of ASXP. This script will be executed each time when you publish customization to modify existing page according yo you changes.

The most important points in the script above are highlighted with bold:
  • Key - is link to data member in graph
  • SessionKey - is unique session key name. This name will be used to save and retrieve file from session.
  • AutoSaveFile - setting that advises Acumatica to not save file to database.
Data Member
Go to the graph code file and define new data member that will be associated with panel. It is not important what DAC will you use for this data member, but make sure that the name is the same as Key in the panel.

public PXSelect<GLTran> NewFilePanel;

Button
Button is your custom handler where you want to process file. Here you need to call Ask() method from data member, in this case Acumatica will show upload panel to user. If everything is OK, Acumatica will call your method second time and you can process further with your file.

Code Snippet:

Please note that Acumatica may have some file size and file types limitation. This can be changed in Upload File Preferences screen.
Also do not forget to remove file from session after processing. This will help you to save server memory. Acumatica will remove this file automatically only when session will be expired.
Acumatica will provide you a binary data of the file, so you can open it with any possible way. Also it may help you to process encrypted and special format files.

That is all, Have a nice development!

No comments: