How can I make a video file available for download? - html

I am trying to offer a download option of videos on my site.
I have direct links (which have .mp4/.webm ending) available for download (they are not hosted on my server if that matters).
This is what I tried:
<a href="http://stream.flowplayer.org/bauhaus/624x260.webm" download>Download</a>
It only works on chrome, in FireFox it will just open the video on the browser itself.

You need a wrapper script which sets the Content-Type Content-Disposition headers appropriately, and outputs the file you want to serve.
In PHP this would be done like this:
Filename: 624x260.php
<?php
// We'll be outputting a webm video
header('Content-type: video/webm');
// It will be called downloaded.webm
header('Content-Disposition: attachment; filename="download.webm"');
readfile('624x260.webm');
?>
You would then link to the PHP file instead, as follows:
Download

if you happen to have an apache server where you can edit the .htaccess file, add this line.
AddType application/octet-stream .webm
If you wish to not do this, you could do this through php as well.
PHP Code:
<?php
$file = $_GET['file'];
header ("Content-type: octet/stream");
header ("Content-disposition: attachment; filename=".$file.";");
header("Content-Length: ".filesize($file));
readfile($file);
exit;
?>
HTML Code:
Download the mp3

HTML5 adds the download attribute, which you have there in your example, but is empty. Add a value to the download attribute and hey presto.
ie change download to download="624x260.webm" in your a tag.
http://updates.html5rocks.com/2011/08/Downloading-resources-in-HTML5-a-download
For non-HTML5 compliant browsers, the most straightforward way would be to accompany the links with a direction to 'right click to download'. This would cover the majority of cases and browsers.
An overview of a couple of techniques here, I realise you can't zip the files.
http://webdesign.about.com/od/beginningtutorials/ht/download_link.htm
There are multitude more involved ways to do this, including modifying the web server config, but not everyone has the access / know-how to do that.

Note: The download attribute is supported in Chrome 14+ and Firefox 20+.
As an alternative for other browsers, you can use jQuery plugin from here
http://johnculviner.com/jquery-file-download-plugin-for-ajax-like-feature-rich-file-downloads/4
OR
http://jqueryfiledownload.apphb.com/
You can make it downloadable like
Download
$(document).on("click", "a.download", function () {
$.fileDownload($(this).prop('href'))
.done(function () { alert('File download a success!'); })
.fail(function () { alert('File download failed!'); });
return false;
});

Related

Why .txt file in <iframe> is getting downloaded instead of displayed?

I am using an <iframe> to display a text file:
<div class="document-view">
<img src="img/302.GIF" />
</div>
$(window).load(function () {
<s:if test="extention.equalsIgnoreCase('txt')">
element = '<iframe class="iframe" src="/dmsrepo/<s:property value="docLocation"/>" />';
</s:if>
$('.document-view').html(element);
});
When I inspect element in the browser I can see the file location.
<iframe class="iframe" src="/dmsrepo/Legal Doc Type/LegalDocType_123456789_1.0.txt" />
But the text file is getting downloaded in Chrome, Firefox and IE.
How to resolve this issue?
EDIT: you can reproduce the behavior in the following fiddle, that strangely affects only Firefox, for every page load after the first one.
Simply open the page, then press Run.
Note: it affects also the first load if Firebug Net module is activated.
This is the issue with the file. it is not formatted as html. because it has some special unicode (eg) characters at end of file read. if text response has special character like that. it failed to parse response as html embedded in iframe.
you can check this example :
<iframe src='http://humanstxt.org/humans.txt' /> </iframe>
because browser understands only html. Change your file extension to .html
or use server side language like php and using file_get_contents() function, you can display text file to browser.
to display file add below line(s) in .htaccess
AddType text/plain .txt
AddType text/plain .log
This is signal Firefox that you don't have to download the file instead view file as plain text.This technique can be applied to all the files that you need to view instead off download.
The Content-Disposition response header could be the culprit. If set to attachment; filename="..." then the file will download. If set to inline then it can displayed in the Web page.
If you are serving these files from Amazon S3, you can add Metadata to the file with the Key=Content-Disposition and the Value=inline.
Note: the default value is typically inline.

HTML5 download attribution not working for file without extension

