Article
· 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.

Class dc.upload.UploadRESTApp Extends %CSP.REST
{
 
Parameter CHARSET = "utf-8";
 
Parameter CONVERTINPUTSTREAM = 1;
 
Parameter CONTENTTYPE = "application/json";
 
Parameter Version = "1.0.0";
 
Parameter HandleCorsRequest = 1;
 
XData UrlMap [ XMLNamespace = "http://www.intersystems.com/urlmap" ]
{
<Routes>
 
<!-- post image -->
<Route Url="/postFile" Method="POST" Call="PostFile" />
 
</Routes>
}
 
ClassMethod PostFile() As %Status
{
   
    //try to do the actions
    try {
        Set info = {}
        Set source = %request.GetMimeData("file")
        Set destination=##class(%Stream.FileBinary).%New()
        Set destination.Filename="/opt/irisbuild/output/"_source.FileName
        set tSC=destination.CopyFrom(source) //reader open the file
        set result=destination.%Save()
        set info.return = result
        set info.message = "File saved into /opt/irisbuild/output/"_source.FileName
       
        Set %response.ContentType = ..#CONTENTTYPEJSON
        Set %response.Headers("Access-Control-Allow-Origin")="*"
 
        Write info.%ToJSON()
 
        Set tSC=$$$OK
   
    //returns error message to the user
    } catch e {
        Set tSC=e.AsStatus()
        Set pOutput = tSC
    }
 
    Quit tSC
}
 
}
Discussion (0)1
Log in or sign up to continue