Gevorg Arutiunian · Sep 6, 2018 1m read

Save a file using %Net.HttpRequest

This code snippet allows for a file on the web to be saved into the file system. Specify the server and GET request, as well as the directory the file should be saved to. The class method "test" runs the code:

Class objectscript.saveFileHTTP Extends %RegisteredObject
    classmethod test() {
        Set httprequest = ##class(%Net.HttpRequest).%New()
        Set httprequest.Server = ""
        Do httprequest.Get("documentation/cache/20172/pdfs/GJSON.pdf")
        Do $System.OBJ.Dump(httprequest.HttpResponse)
        Set stream=##class(%FileBinaryStream).%New()
        Set stream.Filename="c:\test.pdf"

        Write stream.CopyFrom(httprequest.HttpResponse.Data)
        Write stream.%Save()

Here's a link to the code on GitHub

0 709
Discussion (0)2
Log in or sign up to continue