How can I download an XML file when I call a REST endpoint (which responds in XML format) with AngularJS?
So the flow is simple, I have created a button on the UI which makes a call to a REST endpoint (which has a response in an application/xml format) and its response should come as a download on the UI.
If I understand the question correctly (use the browser's download functionality when the user clicks the link), then this is not a question about angular really.
In order to cause a file to download you need to do two things:
Attempt to navigate the browser to the URL that returns the XML (i.e. don't make an AJAX request for it).
e.g. <a href="http://myserver.com/my/REST/endpoint>Click here</a>
Ensure the XML content is being served with headers that would force a download. If you don't do this, the browser may attempt to render the XML itself rather than downloading it. You could try either setting the Content-Type header to be applicaton/octet-stream or look into using the Content-Disposition header:
Content-Disposition: attachment; filename=someFileName.xml;
Related
I would like to change the file name of a downloaded file from Submit that uses
Content-Disposition: attachment; filename=Reports.csv
I would like to Download that file with a preset file name
Content-Disposition: attachment; filename=AnyFileName.csv
I would need it to be userscript friendly
The Download is Post and the content it returns is
Content-Type: application/octet-stream; charset=UTF-8
I need it to change the filename of the download file
That's not gonna be easy at all. To do this you will have to:
send the form properly using either XMLHttpRequest or fetch API
After sending, retrieve the file data as Blob. You need to use responseType to request Blob
Use URL.createObjectURL to create downloadable data URL
Create link to allow download:
Download
Dispatch a click on that link
Wait a bit (few ms) and then free the memory using URL.revokeObjectURL
I'm sory, it's probably much more work than you expected. Good luck. You may alternatively consider some add-on or program that tampers with the HTML headers for that particular site.
It would be much easier if you could use the download attribute on <form>, but that's not possible.
Is it possible to create a download link for a remote file in plain HTML, or with JavaScript or jQuery?
The download attribute doesn't seem to work for remote files in Chrome 73 or Firefox 66.
<a href="//amazon.com/ads.txt" download>ads.txt</a>
No, the file URL must be on the same domain as the containing document, unless it's a blob: or data: URL:
This attribute only works for same-origin URLs.
Although HTTP(s) URLs need to be in the same-origin, blob: URLs and data: URLs are allowed so that content generated by JavaScript, such as pictures created in an image-editor Web app, can be downloaded.
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a#Attributes
As far as I know it's not possible. I would recommend using some sort of proxy-script on your server to overcome the cross-domains issues, like a simple PHP script. You can check referrer, add custom headers for content disposition etc
You can't do this client side.
The server hosting the resource you want to download can provide a Content-Disposition response header which will trigger a download.
Content-Disposition: attachment; filename=ads.txt;
I have an API server, which can reply to me with different formats of data. I can control this via 'Accept' header.
I need to download .csv file. To do it I must make a GET query with
Accept: 'text/csv'
header
How can I do it?
I know that I can do a XHR query, but in this case I don't know how to save data to file. Or I can do a window.open, but in this case I don't know how to set Accept header (and someone tell that it is impossible).
We are using window.location() to download files from server. We are sending a JSON string along with request. As this being a GET request, I am forced to have restriction on the length of the JSON string. How can I send more data with window.location? sort of POST request to download the file.
Sounds like you want to download a file using POST method..?
One method is to create a hidden form with post method and submit it using javascript. Then the server has to respond back with a content-disposition header to download the file.
If you can't add this header, then you have to send a post request using ajax and then save the response you get back using version client-side technics like: FileSaver or StreamSaver
Is there a way to 'force' the browser to download a file instead of opening that?
Download this file
I've tried the method via js using the window.open("file.txt", "Download");
but no success.
Thx.
Updating:
I've done a php file as follow;
<html>
<a href='dl.php?bid=3'>
<php>
$sql="select barquivo from bibilioteca where bid=$_GET[bid]";
$row=mysql_fetch_assoc(mysql_query($sql));
header("Content-Disposition: attachment;filename=biblioteca/$row[barquivo]");
And it download a file "biblioteca_" with 0 bytes.
You should do this server-side.
If you send a
Content-type: application/octet
or
Content-disposition: attachment; filename=file.txt
header then the user will be prompted to download.
Not at the javascript level. You can have a good deal of control on what the user agent (browser) will attempt to do, by changing the the Mime Type of the content served - that can be done from the web server or server side application.
That means, your ".txt" file is sent to the browser with a
Content-Type: text/plain
http header.
If instead it is served with:
Content-Type: application/octect-stream
http header instead, most likely the user will be prompted to save the file
(regardless of the file name or extension)
Can't be done in pure Javascript as far as I know. You have to send the appropriate headers server side.
If you can use ApacheĀ“s .htaccess settings (much easier) or PHP (more complicated because you'd have to parse txt files through PHP, or introduce a PHP script to pass through the files), you can refer to the accepted answer given here.
It's up to the server to send the appropriate header.
Content-Disposition: attachment;filename=schmoo.mp3
For those looking to d/l files trhough a link heres the best solution
PHP: Force file download and IE, yet again
by cballou