function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
tzuvytzuvy 

How To Download Files From URL?

Hello,

 

I'm trying to write a class that would be schedulled in order to download a file from the internet and attach it to a custom object once a day.

 

The file is a Zip file that contains an XML document inside.

 

Idealy in java I simply use the IO.downloadFileFromHttpsURL(url, null, file); method to save the file into a local machine. IS there a method in SalesForce to do this and save the file downloaded to an instance of a custom object?

 

Thanks

 

Tzuvy

Best Answer chosen by Admin (Salesforce Developers) 
Pradeep_NavatarPradeep_Navatar

To download files from URL use the httprequest class which contains lot of methods.

public class DomDocument
{

// Pass in the URL for the request
 
// For the purposes of this sample,assume that the URL

// returns the XML shown above in the response body


    public void parseResponseDom(String url)

    {

    Http h = new Http();
    HttpRequest req = new HttpRequest();
    // url that returns the XML in the response body

 

        req.setEndpoint(url);

    req.setMethod('GET');

        HttpResponse res = h.send(req);

        Dom.Document doc = res.getBodyDocument();


        //Retrieve the root element for this document.

 
    Dom.XMLNode address = doc.getRootElement();

 

    String name = address.getChildElement('name', null).getText();

        String state = address.getChildElement('state', null).getText();

        // print out specific elements

    System.debug('Name: ' + name);

        System.debug('State: ' + state);

 
    // Alternatively, loop through the child elements.

    // This prints out all the elements of the address

    for(Dom.XMLNode child : address.getChildElements()) {

        System.debug(child.getText());

        }

    }
}

For more information you can visit at this link:

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_xml_dom_document.htm
http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_restful_http_httprequest.htm

 

Hope this helps.

All Answers

Pradeep_NavatarPradeep_Navatar

To download files from URL use the httprequest class which contains lot of methods.

public class DomDocument
{

// Pass in the URL for the request
 
// For the purposes of this sample,assume that the URL

// returns the XML shown above in the response body


    public void parseResponseDom(String url)

    {

    Http h = new Http();
    HttpRequest req = new HttpRequest();
    // url that returns the XML in the response body

 

        req.setEndpoint(url);

    req.setMethod('GET');

        HttpResponse res = h.send(req);

        Dom.Document doc = res.getBodyDocument();


        //Retrieve the root element for this document.

 
    Dom.XMLNode address = doc.getRootElement();

 

    String name = address.getChildElement('name', null).getText();

        String state = address.getChildElement('state', null).getText();

        // print out specific elements

    System.debug('Name: ' + name);

        System.debug('State: ' + state);

 
    // Alternatively, loop through the child elements.

    // This prints out all the elements of the address

    for(Dom.XMLNode child : address.getChildElements()) {

        System.debug(child.getText());

        }

    }
}

For more information you can visit at this link:

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_xml_dom_document.htm
http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_restful_http_httprequest.htm

 

Hope this helps.

This was selected as the best answer
tzuvytzuvy

Thanks this is what I was looking for httprequest and responses.

 

It works perfectly for me.

 

Many thanks

 

Tzuvy

 

PhanidraPhanidra
Its not working for me , when i'm trying above i'm getting html page I dont want that I need to content of dowload files which should be of blob type
Talha SaqibTalha Saqib
Please try the following resource:

https://retrology.net/how-to-create-a-file-from-an-email-attachment-in-salesforce
emma mackeyemma mackey

hi, thanks for giving this information.

you can download direct your files from internet download manager (https://easykeys.org/idm-crack-download/)