window.open does not open in IE - javascript

I am trying to open a word document using window.open as below
window.open("myworddoc.doc");
It works fine in FF, but IE tries to open a tab, but closes it immediately and jumps back to the current screen (no dialog is displayed to save or open a file).
What could be the issue?

This is surely a security mesure. Opening Word documents using JavaScript could have nasty effects. Imagine if you are browsing the internet, and someone makes an infected Word document open when your page loads.
Personally, I'd create a PHP file, let's say "servedoc.php", and open that file like so:
window.open("servedoc.php");
servedoc.php could contain something like this:
<?php
$file = "myworddoc.doc";
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=$file");
header("Content-Type: application/msword");
header("Content-Transfer-Encoding: binary");
readfile($file);
IE will open the PHP file, as it's a perfectly valid web file. And the PHP script would serve the file to the browser, asking the user to download the file.

Or if you are using .net (vb):
Response.ContentType = "image/jpeg" 'mime type of the file to serve.
Response.AddHeader("content-Disposition", "attachment;filename=YOURFILENAME")
Response.TransmitFile(YourFILEPath)
Like this you can let them download the .doc or the .zip file if you prefer.

Related

Read pdf without download and print it with html php

Good evening,
I would like to create a feature for my users that allows them to read a pdf file without downloading or printing it.
They will only be able to read the pdf file.
I tried pdf.js which did not work,
I also tried to convert my pdf to html via a script,
I also tried this php code but the download and print button appears:
<?php
// The location of the PDF file
// on the server
$filename = "/path/to/the/file.pdf";
// Header content type
header("Content-type: application/pdf");
header("Content-Length: " . filesize($filename));
// Send the file to the browser.
readfile($filename);
?>
Do you have any solutions or technologies to suggest to me,
I use PHP, HTML and JS.
Try this one:
<?php
// Store the file name into variable
$file = '/path/to/the/filename.pdf';
$filename = 'filename.pdf';
// Header content type
header('Content-type: application/pdf');
header('Content-Disposition: inline; filename="' . $filename . '"');
header('Content-Transfer-Encoding: binary');
header('Accept-Ranges: bytes');
// Read the file
#readfile($file);
?>
PHP uses a standard code to display the pdf file in web browser. The process of displaying pdf involves location of the PDF file on the server and it uses various types of headers to define content composition in form of type, Disposition, Transfer-Encoding etc. PHP passes the PDF files to read it on the browser. Browser either shows it or download it from localhost server then display pdf.
Note: PHP is not actually reading the PDF file. It does not recognize File as pdf. It only passes the PDF file to the browser to be read there. If copy the pdf file inside htdocs folder of XAMPP then it does not need to specify the file path.
The php script you offered only can download the file. if you use php ,you can try the demo ,which is using pdf2text here
or try the method in this link read-pdf-files-with-php

Force IE11 to save download CSV file

I am trying to download a CSV file to the browser.
I am unable to get it to properly download the file in IE11. Instead it opens a new tab and write the CSV contents to the tab.
I am using just the following javascript:
window.open(sUrl, '_target');
In the IE Developer tools, I can see the Response Header shows:
Content-Type: text/csv; charset=UTF-8
I would like it to prompt the user to save the file.
I've changed the browser settings to always download, yet it still doesn't do it.
I would like to fix this from a change to the Javascript, if possible.
How can I do that?
As far as I know, it's not possible to do this with JavaScript. Take a look at this answer if you're comforable using PHP to download the file.
According to that answer, your PHP should look something like this:
<?php
$file_url = 'http://www.myremoteserver.com/file.csv';
header('Content-Type: text/csv; charset=UTF-8');
header("Content-Transfer-Encoding: Quoted-Printable");
header("Content-disposition: attachment; filename=\"" . basename($file_url) . "\"");
readfile($file_url);
?>

Download file from URL, then save to user computer via dialog

