Page 1 of 1

Save As Dialog with href in html

Posted: Mon Apr 02, 2007 7:21 am
by buttsp
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hi All,
        I have a very small problem on which i have spent some reasonable amount of time, but still couldn't able to find out the solution 

The problem is that i have an html page on which there is an href link (infact 4) that contains a url to a file (.csv) located on some different host/domain. Now  what i want is that when ever user clicks to this link, the file *SaveAs* doalog popup should appear and ask the user to save a file. On FireFox, the href defualt behaviour is doing what i needed, but when i use IE6, the .csv file direclty opens itself into the browser.

I have tried the following:

Code: Select all

$url = $_GET[file];
$mm_type="application/msexcel";

header("Cache-Control: public, must-revalidate");
header("Pragma: hack");
header("Content-Type: " . $mm_type);
header("Content-Length: " .(string)(filesize($url)) );
header('Content-Disposition: attachment; filename="'.basename($url).'"');
header("Content-Transfer-Encoding: binary\n");
                  
$fp = fopen($url, 'rb');
$buffer = fread($fp, filesize($url));
fclose ($fp);
                  
print $buffer;
but then on clicking the link, the saveas dialog does come but with some download.php page as file and that contains some error messages.

Could anybody help me out figuring out this problem.

A tested/tried code-snipped example shall be highly appreciated.

thanks.


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Mon Apr 02, 2007 8:32 am
by buttsp
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


i did it finally after a bit of more research:

Code: Select all

$url = $_GET[url]; //complete url
$file = $_GET[file]; //just name of the file
$mm_type="application/msword";

/* Required for IE, otherwise Content-disposition is ignored */
if(ini_get('zlib.output_compression')) ini_set('zlib.output_compression', 'Off');

/* Output HTTP headers that force "Save As" dialog */
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false);
header("Content-Type: application/msexcel");
header("Content-Disposition: attachment; filename=\"$file\"");
header("Content-Transfer-Encoding: binary");

set_time_limit(0);

/* Send the entire file using @ to ignore all errors */
@readfile($url);

/* Exit immediately so no garbage follows the file contents */
exit;

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]