Download File With Rest
Hello Everyone!
Following the example below i can download file only txt format, in other format(pdf,xls,rar) i have errors(can't open file),
So, how i will change code for download file in other format?
Thanks!
{
#dim %request As %CSP.Request
set filename="E:\Rest\"_file
set %response.ContentType=..GetFileContentType($p(filename,".",*))
do %response.SetHeader("Content-Disposition","attachment;filename="""_$p(filename,"\",*)_"""")
Set %response.NoCharSetConvert=1
Set %response.Expires=50
set file=##class(%File).%New(filename)
do file.Open("R")
do file.OutputToDevice()
Quit $$$OK
}
ClassMethod GetFileContentType(pFileType) As %String
{
if pFileType="txt" quit "text/plain"
if pFileType="rar" quit "application/x-rar-compressed"
if pFileType="pdf" quit "application/pdf"
if pFileType="docx" quit "application/msword"
if pFileType="xls" quit "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
quit ""
}
Hi Sabit,
This code looks familiar, a 5 minute hack from another answer I made
The problem is probably with %File, try replacing the file output with the following code...
If you are still getting a file not found then the file name is probably wrong / corrupt, try logging the file name and opening the file from the command line to be sure.
Sean.
Thank you Sean!