Correctly Downloading JSON Response As A File - javascript

I am getting json response against an endpoint. I need to be able to download that as a json file. Which I am able to do. This downloaded json file has to be uploaded against some other endpoint.
PROBLEM
I have tested both the download & upload API's using Postman. File downloaded using Postman when uploaded gives me the expected response. Which means API is working fine.
source.js
const filename = response.headers["content-disposition"].split(
"filename="
)[1];
const data = filename.endsWith(".json")
? JSON.stringify(response.data)
: response.data;
const url = window.URL.createObjectURL(new Blob([data]));
const link = document.createElement("a");
link.href = url;
link.setAttribute("download", filename);
document.body.appendChild(link);
link.click();
link.parentNode.removeChild(link);
But the file downloaded using the front-end is when uploaded doesn't work as expected. Which means that this method of downloading the file is somehow not downloading it correctly.
Am I downloading it correctly?

Related

How can I download an excel file with sheetjs that the backend sends me?

I have been working with sheetjs for several days and I cannot download the file that it sends me, the library cannot process it and it blocks my entire page. What should I do?
this is the file that the backend sends me
this is the code i was using
axios.toExcel(datos).then((res)=>{
const blob = new Blob([res])
let a = document.createElement('a')
a.href = URL.createObjectURL(blob)
a.download = 'demo.xlsx'
a.click()
})
How can I download an excel file with sheetjs that the backend sends me?

Download blob response from an API as excel in react js

I have a scenario where the backend api(SPRING BOOT) is returning response as an excel file and i have to download it.
I have been trying to save the file using File saver library but could not reach the solution.
please help me on this
const blob = new Blob([response.data], {type:'application/vnd.openxmlformats-
officedocument.spreadsheetml.sheet'})
console.log(blob)
[![this is the response i am getting from api][1]][1]FileSaver.saveAs(blob, "tickets.xlsx")

Issue getting google workspace file (JavaScript)

Im calling request from google workspace file with gapi using this code
gapi.client.drive.files.export({
fileId: fileId,
mimeType: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
})
from that code i get response back of base64 string to convert as a blob but when i open file in google docs or msword file is corrupt and not working but if i change mimeType into application/pdf it working
same thing also happen in if i request file with application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
am i doing something wrong here or i there is other way to getting file workspace
md5Checksum is not available
As mentioned in the official documentation for files.get:
Response
By default, this responds with a Files resource in the response body. If you provide the URL parameter alt=media, then the response includes the file contents in the response body. Downloading content with alt=media only works if the file is stored in Drive. To download Google Docs, Sheets, and Slides use files.export instead. For further information on downloading files, refer to Download files.
Finally i found solution for this issue according to this official documentation we have two type we can download from google drive.
here my code how i request for file
const res = await drive
.files
.export({
fileId: fileId,
mimeType: yourMimeTarget
})
.then(res => res)
const blob_file = fetch(`data:${yourMimeTarget};base64,${btoa(res.body)}`)
.then(res => res.blob())
so after you fetch google workspace file you will get base64 string than you can convert it into blob

I want to post two files to a web service using python request

I want to upload two files to a web service, one CSV file with metadata inside and one tiff image. The CSV file and the tiff files are are a pair, because the CSV file contains metadata for the tiff file. So they always need to be uploaded together
I have the javascript working on an uploading server
var xhr = new XMLHttpRequest();
var formData = new FormData();
xhr.open('POST', '/GigaTEMUpload/UploadFiles', true);
xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
files.forEach(function (file, i) {
formData.append('uploadFiles[' + i + ']', file);
});
xhr.send(formData);
but if I want to use python request to do the same post job how to do it through python?
Thanks
I am using python from a server running spyder to upload two files to a web. And once they are uploaded the tiff image would be viewable on the web.
It's a pretty normal outbound request. Here is a similar question for your reference,
Send file using POST from a Python script

Corrupt XLSX file after downloading

I'm trying to download a xlsx file which is generated in the java backend from the angular frontend and I'm getting file as an attachment with Content-Disposition header and I'm able to download the file with the below js code, but when I try to open it, it's always corrupted
var data = response; //from server
var blob = new Blob([data], { type:"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml;charset=UTF-8"});
var link = document.createElement('a');
link.href = URL.createObjectURL(blob);
link.download = 'filname.xlsx';
link.click();
If I console log the server response, here is what I found
Edit:
When I try to open the downloaded file, see below image
Can someone help me on this?
I had this same issue with AngularJS 1.6.1. When I called my HTTP GET service from the browser or from window.open("url"), the xlsx file was downloaded perfectly: 304628 bytes. However, the response.data provided by AngularJS instead had 289414 bytes and the Blob wrapper had 550963 bytes, which is what is downloaded as a corrupted file. This same behavior occurred if I return the xlsx in a zip.
I solved this by setting the XMLHttpRequest.responseType property as such:
$http.get(url, {responseType:'arraybuffer'});

Categories