Single-click download jpg (instead of right-clilck -> save as...)

Posted on 2007-08-09
Last Modified: 2013-12-25
Haven't figured this out...  Gmail gives you two links for attached jpgs - a View and Download link.  The view link works like any link, it opens the jpg in the browser.  The download however asks you to download the file.  How?

Every other site I've seen that wants their visitor to download a jpg puts instructions on the screen to right-click and select "Save as..."  How did Google get the browser to skip those steps?

I've inspected the headers with Fiddler HTTP Debugging Proxy, but it shows the Content-Type and everything else being the same between the view and download links.

I would love to know how they do this.
Question by:eaglewizard_1975
    LVL 6

    Expert Comment

    The content type is how I do it with pdf's.  I use
    application/octet-stream for a type I want to be downloaded
    or it's real type if I want the files application to open it naturally.
    LVL 1

    Author Comment

    Yeah, but that doesn't work with jpgs.  The natural action a browser takes it to open a jpg, gif, bmp, etc.  Google's found a way to override that natural action.
    LVL 15

    Accepted Solution

    Do not link to the jpg directly, but use server-sided script page instead.
    For example,

    In the script, add some additional header before write out the file content.

    content-disposition=attachment; filename=yourfile.jpg;

    Hope this help.
    LVL 1

    Author Comment

    OK, I'll give it a try.
    LVL 1

    Author Comment

    Since you posted the content-disposition:attachment, I was able to look up some more pertinent info.

    I also found some code someone already wrote to accomplish this in .NET and in SQL.

    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    Join & Write a Comment

    In Part 1 ( we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
    This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
    This tutorial demonstrates how to identify and create boundary or building outlines in Google Maps. In this example, I outline the boundaries of an enclosed skatepark within a community park.  Login to your Google Account, then  Google for "Google M…
    The viewer will learn how to count occurrences of each item in an array.

    755 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now