How to save a text as .txt file via a downloadable link?
I reckon I have found a solution to your problem. Though I can't be entirely sure, it has two causes. The first one lies in the href
attribute of the download link. The problem here is the "
(double quotes) in the to_save
variables data. The html, for what I could test with the by you provided data, renders it as follows:
<a href="data:text/plain;charset=UTF-8,b"UNA:+.? 'UNB+UNOC:3+9978715000006:14+9978715000006:14+200529:1139+50582307060_WP?+_200101_200201++TL'UNH+1+MSCONS:D:04B:UN:2.3'BGM+7+50582307060_WP?+_200101_200201-1+9'DTM+137:202005291139:203'RFF+Z13:13008'NAD+MS+9978715000006::9'CTA+IC+:Michael Jordan'COM+m.jordan@energycortex.com:EM'NAD+MR+9978715000006::9'"" download=filename.txt>Download File</a>
As you can see the value of the href
attribute isn't all in the blue color (here in the stackoverflow code container above). That is because of the "
that interrupt the string, it closes the earlier opened "
after href="
. In order to prevent this behaviour, you should replace the "
in to_save
with "
. To the user this will look the same as "
but the browser will treat it like a normal string.
You should add the following line of code to your python script to make that happen
to_save = abc.serialize().encode("ascii", "ignore")
#add this line:
to_save = to_save.replace('"','"')
Next the download
attribute doesn't have any double quotes around it's value. It should, formally, look like this: download="filename.txt". Then again, for safety replace any possible
"with
"`.
The full python code should now look like this:
reference = 50582307060_WP+_200101_200201
reference = reference.replace('"','"')
to_save = abc.serialize().encode("ascii", "ignore")
to_save = to_save.replace('"','"')
href = f'<a href="data:text/plain;charset=UTF-8,{to_save}" download="{reference}.txt">Download File</a> (right-click and save as {reference}.txt)'
st.markdown(href, unsafe_allow_html=True)
Hope this helps! If not, please comment.
Force download of 'data:text/plain' URL
As of now, it has been made possible to use <a download>
in Chrome. Using dispatchEvent
, you can download any string as file (even with a custom filename) whenever you want. Here's a utility function to use it:
var downloadFile = function(filename, content) {
var blob = new Blob([content]);
var evt = document.createEvent("HTMLEvents");
evt.initEvent("click");
$("<a>", {
download: filename,
href: webkitURL.createObjectURL(blob)
}).get(0).dispatchEvent(evt);
};
Usage:
downloadFile("foo.txt", "bar");
It uses jQuery and the webkit
prefix, but both can be avoided.
How to create downloadable link to text file?
Add the following lines to your .htaccess
file.
<Files "backup.sql">
ForceType applicaton/octet-stream
Header set Content-Disposition attachment
</Files>
Force download of a .txt file with button
If you use Apache as a web server add .htaccess
to the folder with the following content:
<Files *.txt>
ForceType applicaton/octet-stream
</Files>
How to force a Download File prompt instead of displaying it in-browser with HTML?
This is something that you cannot absolutely control with HTML itself.
If the user is having a browser with PDF reading capabilities (or a plugin) and the corresponding settings to open PDF files in-browser, the PDF will open like that.
The PDF opens in a new tab simple because of your target="_blank"
, which has nothing to do with a download prompt.
If you are using HTML5 you can use the download attribute:
<a href="sample.pdf" download="sample.pdf">Download</a>
If you have a back-end service which you can control or you feel like fiddling with your Web Server, you can always look for setting the right Content-Disposition
. See this SO question for some nice discussion on Content-Disposition.
How to force text file to download instead of opening in browser in jsp
Your server needs to add a header to the response:
Content-disposition: attachment
How to download .txt files on hyperlink click?
Answer to your question might be in this StackOverflow article:
how to force a download file prompt instead of displaying it in browser
For non HTML5 approahc you will have to trick the browser by changing the headers (from your app side or directly at server lever) see this article for more details: http://www.tipsandtricks-hq.com/forum/topic/force-a-file-to-download-instead-of-showing-up-in-the-browser
How to download a text file on link click in codeigniter
You can do it like this, it won't redirect you and also works good for larger files.
In your controller "Controller.php"
function downloadFile(){
$yourFile = "Sample-CSV-Format.txt";
$file = @fopen($yourFile, "rb");
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=TheNameYouWant.txt');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($yourFile));
while (!feof($file)) {
print(@fread($file, 1024 * 8));
ob_flush();
flush();
}
}
In your view "view.php"
<a href="<?=base_url("Controller/downloadFile")?>">Download</a>
Related Topics
How to Make a Radio Button Look Like a Toggle Button
Autoplay Audio on Mobile Safari
Safari Anchors on Links Not Working
How to Get a Textarea to Stretch to Fit Its Content Without Using PHP or JavaScript
How to Use Access-Control-Allow-Origin? Does It Just Go in Between The HTML Head Tags
An Url to a Windows Shared Folder
CSS Overflow Hidden with Absolute Position
HTML5 File API Downloading File from Server and Saving It in Sandbox
How to Force Inline Divs to Stay on Same Line
Removing Unwanted Table Cell Borders with CSS
Inserting HTML Tag in The Middle of Arabic Word Breaks Word Connection (Cursive)
Auto Resize Image in CSS Flexbox Layout and Keeping Aspect Ratio
How to Control The Width of a Label Tag