I´m trying to use the 'download' attribute of a href to rename a file without extension, but it doesn't work.
<a href="/fileWithoutExtension" download="newName.pdf">
I tried with IE, Chrome and FF.
Any thoughts?
Thanks,
The download attribute only works in Chrome 14+ and Firefox 20+ so this May explain why you are not getting this to work. I suggest you do not use it as it is not yet widely used. Prefer some other alternatives.
You can check that your attribute is supported with this javascript code, taken from here : http://www.webdesignerdepot.com/2013/04/how-to-use-the-download-attribute/
var a = document.createElement('a');
if(typeof a.download != "undefined")
{
// download attribute is supported
}
else
{
// download attribute is not supported
}
If you want to change the filename then just use a server-sided PHP script:
<?php
header('Content-type: application/pdf');
header('Content-Disposition: attachment; filename="newFile.pdf"');
echo file_get_contents("your_original_file");
?>
and make your link like this

How can I make a url a download link in html?

A client wants a url to be a download link.
Use case is like so:
user gets linked to example.com/download once there, it downloads a pdf file.
Can I do this without php ?
HTML5 introduced the download attribute.
Supporting user-agents will offer to download the file foo.png when clicking this link:
<a href="foo.png" download>Save the image</a>
You can also specify a different default file name that should be used:
<a href="foo.png" download="image.png>Save the image</a>
Read more at http://www.w3.org/TR/html5/links.html#downloading-resources.
Note that this only works for links. When users enter the URL directly into their browsers, this will have no effect, of course. If you want that, you need to send specific HTTP headers. See for example the question: How to force download of a file?. You don’t necessarily need a programming language like PHP for that. You can do it with, for example, .htaccess, too: Force File(image) Download with .htaccess
How a file is displayed is browser specific. Some may force you to download while some directly render it on the browser.
If you want to force the browser to download the file then you can set in Header the
Content-Type : application/octet-stream
You only need a link (anchor tag). The way the link behaves on click will depend on what browser you are and what settings you have in that particular browser. Some browsers will prompt you to open or save the file, other browsers will open the PDF file on a new tab or window.
Download PDF
You'll also need to make sure that the path to the PDF file is correct on the href property of your anchor tag.
Use this (HTML) not PHP:
Download pdf
Use the full url including the pdf file like.
Download

Audio download link works in Firefox but streams in Chrome

I've setup a download link on the site I'm building so that when users sign up to the musicians mailing list they can download a track for free. With this current code:
click here to download
It works in Firefox when you click the link it opens a window asking if you'd like to download but in Chrome it streams the track. If I change the file to .ogg then the reverse happens - I can download in Chrome but it streams in Firefox. Guess this is happening because I'm providing a format that the browser is capable of streaming. So how do I stop it streaming? can I provide two href's?
Having looked for similar questions here I came across the html5 attribute which can be added to links download="filename.mp3" I've tried this:
click here to download
But still it keeps streaming in Chrome, any ideas? help please?
Can you zip the file? that'll avoid the streaming.
OTHER OPTION
Other option a little of PHP :
<?php
$file = $_GET['file'];
header ("Content-type: octet/stream");
header ("Content-disposition: attachment; filename=".$file.";");
header("Content-Length: ".filesize($file));
readfile($file);
exit;
?>
and then
Download the mp3
Can you try the below code?
download
I think, it will work on all browsers.

How to change file download name in iframe?

My iframe tag look like bellow
<iframe width="100%" height="100%" src="'+file_url+'" frameBorder="0"> <p>Your browser does not support .</p></iframe>
file_url = store/ASDFVASDFADSFACASDFCDDF.doc
file_name = my word.doc
when i click on the iframe open link file download with incorrect file name.(doc file download with ASDFVASDFADSFACASDFCDDF.doc file name). how can i download file with real file name using iframe?
The iframe element can't do that, but you could put this in a .htaccess file in the 'store' directory :
<FilesMatch ".doc$">
Header set Content-Disposition "attachment; filename=myword.doc"
</FilesMatch>
Note that I'm assuming Apache here, but most webservers have a similar function
In modern browsers that support the download attribute, you should be able to specify in the following way:
Download
The nice thing is, this will also trigger a forced download behavior in the browser.
Alternatively, you could pass your download through a PHP script to set the filename to be served in the headers, something like this:
<?php
// We'll be outputting a PDF
header('Content-type: application/pdf');
// It will be called downloaded.pdf
header('Content-Disposition: attachment; filename="downloaded.pdf"');
// The PDF source is in original.pdf
readfile('original.pdf');
?>
This has nothing to do with HTML. You cannot do it.
The web server needs to send a "Content-Disposition" header. How to make your server do that depends what software is running on the server.
Alternatively... Just change the URL to this (and obviously move the file on the server):
store/ASDFVASDFADSFACASDFCDDF/Filename.doc