Solved

How to Upload xml file into drive location

  • 23 May 2023
  • 1 reply
  • 70 views

Userlevel 3
Badge

Hi,

I need to create xml file and then need to upload into drive location.

Below code for creating xml file and download into local machine.

 #region Actions
        public PXAction<POOrder> MyAction;
        [PXUIField(DisplayName = "Export XML", MapEnableRights = PXCacheRights.Select, MapViewRights = PXCacheRights.Select)]
        [PXButton(CommitChanges = true)]
        public virtual IEnumerable myAction(PXAdapter adapter){
           
          XmlDocument xmlDoc = new XmlDocument();
          XmlDeclaration xmldecl;
          xmldecl=xmlDoc.CreateXmlDeclaration("1.0", null, null);

         // Create root node of XML file
         XmlNode rootNode = xmlDoc.CreateElement("GridData");

          foreach (InventoryItem dacRecord in dataview.Select())
             {      
                // Create an XML Element to represent the DAC row
                XmlNode xmlDACRecord = xmlDoc.CreateElement("DACRecord");
          
                // Add desired DAC fields as child XML Elements of the DAC row XML element
                XmlNode xmlDACField1 = xmlDoc.CreateElement("ItemType");
                xmlDACField1.AppendChild(xmlDoc.CreateTextNode(dacRecord.ItemType.ToString()));
                xmlDACRecord.AppendChild(xmlDACField1);
               
               XmlNode xmlDACField2 = xmlDoc.CreateElement("Item");
               xmlDACField2.AppendChild(xmlDoc.CreateTextNode(dacRecord.InventoryCD.ToString()));
               xmlDACRecord.AppendChild(xmlDACField2);
          
                // Adding XML DAC Record to XML root node
                rootNode.AppendChild(xmlDACRecord);
             }
               
               // Adding XML root node to XML document
   xmlDoc.AppendChild(rootNode);

   // Redirect browser to XML file created in memory on server
   throw new PXRedirectToFileException(new PX.SM.FileInfo(Guid.NewGuid(),
                                                          "filename.xml",
                                                          null,
                                                          System.Text.Encoding.UTF8.GetBytes(xmlDoc.OuterXml)),
                                       true);
         return adapter.Get();
             }
 #endregion
 

Can you please help for me to upload this file into drive location using acumatica.

 

icon

Best answer by sweta68 23 May 2023, 05:52

View original

1 reply

Userlevel 7
Badge +10

Hi @jeewanishalika20 

To upload this file to a drive location using Acumatica, utilize the following code snippet.

#region Actions
public PXAction<POOrder> MyAction;
[PXUIField(DisplayName = "Export XML", MapEnableRights = PXCacheRights.Select, MapViewRights = PXCacheRights.Select)]
[PXButton(CommitChanges = true)]
public virtual IEnumerable myAction(PXAdapter adapter)
{
// Your existing code for XML creation
XmlDocument xmlDoc = new XmlDocument();
// ...

// Save the XML document to a byte array
byte[] xmlData = System.Text.Encoding.UTF8.GetBytes(xmlDoc.OuterXml);

// Upload the file to the desired drive location
PX.SM.FileInfo fileInfo = new PX.SM.FileInfo(Guid.NewGuid(), "filename.xml", null, xmlData);
PX.SM.FileInfo uploadedFile = PX.Common.FileUploadProvider.SaveFile(fileInfo);

// Check if the file was uploaded successfully
if (uploadedFile != null)
{
// Specify the drive location where you want to save the file
string folderPath = "Your/Drive/Location/Folder/";

// Create a new folder in the drive location if needed
PX.SM.FileInfo folderInfo = PX.SM.FileInfo.FromPath(folderPath);
PX.SM.FileInfo createdFolder = PX.Common.FileUploadProvider.CreateFolder(folderInfo);

if (createdFolder != null)
{
// Move the uploaded file to the desired folder
PX.Common.FileUploadProvider.MoveFile(uploadedFile.UID, createdFolder.UID);

// Display a message to the user indicating a successful upload
PX.Common.PXContext.Session.FolderItemMessage = "File uploaded successfully.";
}
else
{
// Display an error message if the folder creation failed
PX.Common.PXContext.Session.FolderItemMessage = "Error creating folder.";
}
}
else
{
// Display an error message if the file upload failed
PX.Common.PXContext.Session.FolderItemMessage = "Error uploading file.";
}

return adapter.Get();
}
#endregion

After generating the XML file and converting it to a byte array, we use the PX.Common.FileUploadProvider to save the file using Save File

Finally, we move the uploaded file to the specified folder using Move File.
Please modify the folderPath variable to correspond with your preferred drive location in Acumatica.

Hope it helps!

Regards,

Sweta

Reply


About Acumatica ERP system
Acumatica Cloud ERP provides the best business management solution for transforming your company to thrive in the new digital economy. Built on a future-proof platform with open architecture for rapid integrations, scalability, and ease of use, Acumatica delivers unparalleled value to small and midmarket organizations. Connected Business. Delivered.
© 2008 — 2024  Acumatica, Inc. All rights reserved