Written by

Software Architect at Visum
Article Yuri Marx · Jan 30, 2022 2m read

Upload into a InterSystems IRIS REST API

If you need create a upload REST API with IRIS is very simple. Do these procedures:

From Postman client you send a file

P.S.: It is a multipart form with a file type using "file" in the name. The request type is form/multipart. See the http request:

POST /image-analyzer/postFile HTTP/1.1
Host: localhost:52773
Content-Length: 213
Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW

----WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="file"; filename="/C:/Users/yurim/OneDrive/Imagens/salesschema.png"
Content-Type: image/png

(data)
----WebKitFormBoundary7MA4YWxkTrZu0gW

Do a REST API backend to get the file and save (upload):

P.S.: pay attention if the destination folder has write permissions.

Classdc.upload.UploadRESTAppExtends%CSP.REST
{

 

ParameterCHARSET="utf-8";

 

ParameterCONVERTINPUTSTREAM=1;

 

ParameterCONTENTTYPE="application/json";

 

ParameterVersion="1.0.0";

 

ParameterHandleCorsRequest=1;

 

XDataUrlMap[XMLNamespace="http://www.intersystems.com/urlmap"]
{
<Routes>

 

<!--postimage-->
<RouteUrl="/postFile"Method="POST"Call="PostFile"/>

 

</Routes>
}

 

ClassMethodPostFile()As%Status
{
   
    //try to do the actions
    try{
        Setinfo={}
        Setsource=%request.GetMimeData("file")
        Setdestination=##class(%Stream.FileBinary).%New()
        Setdestination.Filename="/opt/irisbuild/output/"_source.FileName
        settSC=destination.CopyFrom(source)//reader open the file
        setresult=destination.%Save()
        setinfo.return=result
        setinfo.message="File saved into /opt/irisbuild/output/"_source.FileName
       
        Set%response.ContentType=..#CONTENTTYPEJSON
        Set%response.Headers("Access-Control-Allow-Origin")="*"

 

        Writeinfo.%ToJSON()

 

        SettSC=$$$OK
   
    //returns error message to the user
    }catche{
        SettSC=e.AsStatus()
        SetpOutput=tSC
    }

 

    QuittSC
}

 

}