You need to sign in to do that
Don't have an account?
Shubham Deshmukh 15
how to download docx file from url using apex
if it is pdf we can download using following code
Pagereference pg = new Pagereference(url);
Blob b = pg.getContentAsPDF(); //Here you can use getContent() too for another type of file
Attachment att = new Attachment(Name = extractedString, Body = b, ContentType = 'application/pdf', ParentId='a3KDx000000TXxBMAW');
insert (att);
If it is other than pdf like docx, image, recording etc etc. file is being inserted but it has nothing. Thanks in advance any help will be appreciated.
To download a DOCX file from a URL using Apex, you can use the Http class to make a GET request and retrieve the file contents.
Please refer below code : In the code above, the Http class is used to send a GET request to the specified URL. The response is then checked for a successful status code (200) to ensure that the request was successful. If the request is successful, the file contents are retrieved as a Blob using response.getBodyAsBlob(). Then, an Attachment record is created with the appropriate properties and inserted into Salesforce. Make sure to replace 'a3KDx000000TXxBMAW' with the correct ParentId for the record you want to associate the attachment with.
Note that in the example above, the ContentType is set to 'application/vnd.openxmlformats-officedocument.wordprocessingml.document' for a DOCX file. You may need to adjust the content type based on the file type you are downloading.
If this helps , please mark this as Best Answer.
Thank you.
Hello Subrat,
This is not working, it is inserting file but that file nas nothing inside, showing as shown in attached image
In this code, we create an HTTP request with the URL of the DOCX file and perform a GET request to retrieve its contents. If the response status code is 200 (indicating a successful request), we create a new Attachment record with the retrieved body, specify the correct content type (application/vnd.openxmlformats-officedocument.wordprocessingml.document for DOCX files), and set the appropriate parent record ID. Finally, we insert the Attachment record. for more infor: azure development course (https://www.igmguru.com/cloud-computing/microsoft-azure-developer-az-203-certification-training/)
Make sure to replace 'https://example.com/path/to/file.docx' with the actual URL of the DOCX file you want to download, and 'a3KDx000000TXxBMAW' with the ID of the parent record where you want to attach the file.