How to change file download name in iframe? - html

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

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.

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

How can I make a video file available for download?

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;
});

View local html file with .asp extension in browser

Hi I have crawled a website for offline view, but many pages are downloaded and saved as .asp and there are links that point to these pages. But when I open these html page that has .asp extension in Chrome browser, the chrome will download the page instead of rendering the page. Is there a way to set the chrome to render the page instead of downloading it ?
Thanks
Chromium uses the system mime-type for the local files. On Linux you can change it in the xdg-open configuration:
$ mkdir -p ~/.local/share/mime/packages
$ cd ~/.local/share/mime/packages
$ touch application-x-asp.xml
Then edit the ~/.local/share/mime/packages/application-x-asp.xml:
<?xml version="1.0" encoding="UTF-8"?>
<mime-info xmlns="http://www.freedesktop.org/standards/shared-mime-info">
<mime-type type="text/html">
<comment>Active Server Page</comment>
<icon name="text-html"/>
<glob-deleteall/>
<glob pattern="*.asp"/>
</mime-type>
</mime-info>
to set up the mime-type to text/html. On te end you have to logout/-in again, or just run:
$ update-desktop-database ~/.local/share/applications
$ update-mime-database ~/.local/share/mime
When your browser opens a file it uses the MIME type to decide which action it should take, typically download or open in the bowser.
So, for example, it encounters a zip file it will open the file save dialogue box and allow you to save the file.
And, if for example, you request a .asp from your browser which is served from a web server the browser will use the MIME type to decide which action to take, which will be to display in the browser.
The MIME type will be sent within the http headers and this would not be sent to the browser when you open your off-line .asp pages.
So if you could change the MIME type to "text/html" for .asp it should open it in the browser.
Unfortunately, there does not seem to be an option for changing MIME type / actions in Chrome.
You can change these setting in Firefox within Tools > Options > Content > FileTypes > Manage..
There is a way to open local, .asp files as HTML in Firefox.
Find your Firefox profile folder. On Windows 7, I found it here:
C:\Users\user\AppData\Roaming\Mozilla\Firefox\Profiles\738xdie.default\mimeTypes.rdf
Then add these lines to mimeTypes.rdf
<RDF:Description RDF:about="urn:mimetype:text/html"
NC:fileExtensions="asp"
NC:description="ASP Pages"
NC:value="text/html"
NC:editable="true">
</RDF:Description>
Restart browser. Your local file will now render as HTML.
There may be a similar technique with Chrome.
I suppose you want to view downloaded pages directly, that is not via some local web-server. Then I'd suggest to change (or append) file extensions during download according to their reported content-types from response headers. Of course, this will require to adjust all links to changed filenames inside other files. The other approach could be to store content-type of each page in a meta-base, and using a local web-server to serve the files according to their actual types from the base, not file extensions.
I have two solutions for you. the first one is to open those files with firefox.. and that's it..
if you insist to open them with chrome. you have to change all files extension to html instead of asp. use some programs for renaming..
but the links will be broken between pages..
so you have to find the js file that all pages use add this code in it to fix the links
document.body.addEventListener('click',function(e){
if(e.target.nodeName=='A'){
e.preventDefault()
href=e.target.href.split('/')
href[href.length-1]=href[href.length-1].replace('.asp','.html')
href=href.join('/')
parent.location=href
}
})
})

Preventing playing an MP3 file using an HTML anchor

How do you make an MP3 link download instead of play "in-browser"?
I tried changing the target to blank, but that just opened the player in a new window.
You can't do this by modifying the link. You will have to have the HTTP server that serves the file send a Content-Type of "application/octet-stream". Presumably it is sending the type "audio/mpeg", which is hinting to the browser that it is MP3 content. Without the capacity to alter this header, you can't achieve this.
If your server supports PHP, create a PHP script called "getfile.php" (or similar) that takes a parameter of a file ID or file name. Set the content-type and content-disposition headers within the script to force a download prompt.
See: http://webdesign.about.com/od/php/ht/force_download.htm
Lots of solutions here. Here's the basic idea:
Use Javascript to make a (ajax) request to the server
When the request is received, run a script
This script redirects your browser temporarily to a new page
The new page has a few headers telling your browser to expect a file attachment, and what kind of attachment it is
The "save as" dialog pops up and your original page is still onscreen
I could be wrong, but I've seen people with the same problem before just with other file types, they used the code below:
<FilesMatch "\.(?i:mp3)$">
ForceType application/octet-stream
Header set Content-Disposition attachment
</FilesMatch>
Hope this works for you!
For quite some time (Chrome 14+, Firefox 20+, Edge 44+) it is possible to use the download attribute on same-origin links to force download behavior even when the server doesn't supply a Content-Disposition header on the link target.
Without access to the server generating the HTTP responses, the browser gets to decide what to do with different types of responses. usually the only files a browser will download are things like .zip files which it cannot display.