Question
· 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--

Discussion (3)1
Log in or sign up to continue

Hi Yuri,
Have a look at this : https://github.com/grongierisc/iris-csvgen-ui/blob/master/src/CSVGEN/API...
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.