I am writing a web based files administrator. How can I have an html file be downloaded when its link is clicked instead of displayed.
On server side, when serving file, add this header:
Content-Disposition: attachment; filename="document.html"
For examle in PHP you'd do:
header('Content-Disposition: attachment; filename="document.html"');
In the response headers, set Content-Disposition to "attachment; filename=something.html"
Set the Content-Type header on your HTTP response to 'application/octet-stream'.
In ASP.net/C#
Response.ContentType = "application/octet-stream";
The server would need to return a different MIME Type when that file is requested such as application/octet-stream.
Related
I want to upload a PDF file but can't seem to serialize it correctly. Because of specific requirements I cannot use the FormDataAPI nor an HTML form.
I'm manually creating the multipart/form-data body like so.
------WebKitFormBoundaryFb3Biw9LyOW5jlUQ
Content-Disposition: form-data; name="username"
bill
------WebKitFormBoundaryFb3Biw9LyOW5jlUQ
Content-Disposition: form-data; name="file"; filename="test.pdf"
Content-Type: application/pdf
[object ArrayBuffer]
------WebKitFormBoundaryFb3Biw9LyOW5jlUQ--`
and sending the request via xhr.send(body) where body is my built up string. Is this even possible?
When I've tested using the FormDataAPI I can see the file as a nice big (encoded?) blob in the Chrome network request inspector. Is FormDataAPI doing something that I cannot achieve via JS?
I have headers which force a file download, rather than viewing it in the browser but I need to give that option. I've looked at several posts on Stack Overflow which answer this question (eg How to force files to open in browser instead of download (pdf)?) but I am still not getting the desired result, so I wanted to check if I'm missing something.
Here are my headers to force a download
$file = 'file.pdf';
$filePath = 'path/to/file.pdf';
header("Cache-Control: public");
header("Content-Length: 20000");
header("Content-Description: File Transfer");
header("Content-Type:file");
header('Content-Disposition: attachment; filename="$file"');
header("Content-Transfer-Encoding: binary");
readfile($filePath);
And here are the header to try and display the file in the browser
header("Cache-Control: public");
header("Content-Length: 20000");
header("Content-Description: File Transfer");
header("Content-Type:file");
header('Content-Disposition:inline; filename="$file"');
header("Content-Transfer-Encoding: binary");
readfile($filePath);
So the only change is the content-disposition header.
However, I am still just being shown the markup in the browser (see image).
Any ideas?
Many thanks
You are still using inline for content-disposition when you want to instruct browser to perform a download. Try use attachment instead.
more detais: Content-Disposition
HTML5 introduced a nice feature for marking <a> links as download endpoints, by simply adding download attribute to the <a> tag (see description).
Is it possible to do the same for HTML forms?
Here is a simple use case for example: I have a form that requests the user for some details, and after the user submits the form the server should return a file according to these details.
This is not possible.
According to the specification is the "download" attribute only specified for a and area.
http://www.w3.org/html/wg/drafts/html/master/links.html#downloading-resources
No, form doesn't have a download attribute, so it is not possible to have that exact behavior with a form.
You can set the output file name through a post though, by setting the Content-Disposition HTTP header:
Content-Disposition: attachment; filename="yourPicture.png"
Yes after submitting data you can return file in form of pdf or else.
By using header function
<?php
//create pdf with details
.....
.
.
// We'll be outputting a PDF
header('Content-type: application/pdf');
// It will be called downloaded.pdf
header('Content-Disposition: attachment; filename="downloaded.pdf"');
?>
But this is not possible using download attribute
In my website I stream users mp4 content. I also allow users to download.
However in Chrome it seems to automatically play the file in an internal player instead of downloading the file.
How do I force the browser to download the file instead.
Regards and thanks
Craig
You have to use the HTTP header "Content-Disposition" and 'Content-Type: application/force-download' which will force browser to download the content instead of displaying it there.
Depending upon the server side language you are having the implementation differs. In case of
PHP:
header('Content-Disposition: attachment; filename="'.$nameOfFile.'"');
will do the job for you.
Ofcourse to simplify and generalize this for all your files, you may need to write a method which will route a link to downloadable content.
The link you can show in the html will be like:
Click here to Download Hello.mp4
And in the server side, you need a script which is being called on /downloadFile (depending on your routing), get the file by id and send it to user as an attachment.
<?php
$fileId = $_POST['id'];
// so for url http://yoursite.com/downloadFile?id=1234 will download file
// /pathToVideoFolder/1234.mp4
$filePath = "/pathToVideoFolder/".$fileId."mp4";
$fileName = $fileId."mp4"; //or a name from database like getFilenameForID($id)
//Assume that $filename and $filePath are correclty set.
header('Content-Description: File Transfer');
header('Content-Disposition: attachment; filename="'.$filename.'"');
header('Content-Type: application/force-download');
readfile($filePath);
Here 'Content-Type: application/force-download' will force the browser to show the download option no matter what's the default setting is for a mime-type.
No matter what your server side technology is, the headers to look out for are:
'Content-Description: File Transfer'
'Content-Type: application/force-download'
'Content-Disposition: attachment; filename="myfile.mp4"
Sounds like you are using a direct href to the mp4. If you are using any server side languages (i.e.asp.net, php, etc) language on your website you can force a download. In asp or .net you can use HttpHandlers with "content-disposition","attachment; filename=fname.ext"
or return File() ActionResult in MVC. Let me know if you can use any server side code and I can provide some code.
Alternatively you can try the html5 download attribute: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a?redirectlocale=en-US&redirectslug=HTML%2FElement%2Fa#attr-download
i.e. <a href="http://www.w3schools.com/images/myw3schoolsimage.jpg" download="downloadfilename">
Or, try javascript/jQuery. Here is a plugin: http://johnculviner.com/jquery-file-download-plugin-for-ajax-like-feature-rich-file-downloads/
Setting the Content-Disposition header should fix it.
Content-Disposition: attachment; filename=whatever.mp4;
Either in the server settings or in the preprocessing of the page.
if you want a cross browser solution
you need a server-side code to download the file
example:
I am working on jsp technology, if you can use jsp in your website you can try the following code in the file download.jsp:
<%# page import="java.io.*, java.lang.*, java.util.*" %>
<%
String filename=request.getParameter("filename");
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition",
"attachment;filename="+filename);
%>
<%
/*
File file = new File(filepath+filename );*/
String path = getServletContext().getRealPath("/mp4/"+filename);
File file = new File(path);
FileInputStream fileIn = new FileInputStream(file);
ServletOutputStream out1 = response.getOutputStream();
byte[] outputByte = new byte[4096];
//copy binary contect to output stream
while(fileIn.read(outputByte, 0, 4096) != -1)
{
out1.write(outputByte, 0, 4096);
}
fileIn.close();
out1.flush();
out1.close();
%>
you can put the code above in a file: download.jsp
then in your page links you will use it like:
song1
with my best wishes to you
You can get it done in a couple of ways. I'm not sure you use IIS or apache and which server side language you are using, but the techniques are similar for all.
You can add the MIME type application/octect-stream to the extension .mp4 in your IIS or apache, sothat all files with extension .mp4 will be shown with a download prompt. This is the most easy and sure fire way of showing the "download" prompt.
Plz see the example below.
http://www.codingstaff.com/learning-center/other/how-to-add-mime-types-to-your-server
In the above example, instead of setting video/mp4 fpr .mp4 extensions, change it to application/octect-stream
Also, the same can be done via server side code as well, (PHP code). The code will be similar with ASP.NET also,please google for "force file download"
$file_url = 'http://www.myremoteserver.com/file.mp4';
header('Content-Type: application/octet-stream');
header("Content-Transfer-Encoding: Binary");
header("Content-disposition: attachment; filename=\"" . basename($file_url) . "\"");
readfile($file_url);
Why not simply use download attribute? Today is the simplest way.
<a href="/images/myw3schoolsimage.jpg" download>
See more here.
My application generates a dynamic link to any PDF files that are associated with a product. The link is presented like this:
Brochure
If the user right-clicks and selects "Download Linked File As" (or its equivalent), the file is presented with a ".pdf.png" extension in Google Chrome and Safari. Firefox works appropriately, not sure about Internet Explorer.
I want Firefox and Chrome to know that it is a PDF. Because obviously users are going to try to download these, they are going to save it with the wrong extension, and they won't be able to open the file.
Assuming you are using "send_data" from within a rails controller to serve the file might I suggest:
send_data(
data,
:filename => "filename.pdf",
:disposition => "attachment",
:type => 'application/pdf'
)
Where "data" is the contents of the PDF.
For more information checkout the following link:
http://api.rubyonrails.org/classes/ActionController/DataStreaming.html#method-i-send_data
send proper headers in some scripting lang like php or user .htaccess
<Files *.pdf>
ForceType application/pdf
Header set Content-Disposition attachment
</Files>
Have you heard of the content-disposition header? It allows you to tell the browser to ask the user what to do with the file, rather than try and handle it by itself. I don't think it is part of the HTTP spec, but it is documented by the IETF under RFC 2183.
You should be able to use whatever language you are using to alter the HTTP headers before they go to the client. The header you add will look something like this:
Content-Disposition: attachment; filename=filename.pdf
You might also need a Content-Type header:
Content-Type: application/pdf