How can I download file (I just know the URL - the file can have x MB) and then save via dialog to user computer? (Dialog = user can choose the directory, where he want to save the file). Just like in screen.
I have absolutely no idea how to do it. I Google it and I found only how to load the file via FILE input, but I do not need that.
File suffix is "*.db", thanks guys, I'm not such a web guy.
You can do that pretty easily.
Make a php file, let's call it index.php. And this should be all the code you need:
<?php
$file = "/your/location/file.db";
header("Content-Type: application/octet-stream");
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename='" . basename($file) . "'");
readfile ($file);
?>
I hope I've understood what you actually need. This should do the trick.

Detect native or third-party PDF plugin in browser

We're writing a small tool to view generated PDFs from our site and have the browser open them. The twist in our case is that we want the PDF to show in the browser 100% of the time. We do not want the browser to save the PDF to its downloads folder automatically (for HIPAA reasons). (If the users download it themselves, that is fine. The save being initiated by them is a very important distinction.)
I would like to find a solution that compromises between browser and the need for this extra. My plan of attack is:
Detect native browser plugin, e.g. Google Chrome and Firefox.
If no native browser plugin is available, check for third-party plugins, e.g. for Adobe, or FoxIt.
If no third-party plugin is available, render with PDF.js or some other Javascript solution.
I am unsure how to go about step 1. I've found a few hits for step 2, but I want to get step 1 working as that is the primary use case. Any help would be appreciated.
Thanks in advance.
If you want render the pdf file from the server programmatically you need to add mime type to header:
PhP example:
<?php
$file = 'path/to/PDF/file.pdf';
$filename = 'filename.pdf';
header('Content-type: application/pdf');
header('Content-Disposition: inline; filename="' . $filename . '"');
header('Content-Transfer-Encoding: binary');
header('Accept-Ranges: bytes');
#readfile($file);
?>
C# example:
Response.AppendHeader("Content-Disposition", "inline; filename=something.pdf")
Return File(output, "application/pdf", "something.pdf")
Or if you want to detect the pdf viewer using JavaScript this can be help you: Javascript Detect if Adobe Reader is installed

Download image direct from url

Let's say that I have some URL to an image on the web. Let's say the URL is http://www.gearheadwalls.com/wp-content/uploads/2013/07/Mercedes-Benz-S-Class-4.jpg
Now, when a user press the download button, the image should be downloaded.
I've tried this:
window.location.href = Link;
But sometimes it just opens the image on the browser and sometimes it is downloaded as I wanted.
How can I achieve this?
You can use the HTML5 download attribute on anchors :
<a href="http://www.gearheadwalls.com/wp-content/uploads/2013/07/Mercedes-Benz-S-Class-4.jpg" download="http://www.gearheadwalls.com/wp-content/uploads/2013/07/Mercedes-Benz-S-Class-4.jpg">
<img src="http://www.gearheadwalls.com/wp-content/uploads/2013/07/Mercedes-Benz-S-Class-4.jpg">
</a>
You need to pass appropriate headers in order to allow user to download the file. If you just provide the file link in the url the browsers interpret it differently. They may first try to open the file in the browser, if it fails the file will be prompt as force-download.
If you are using PHP, the headers in download script is something like:
header('Content-Type: ' . $mime_type);
header('Content-Disposition: attachment; file="'.$name.'"');
header("Content-Transfer-Encoding: binary");
header('Accept-Ranges: bytes');
header("Cache-control: private");
header('Pragma: private');
The full tutorial can be found here: www.phptutorialforbeginners.com/2013/04/file-download-script-in-php-php.html
You will have to set Content-Disposition header field, as suggested by http://www.w3.org/Protocols/rfc2616/rfc2616-sec19.html, if you want the image should not be handled natively by browser. If you use PHP, may be this link would help, http://w3schools.invisionzone.com/index.php?showtopic=39943
Only issue with this, you can't directly make apache serve you this file, as a normal static resource, or I don't know the way to do this Apache :)

Categories

Resources