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.

Force downolad a file

See original GitHub issue

I’m sending a request that send back a file to download with Content-Disposition:attachment;, however vue-resource is getting it as a string and is not downloading the file.

Any idea how can i force download the file instead?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:9

github_iconTop GitHub Comments

17reactions
airtonixcommented, Sep 9, 2016

Found this: http://stackoverflow.com/a/35851873

$http.post(
  'url',
  {},
  {responseType: 'arraybuffer'}
).then(function (response) {
  var headers = response.headers();
  var blob = new Blob([response.data],{type:headers['content-type']});
  var link = document.createElement('a');
  link.href = window.URL.createObjectURL(blob);
  link.download = "Filename";
  link.click();
});

so three things:

  1. As a developer for your proejct, you’ve chosen to use vue-resource. Have you defined for your client and had them sign off on a list of browser you aim to support?
  2. is Blob supported in that list of browsers?
  3. most importantly, does vue-resource allow us to set responseType?
12reactions
Kam1nicommented, Jan 19, 2018

This solution works for me without any form of file corruption.

	async download(){
		let response = await Vue.http.get("file/download/" + this._id, {responseType: 'arraybuffer'});
		let blob = new Blob([response.data], {type:response.headers.get('content-type')});
		let link = document.createElement('a');
		link.href = window.URL.createObjectURL(blob);
		link.download = this.name;
		link.click();
	}

The responseType is very important. Otherwise only txt files will work. By setting responseType to arrayBuffer, all file types are converted correctly.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to force a file to download instead of open in the browser ...
One of the questions I get asked relatively often is how to force a file to download instead of opening directly in the...
Read more >
6 Ways to Force Download Instead of Opening File in Browser
Check out these 6 simple and straightforward ways to force downloading WordPress files instead of opening in the browser here!
Read more >
Force a File to Download (PDF, Image, Audio) Rather than ...
Encapsulating your downloadable items in a zip file will ensure they are forced to download. This is a simple technique that forces the...
Read more >
How to force a file to be downloaded | Media Temple Community
To expand force downloads to include other extensions, the following code can be added to the .htaccess file: The code below will force...
Read more >
Force to open "Save As..." popup open at text link click for PDF ...
A very easy way to do this, if you need to force download for a single link on your page, ...
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