Jump to content

downloading images


chadmichael

Recommended Posts

Hello. This seems like a no brainer, but how does downloading an image work from the developer perspective. Do I just create an anchor to the photo's URL? This seems to default to opening a view of the image in the browser. I can right click on such a link and choose "save image as", but can I just make a link that goes directly to the download function? Thanks

Link to comment
Share on other sites

Well, I know they are the same results. But there's a difference in terms of the user interface. The gumby user won't know that they need to right click on an link and choose save as. It would be cool to just have the link automatically go to the dialog that asks them where to save it in their local filesystem. This is what happens if the link is to a file type that the browser can't handle, and it would be a nice default behaviour for image downloads.

Link to comment
Share on other sites

You would need to create a PHP page or something to do the download for you, and send a different mime type. Instead of saying the content is inline, you say the content is an attachment. That should force the browser to prompt the user to save. So you need some script that takes in a filename, and opens the file to get a string of bytes. The output of the page will be some mime type headers and then the file itself. Something like this. Look up the php reference page on 'header' if you want more info.

<?php$fname = $_GET['fname'];/* validate the file name here */$file_info = file_get_contents($fname);header('Content-type: application/jpg');header('Content-Disposition: attachment; filename="' . $fname . '"');echo $file_info;?>

Link to comment
Share on other sites

header('Content-type: application/jpg');
I'm not sure, but I think application/octet-stream is better?there is no mime-type application/jpg in my Linux mime.types,but application/octet-stream there is and it works fine with all images (all binary-files).Perl is beautiful:
#!/usr/bin/perl$fname = "image1.png";open FILE, $fname or die $!; local $/; $file = <FILE>;close FILE;print "Content-type: application/octet-stream\n";print "Content-Disposition: attachment; filename=somename.png\n\n";print $file;#

somename.png is only as "save name", it could be same as $fname or something else.

Link to comment
Share on other sites

I found application/jpg through Google. application/octet-stream will definately work, but it is my understanding that if the browser encounters a mime type that it does not understand, it will just prompt to save. I believe that it will just display the picture if you send image/jpg, so by changing it to application/jpg, I think that should cause a download prompt.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...