Search code examples
asp.netfile-uploadacumaticaerpacumatica-kb

How do I upload a file to an Acumatica Screen through HTTP virtual path?


How do I upload a file to an Acumatica Screen through HTTP virtual path? For example, I would like to upload mysite.com/files/abc.pdf to the Sales orders screen.


Solution

  • Below is a code snippet to achieve your goal.It is reading file from HTTP URL and attaching it to one of the existing Case.

          //Graph for file management
          PX.SM.UploadFileMaintenance filegraph = PXGraph.CreateInstance<PX.SM.UploadFileMaintenance>();
          //Since you need file from HTTP URL - below is a sample
          WebRequest request = WebRequest.Create("http://www.pdf995.com/samples/pdf.pdf");
          using (System.IO.Stream dataStream = request.GetResponse().GetResponseStream())
          {
              using (System.IO.MemoryStream mStream = new System.IO.MemoryStream())
              {
                  dataStream.CopyTo(mStream);
                  byte[] data = mStream.ToArray();                 
    
                  //Create file info, you may check different overloads as per your need
                  PX.SM.FileInfo fileinfo = new PX.SM.FileInfo("case.pdf", null, data);
    
                  if (filegraph.SaveFile(fileinfo))
                  {
                      if (fileinfo.UID.HasValue)
                      {
                          // To attach the file to case screen - example
                          CRCaseMaint graphCase = PXGraph.CreateInstance<CRCaseMaint>();
    
                          //Locate existing case
                          graphCase.Case.Current = graphCase.Case.Search<CRCase.caseCD>("<Case to which you want to attach file>");
    
                          //To Attach file
                          PXNoteAttribute.SetFileNotes(graphCase.Case.Cache, graphCase.Case.Current, fileinfo.UID.Value);
    
                          //To Attach note
                          PXNoteAttribute.SetNote(graphCase.Case.Cache, graphCase.Case.Current, "<Note you wish to specify>");
    
                          //Save case
                          graphCase.Save.Press();
                      }
                  }
              }
          }