Skip to main content
Solved

Upload xml file into drive location

  • 22 May 2023
  • 1 reply
  • 67 views

Forum|alt.badge.img

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.

 

Best answer by sweta68

Hi @jeewanishalika20 

To upload the file to a drive location using Acumatica, you can refer to the following code.

#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);

    // Save XML document to a file on the server
    string fileName = "filename.xml";
    string filePath = System.Web.Hosting.HostingEnvironment.MapPath("~/App_Data/") + fileName;
    xmlDoc.Save(filePath);

    // Upload the XML file to the desired drive location
    var uploadFileMaintenance = PXGraph.CreateInstance<UploadFileMaintenance>();
    byte[] fileData = System.IO.File.ReadAllBytes(filePath);
    PX.SM.FileInfo fileInfo = new PX.SM.FileInfo(Guid.NewGuid(), fileName, null, fileData);
    uploadFileMaintenance.SaveFile(fileInfo);

    // Delete the temporary XML file from the server
    System.IO.File.Delete(filePath);

    return adapter.Get();
}
#endregion

In this code, the XML document is saved to a temporary file on the server using the Save method of the XmlDocument class. Then, the UploadFileMaintenance graph is used to read the file from the server, create a PX.SM.FileInfo object, and save the file using the Save File method. After the file is uploaded, the temporary XML file is deleted from the server using System.IO.File.Delete.

 

Make sure to replace "filename.xml" with the desired file name and update the file path in the filePath variable to match the location where you want to temporarily save the file on the server.

Hope it helps.!

Regards,

Sweta

View original
Did this topic help you find an answer to your question?

1 reply

Forum|alt.badge.img+10
  • Semi-Pro III
  • 229 replies
  • Answer
  • May 25, 2023

Hi @jeewanishalika20 

To upload the file to a drive location using Acumatica, you can refer to the following code.

#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);

    // Save XML document to a file on the server
    string fileName = "filename.xml";
    string filePath = System.Web.Hosting.HostingEnvironment.MapPath("~/App_Data/") + fileName;
    xmlDoc.Save(filePath);

    // Upload the XML file to the desired drive location
    var uploadFileMaintenance = PXGraph.CreateInstance<UploadFileMaintenance>();
    byte[] fileData = System.IO.File.ReadAllBytes(filePath);
    PX.SM.FileInfo fileInfo = new PX.SM.FileInfo(Guid.NewGuid(), fileName, null, fileData);
    uploadFileMaintenance.SaveFile(fileInfo);

    // Delete the temporary XML file from the server
    System.IO.File.Delete(filePath);

    return adapter.Get();
}
#endregion

In this code, the XML document is saved to a temporary file on the server using the Save method of the XmlDocument class. Then, the UploadFileMaintenance graph is used to read the file from the server, create a PX.SM.FileInfo object, and save the file using the Save File method. After the file is uploaded, the temporary XML file is deleted from the server using System.IO.File.Delete.

 

Make sure to replace "filename.xml" with the desired file name and update the file path in the filePath variable to match the location where you want to temporarily save the file on the server.

Hope it helps.!

Regards,

Sweta


Reply


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings