Written by

Question Vivek Nayak · Dec 31, 2019

How to save file/image at some specific folder.

Hi Team,

I want to save image/file using inter system iris web api.

I am sending file as Base64 formate  in JSON object to api .and I want to save it at D/Images folder.

please refer below code that i was tried.

Obj.OrganizationLogoBase64--> has base64 value of image

Set decode = $System.Encryption.Base64Decode(Obj.OrganizationLogoBase64)

set file = ##class(%Stream.GlobalBinary).%New()

    do file.Write(decode)

Comments

Peter Steiwer · Dec 31, 2019

Hi Vivek,

I would suggest using %Stream.FileBinary and then using the LinkToFile method:

Set file=##class(%Stream.FileBinary).%New()
Set sc=file.LinkToFile("c:\myfile.txt")
Do file.Write(decode)
0
Vivek Nayak  Dec 31, 2019 to Peter Steiwer

Hi Peter,

Thanks but above solution is not working.

Set Obj.OrganizationLogo="abc.png"

Set path="D:\temp\"_OrganizationLogo
    Set file=##class(%Stream.FileBinary).%New()
    Set sc=file.LinkToFile(path)
    Do file.Write(decode)
 
 
 
just for information
I have created web api in intersystem iris and at front end I am using angular.
So, I am passing this image as base64 formate along with the other fields like fname,lname, address etc..

in JSON 

0
Peter Steiwer  Dec 31, 2019 to Vivek Nayak

Have you checked the error codes? Is the file being created?

Set file=##class(%Stream.FileBinary).%New()
Set sc=file.LinkToFile(path)
w $system.Status.GetErrorText(sc)
set sc=file.Write(decode)
w $system.Status.GetErrorText(sc)
0
Vivek Nayak  Dec 31, 2019 to Peter Steiwer

File is not created and also not getting any error message

0
Peter Steiwer  Dec 31, 2019 to Vivek Nayak

Are you testing this through terminal or your web API? If you are testing through your web API you will need to log the status codes instead of writing them

0
Peter Steiwer  Dec 31, 2019 to Vivek Nayak

Also, I am not sure what the rest of the code looks like, but it will also need to be saved:

set sc=file.%Save()
0
Vivek Nayak  Dec 31, 2019 to Peter Steiwer

Thanks Peter,

Now it is working fine..

0
Vivek Nayak  Dec 31, 2019 to Peter Steiwer

Hi Peter

Can you please help me on how to set status when some error occurs for below statement.

$system.Status.GetErrorText(sc)

I have tried below code but it is not working

Set status = $$$OK
Set status =$system.Status.GetErrorText(sc)
If ($$$ISERR(status)){
-- Some error handling
}
else {
-- some success message
}
0
Peter Steiwer  Jan 1, 2020 to Vivek Nayak

You can just check $$$ISERR(sc) directly here, you do not need to run it through $system.Status.GetErrorText(sc)  first.

$$$ISERR(status) will compile into ('status). In the case where sc=1, status will become = "". This means that 'status will evaluate to true when sc=1, which means it will think there is an error when there is not.

USER>set sc=1
 
USER>set status=$system.Status.GetErrorText(sc)
 
USER>zw sc
sc=1
 
USER>zw status
status=""
 
USER>w ('status)
1
USER>w ('sc)
0
0