Escrito por

Sales Engineer at InterSystems
Artículo Eduardo Anglada · feb 14, 2022 2m read

Subir un fichero a InterSystems IRIS usando la API REST

Subir un fichero a IRIS empleando la API REST es sencillo. Primero, usando un cliente Postman, enviamos el fichero:

P.S.: Vemos que es un "form" de tipo "multipart" con el nombre "file". Todo esto lo aprendemos viendo la petición http:

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

Después hacemos la API REST para obtener y guardar el fichero:

P.S.: hay que prestar especial atención a que la carpeta de destino tenga los permisos de escritura adecuados.

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
}

 

}