question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Fetch API uploads corrupted Excel files to Dropbox

See original GitHub issue

Before you start Have you checked StackOverflow, previous issues, and Dropbox Developer Forums for help? Yes.

What is your question? I’m using a Dropbox SDK JS ("dropbox": "^10.10.0") to upload files via filesGetTemporaryUploadLink, this method returns a temporal URL for file uploading.

The way I upload a file:

const fileUploadResult = await fetch(uploadDestination.url, {
	body: fileData,
	cache: "no-cache",
	credentials: "same-origin",
	headers: {
		"Content-Type": "application/octet-stream"
	},
	method: "POST",
	mode: "cors"
});

When I upload PDFs, everything is working OK, but when I try to upload .xlsx file, this file is uploaded in a corrupted way.

I’ve tried to play with Content-Type, but when I change application/octet-stream to Excel’s MIME (application/vnd.openxmlformats-officedocument.spreadsheetml.sheet) or to binar/octet-stream, I get the error:

Failed to load resource: the server responded with a status of 400 (Bad Request)

That’s quite strange, what’s the difference between sending PDFs and Excel files via POST with Fetch API?

Versions

  • What version of the SDK are you using? "dropbox": "^10.10.0"
  • What version of the language are you using? JS 2021
  • Are you using Javascript or Typescript? JS
  • What platform are you using? (if applicable) Node.js

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
greg-dbcommented, Sep 27, 2021

Thanks! By the way, regarding “And what data does it contain?”, I was actually referring to the corrupted version, in case the contents would serve as a hint as to the source of the issue.

Anyway, I see the fileData you’re passing in is an instance of FormData. That’s likely the issue here; you should only be passing up the raw bytes for the file. The Dropbox servers don’t expect this as form data (multipart or not). (This seems to be supported by the fact that the uploaded data is larger than expected. That is, there’s some additional data being added via the form type.)

You’ll need to update your code to send just the data for the file. I can’t offer help with OpenUI5 itself though, so you may need to refer to its documentation for information on directly accessing the file data.

0reactions
pubmikebcommented, Sep 27, 2021

You’ll need to update your code to send just the data for the file.

Bingo! Sending just a file without wrapping by FormData solved the issue:

The final solution:

const fileData = domRef.files[0];

const fileUploadResult = await fetch(uploadDestination.url, {
	body: fileData,
	cache: "no-cache",
	credentials: "same-origin",
	headers: {
		"Content-Type": "application/octet-stream"
	},
	method: "POST",
	mode: "cors"
});
Read more comments on GitHub >

github_iconTop Results From Across the Web

Fetch API uploads corrupted Excel files to Dropbox
I'm using a Dropbox SDK JS ( "dropbox": "^10.10.0" ) to upload files via filesGetTemporaryUploadLink , this method returns a temporal URL for ......
Read more >
Solved: Corruption of Excel Files saved to Dropbox
We regularily encouter Excel files being saved to Dropbox becoming corrupt. Version history is used is used to recover the last stable ...
Read more >
Excel files get corrupted when uploading or downloading from ...
Excel files get corrupted when uploading or downloading from one drive or Dropbox. Please help! Everytime we save excel files to Dropbox or ......
Read more >
Recover Older Versions of Files - Dropbox Help Center
Follow these steps to quickly restore your files to a previous version. ... Learn how to find or restore missing or corrupted files....
Read more >
Read from Excel Files - UiPath Documentation Portal
Excel.Activities package. This is how the automation process can be built: Open Studio and ... Uploading Your Custom Activity to the Community Repository....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found