Javascript JQuery link to download a txt file - javascript

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

Related

Swagger Downloads "text/csv" as *.txt not *.csv

I have a Swagger where I make calls to an Express server hosted locally. The content-type is set to text/csv and the swagger is set up to accept txt/csv, but the file extension when using swagger's "Download" button returns a response*.txt file. I need the download have a .csv extension, how do I go about doing that? Do I make modifications to my swagger or my express, or both?
Express:
res.status(200).header('Content-Type', 'text/csv');
Swagger:
produces:
- text/csv
responses:
'200':
description: OK
text/csv: {}
Note: I don't want to set the Content-Disposition header as attachment; filename="*.csv", since this removes the ability to also display the response body as well as have the option to download.
I would like to both display the CSV response as text and have the ability to download as .csv.
Any help would be appreciated, thank you :D

Change Content-Disposition filename with userscript from a Post

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 make a download link for file on remote server?

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;

Download XML from REST endpoint with AngularJS

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;

Prompting for file download C backend

I have an FPGA that is hosting a website with an html and javascript front end and a C backend (ugh).
Is there anyway to send a file from the C backend to the client? I'm talking to the backend via an html form (since the back end is hosted on an FPGA I'm unsure how it will handle AJAX).
Some tricky points, the website is hosted in read only memory (hence the desire to send the client a file).
I'm going nuts, is this impossible?
No, this is possible. You just need to ensure the relevant HTTP headers are set in the GET response. Specifically Content-type and Content-Disposition. e.g.:
Content-type: application/pdf
Content-Disposition: attachment; filename="downloaded.pdf"
It is certainly possible via CGI, see for example: http://www.cs.tut.fi/~jkorpela/forms/cgic.html

Categories