DownloadDocumentController
@RestController
@RequiredArgsConstructor
public class DownloadDocumentController {
private static final String APPLICATION_MS_WORD_VALUE = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
private final DownloadDocumentService downloadDocumentService;
@PostMapping(value = "/download", produces = APPLICATION_MS_WORD_VALUE)
public ResponseEntity<byte[]> downloadFile(@RequestBody String pathFile) throws IOException {
byte[] content = downloadDocumentService.downloadFile(pathFile);
return ResponseEntity.ok()
.contentLength(content.length)
.header(HttpHeaders.CONTENT_TYPE, APPLICATION_MS_WORD_VALUE)
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=File.docx"))
.body(content);
}
}
DownloadDocumentService
@Service
public class DownloadDocumentService {
public byte[] downloadFile(String pathFile) {
// TODO do read available resource and create byte[]
return null;
}
}
Answer from Oleg Cherednik on Stack OverflowOracle
docs.oracle.com › en › cloud › paas › content-cloud › rest-api-documents › op-documents-api-1.2-files-fileid-data-get.html
REST API for Documents - Download File
November 27, 2023 - File ID is not found. Requested range cannot be satisfied. Back to Top · The following example downloads version 2 of the specified file.
IBM
ibm.com › docs › en › blueworks-live
FileDownload API
Use this resource to download a file that is attached to a process, an instance, or a comment.
Videos
23:52
FastAPI Tutorial - Uploading File and Downloading File From API ...
10:35
Web Api Download Multiple Files - YouTube
29:04
File Upload and Download with Spring Boot - REST API - YouTube
Spring Boot File Upload and Download REST API Examples
Angular File Download From API | Download File Using Angular - YouTube
File Download API in Spring Boot (Step-by-Step Guide) – Part ...
Sugarsync
sugarsync.com › dev › download-file-example.html
SugarSync for Developers-API Examples: Downloading a File
Notice that handleDownloadCommand() method uses the getNodeValues() method in the XmlUtil utility class to extract the displayName and fileData nodes for the files listed in the message response body. The getNodeValues() method uses XPath expressions to identify the targeted nodes in the XML response body. The fileData node contains the link to the data resource for the file and will be used in the download request.
Medium
medium.com › @khushbooverma8319 › download-api-file-in-frontend-91bd51e4ee19
Download file in frontend getting from API | by Khushbooverma | Medium
May 22, 2023 - Suppose we have a button on click of which we want to download the CSV file. We have a onClick handler named handlDownload as shown below. const handleDownload = () => { setLoading(true); API.get('https://sample/api', { responseType: "blob", }) .then((response) => { const url = window.URL.createObjectURL(new Blob([response.data])); console.log(url); var blob = new Blob([response.data], { type: "text/plain;charset=utf-8", }); saveAs(blob, `${response.fileName}.pdf`); setLoading(false); }) .catch((err) => { console.log(err); setLoading(false); }); }; This method calls the API and expects a blob in response.
Postman
postman.com › api-evangelist › box › request › vesg2u5 › download-file
Download file | Box Platform API
These are APIs that have been defined by the API Evangelist, providing ready to go collections for many of the popular, as well as the interesting long tail of of the API sector. These APIs were created by the API Evangelist team and do not necessarily reflect the views of the API providers ...
Box
developer.box.com › guides › downloads › file
Download File - Box Dev Docs
To download a file, pass the the ID of the file to get the content for. ... curl -i -L -X GET "https://api.box.com/2.0/files/12345/content" \ -H "authorization: Bearer <ACCESS_TOKEN>"
Databricks Documentation
docs.databricks.com › api › workspace › files › download
Download a file | Files API | REST API reference
Introduction page for Databricks Workspace REST API reference
Top answer 1 of 2
1
The issue was with the axios instance.
I have added response type to the request header, the issue was solved.
responseType: "blob",
this was the root cause. After that I was able to download the file with response provided by 'Klian' in the comments to my question.
2 of 2
0
You can try using the FileSaver.js library to download the file.
Here is the sample code:
import FileSaver from 'file-saver';
// Call your API to get the response
axios.get('/PrintWorkSheet', { responseType: 'arraybuffer' }).then((response) =>
{
//Create a Blob object from the response data
const blob = new Blob([response.data], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' });
// Use FileSaver.js to save the Blob as a file
FileSaver.saveAs(blob, 'WorkSheet.xlsx');
})
.catch((error) => {
console.log(error);
});
CodeJava
codejava.net › frameworks › spring-boot › file-download-upload-rest-api-examples
Spring Boot File Download and Upload REST API Examples
November 16, 2023 - You can click Save Response > Save to a file to store the file on disk:Those are some code examples about File upload API and File download API implemented in Java and Spring framework. You can get the sample project code from this GitHub repo.To see the coding in action, I recommend you watch ...
Appsmith
docs.appsmith.com › how-to guides › download files
Download Files | Appsmith
Set the Button widget's onClick event to download all the files using the JS Object created in Step 2. Example: In the following example, fetch_files is the API query to fetch file data and bulk_download is the name of the JS Object.
CodeBurst
codeburst.io › download-files-using-web-api-ae1d1025f0a9
Download Files using Web API. How to return a file from an API… | by Changhui Xu | codeburst
February 24, 2021 - In this article, I will use a demo Web API application in ASP.NET Core to show you how to transmit files through an API endpoint. In the final HTML page, end users can left-click a hyperlink to download the file or right-click the link to choose “Save Link As” in the context menu and save the file.
M-files
developer.m-files.com › Samples-And-Libraries › Samples › REST-API
REST API Samples
The items linked below are designed as teaching tools and may not be fully complete. M-Files has published a number of public samples within our MFilesSamplesAndLibraries GitHub repository, which focus on achieving specific tasks using our public APIs or Frameworks.
Zoho
zoho.com › creator › help › api › v2.1 › download-file.html
Download File - API v2.1 | Zoho Creator Help
Copiedimport requests api_headers = { "Authorization": "Zoho-oauthtoken 1000.8cb99dxxxxxxxxxxxxx9be93.9b8xxxxxxxxxxxxxxxf" } try: response = requests.get("https://www.zohoapis.com/creator/v2.1/data/jason18/zylker-store/report/Inventory_Report/3888834000000114050/Product_Manual/download", headers=api_headers) except: print("Exception while making the API request.") This sample request will download the file present in the Product_Manual field of the record with ID 3888834000000114050, which is displayed in the Inventory Report of the Zylker Store application.
CodeSignal
codesignal.com › learn › courses › efficient-api-interactions-with-java › lessons › downloading-files-from-an-api-using-javas-httpclient
Downloading Files from an API Using Java's HttpClient
Here's a basic example of downloading a file named welcome.txt from our API at http://localhost:8000/notes. This approach downloads the entire file at once, which is manageable for smaller files. This code sends a GET request and writes the response content to a local file.
Top answer 1 of 5
2
I'm executing python script from the command line. · I may be missing some steps below but the general way to install python is as follows. · 1. Install python · https://phoenixnap.com/kb/how-to-install-python-3-windows · 2. Create subdirectory · mkdir servicenowapis · 3. Move to the subdirectory · cd servicenowapis · 4. Create a virtualenv · python-m venv venv · 5. Activate venv · venv\Scripts\activate.bat · 6. Install request module · pip install requests · 7. Copy the script I've provided to the directory · 8. Copy and paste content of Constants.py over import statements · 9. Edit username, password · 9. Edit table_api.py to use Incident number · e.g. · get_file_attachment('INC0000002') · 10. Execute command · python test_api.py · · import requests · SERVICENOW_URL = 'https://.service-now.com' · SERVICENOW_USER = '' · SERVICENOW_PWD = '' · DOWNLOAD_DIR = './downloads/' · REQUEST_HEADER = {"json": {"Content-Type": "application/xml", "Accept": "application/json"}, · "png": {"Content-Type": "application/xml", "Accept": "application/png"}, · "xml": {"Content-Type": "application/xml", "Accept": "application/xml"}, · } · ATTACHMENT_API = '/api/now/attachment' · TABLE_API = '/api/now/table' · # TABLE_NAME = 'kb_knowledge' · TABLE_NAME = 'incident' · def get_table_data(table_name, param): · url = SERVICENOW_URL + TABLE_API + '/' + table_name + '?' + param · response = requests.get(url, auth=(SERVICENOW_USER, SERVICENOW_PWD), headers=REQUEST_HEADER.get('json')) · if response.status_code != 200: # if error, then exit · print('Status:', response.status_code, 'Headers:', response.headers, 'Error Response:', response.json()) · exit() · return response.json() · def get_attachment_info(sys_id): · url = SERVICENOW_URL + ATTACHMENT_API + '/' + sys_id · response = requests.get(url, auth=(SERVICENOW_USER, SERVICENOW_PWD), headers=REQUEST_HEADER.get('json')) · if response.status_code != 200: # if error, then exit · print(f'Status: {response.status_code}, Headers:{response.headers}') · exit() · return response.json() · def get_attachment(att_sys_id, file_type, download_dir): · url = SERVICENOW_URL + '/' + ATTACHMENT_API + '/' + att_sys_id + '/file' · response = requests.get(url, auth=(SERVICENOW_USER, SERVICENOW_PWD), headers=REQUEST_HEADER.get(file_type)) · if response.status_code != 200: # if error, then exit · print('Status:', response.status_code, 'Headers:', response.headers, 'Error Response:', response.json()) · exit() · with open(download_dir, 'wb') as f: · for chunk in response: · f.write(chunk) · def get_file_attachment(record_number): · param = 'sysparm_query=number=' + record_number + '&sysparam_limit=1' · file_info = get_table_data(TABLE_NAME, param).get('result') · if len(file_info) < 1: · print(f'There is no attachment to sys_id:{record_number}') · return · attachment_sys_id = file_info[0].get('sys_id') · param = 'sysparm_query=table_sys_id=' + attachment_sys_id + '&sysparam_limit=1' · kb_attachments = get_table_data('sys_attachment', param) · result = kb_attachments.get('result') · for attach_file in result: · attach_sys_id = attach_file.get('sys_id') · content_type = attach_file.get('content_type').split('/') · file_ext = content_type[1] · file_name = attach_file.get('file_name') · get_attachment(attach_sys_id, file_ext, DOWNLOAD_DIR + file_name) · if __name__ == '__main__': · # get_file_attachment('KB0010062') # knowledge base number · get_file_attachment('INC0000002') · View solution in original post
2 of 5
0
Hi, · please find my points below · APIs are only used to retrieve the data from the instance. It will not download the attachment directly, using API you can get the url of the attachment and when you clicked on the URL it will download the file once you give the credentials.Otherwise use the binary code returned by the API to convert into the attachment file and download into the system from where this API has been called. · RegardsAnkur · Regards,Ankur✨Certified Technical Architect || 10x ServiceNow MVP ServiceNow Community Leader
Laserfiche Answers
answers.laserfiche.com › questions › 213845 › How-do-I-download-a-document-using-API
How do I download a document using API? - Laserfiche Answers
November 29, 2023 - With the document id, you could then call the export api https://api.laserfiche.com/repository/swagger/index.html?urls.primaryName=v2#/Entries/ExportEntry to download the part you need. Here's the example code for download Edoc part of a document in our sample code https://github.com/Laser...
CodeSignal
codesignal.com › learn › courses › efficient-api-interactions-with-scala › lessons › downloading-files-from-an-api-using-requests-scala
Downloading Files from an API | CodeSignal Learn
When you send a GET request using ... an HTTP status code (like 200 OK). Here's a basic example of downloading a file named welcome.txt from our API at http://localhost:8000/notes....