Written by

Software Architect at Visum
Question Yuri Marx · Nov 3, 2020

Save multipart file upload to a Ens.BusinessService

I created a Custom Business Service to receive a multipart message with a file and I need to save only the file, not all message. How Can I extract only the file from the pInput to save in a File? This is my code:

Class dc.Test.TestService Extends Ens.BusinessService
{

 

Parameter ADAPTER = "EnsLib.HTTP.InboundAdapter";

 

Method OnProcessInput(pInput As %GlobalBinaryStreampOutput As %RegisteredObjectAs %Status
{
    Set file=##class(%Stream.FileBinary).%New()
    Set file.Filename="/tmp/automl.png"
    Do file.CopyFromAndSave(pInput
    

 

    Set tSC=$$$OK
     Set pOutput = "success"
    Quit tSC
}

 

}

This is the content of the pInput:

----------------------------684516965731952448662577
Content-Disposition: form-data; name="id"; filename="automl.png"
Content-Type: image/png

‰PNG

   
IHDR  €  8   g±V   sRGB ®Îé   gAMA  ± üa       pHYs  t  tÞfx  ÿ¥IDATx^ìÝ    |Uº'~îÜžIß¡ûO;ssïô0Ž×±½Þ¯c÷qci@DA[h\pAŶQYDÐ JØ! ȶd@XÂK€°„H^’7«ÿ§êœ:uj}

.....(binary content)...

----------------------------684516965731952448662577--

Comments

Guillaume Rongier · Nov 4, 2020

Hi Yuri, Have a look at this : https://github.com/grongierisc/iris-csvgen-ui/blob/master/src/CSVGEN/API/Dispatch.cls Here you will find and example of a multi-part upload on a %CSP.REST class.

To get the stream from the multi part you have to do this ligne 39 to 43 :

    // Get stream
    set stream = %request.GetMimeData("id")
    if ('$IsObject(stream) {
      $$$ThrowOnError($$$ERROR(9200,"no file"))
    }

Where id is the name of you multi-part

To send the stream to a business service :

        $$$ThrowOnError(##class(Ens.Director).CreateBusinessService(BsName,.tService))
        
        $$$ThrowOnError(tService.ProcessInput(stream,.output))

Where BsName is the name of your business service in the active production of your namespace. And stream you stream.

0
Yuri Marx  Nov 4, 2020 to Guillaume Rongier

It is because this business service will be a PEX service, so it will are depends with an external rest service, and it limits your reusability. In my service I have the file yet, but I don't know how  to extract file inside the globalbinary variable. But if did not have alternative, I will create de REST service.

0