...

View Full Version : Function to pop up a save as dialog box



susoxty
12-30-2005, 02:42 PM
Hello all -

Any help would be greatly appreciated, for I am fairly new to JavaScripting. I am trying to create a function that force the download of an image on his/her computer. Right now, with the function I have created, when the user clicks the "Download" link, a "Save As" box opens, but it is trying to save the HTML page, not the actual image. How can i go about reworking my code to save the image, not the HTML page. Here is what i currently have:

SORRY: HERE'S THE REAL CODE


<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Cadillac Surprise & Delight</title>
</head>

<body>
<!--

/* This is the function that opens the 'save as' dialogue box in IE. */
/* It appears to fail gracefully in mozilla-based browsers but hsa not */
/* been fully tested */
-->
<script language="JavaScript">
function forceSaveAs (imagename){
document.execCommand('SaveAs',null,imagename)
}

</script>

<div>
<div class="thumb_img"><img src="thumb.jpg" border="0"></div>
<div class="thumb_txt"><a href="wallpaper.jpg" onclick="forceSaveAs('wallpaper.jpg'); return false">Download</a></div>
</div>

_Aerospace_Eng_
12-30-2005, 08:32 PM
<a href="image1.php"><img src="image1.jpg" style="border:0;"></a>
image1.php

<?php
// We'll be outputting a .JPG
header('Content-type: image/jpeg');

// It will be called image1.jpg
header('Content-Disposition: attachment; filename="image1.jpg"');
?>

feras_wilson
12-30-2005, 08:39 PM
type this:
<a href="saveimage()">Save</a>
<img name="myimg">
<script>
function saveimage() {
myi = window.open()
myi.document.execCommand("saveas","","myimage.jpg")
}
</script>

felgall
12-30-2005, 10:07 PM
type this:
<a href="saveimage()">Save</a>
<img name="myimg">
<script>
function saveimage() {
myi = window.open()
myi.document.execCommand("saveas","","myimage.jpg")
}
</script>

The request was for a JavaScript solution and the code you provided is JScript which Internet Explorer provides for use on Intranets where it is the only browser being used to display the page. The HTML/PHP solution is the only cross browser solution.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum