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.

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
}

 

}