DG3
DG3

Reputation: 5298

window.open does not open in IE

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?

Upvotes: 3

Views: 627

Answers (2)

jValdron
jValdron

Reputation: 3418

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.

Upvotes: 4

Jony Boro
Jony Boro

Reputation: 1

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.

Upvotes: 0

Related Questions