Question
· Oct 29, 2018

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! 

ClassMethod GetFile(file) As %String
{
#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 ""
}

Discussion (3)0
Log in or sign up to continue

Hi Sabit,

This code looks familiar, a 5 minute hack from another answer I made blush

The problem is probably with %File, try replacing the file output with the following code...


set file=##class(%FileBinaryStream).%New()
set file.Filename=filename
quit file.OutputToDevice()

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.