Link to home
Create AccountLog in
Avatar of awassall
awassall

asked on

Save as on click

Hello

I need a solution to download a file by clicking in instead of left clicking and pressing "save as"

Is there any way of doing this (using javascript maybe)


Thanks
Adam
Avatar of Proculopsis
Proculopsis

This is best done server-side with ContentType, content-disposition etc:

<%@ Page Language="C#" %>
<%@ Import Namespace="System.IO" %>
<HTML>
<HEAD>
<script runat="server" ID=Script1>
void Page_Load(object sender, System.EventArgs e) {
if (Page.IsPostBack){
FileStream MyFileStream = new FileStream(@"C:\FTP\Catalog_DropFolder\LostSong.mp3", FileMode.Open);
long FileSize;
FileSize = MyFileStream.Length;
byte[] Buffer = new byte[(int)FileSize];
MyFileStream.Read(Buffer, 0, (int)MyFileStream.Length);
MyFileStream.Close();
Response.ContentType="audio/mpeg3"; 
Response.AddHeader( "content-disposition","attachment; filename=LostSong.mp3");
Response.BinaryWrite(Buffer);
}
}
</script>
</HEAD>
<body>
<form runat="server">
<asp:button id="link1" Text = "Play LostSong.mp3" runat="server" />
</form>
</body>
</HTML>

Open in new window

Not really.  You need to have a php file responsible for setting the correct headers so that the "download" box appears.  Check out this script:

http://elouai.com/force-download.php

Please note that this script has a gaping security hole.  At the top of the file, you will see:

$filename = $_GET['file'];

You need to validate the data!!!  I can't stress this enough.  Basically, you want to put some restrictions on the "file" name and make sure that the "file" name passes a strict series of tests.  Otherwise, a 10 year old could hack your server and have access to all of your files.
Here is a PHP script that will force a download.  The client gets to choose where to save the file.  Install it and run it to see how it works.

HTH, ~Ray
<?php // RAY_force_download.php
error_reporting(E_ALL);



// A FILE TO DOWNLOAD - THIS LINK COULD COME IN THE URL VIA $_GET, OR COULD BE GENERATED INSIDE THE SCRIPT
$url = "http://www.google.com/intl/en_ALL/images/logo.gif";

// USE CASE
force_download($url);




// FUNCTION TO FORCE A DOWNLOAD FROM A FILE
function force_download($filename)
{
    // GET THE CONTENTS OF THE FILE
    $filedata = @file_get_contents($filename);

    if ($filedata)
    {
        // GET A NAME FOR THE FILE
        $basename = basename($filename);

        // THESE HEADERS ARE USED ON ALL BROWSERS
        header("Content-Type: application-x/force-download");
        header("Content-Disposition: attachment; filename=\"$basename\"");
        header("Content-length: ".(string)(strlen($filedata)));
        header("Expires: ".gmdate("D, d M Y H:i:s", mktime(date("H")+2, date("i"), date("s"), date("m"), date("d"), date("Y")))." GMT");
        header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");

        // THIS HEADER MUST BE OMITTED FOR IE 6+
        if (FALSE === strpos($_SERVER["HTTP_USER_AGENT"], 'MSIE '))
        {
            header("Cache-Control: no-cache, must-revalidate");
        }

        // THIS IS THE LAST HEADER
        header("Pragma: no-cache");

        // FLUSH THE HEADERS TO THE BROWSER
        flush();

        // CAPTURE THE FILE IN THE OUTPUT BUFFERS - WILL BE FLUSHED AT SCRIPT END
        ob_start();
        echo $filedata;
    }

    // ERROR
    else
    {
        die("ERROR: UNABLE TO OPEN $filename");
    }
}

Open in new window

"I need a solution to download a file by clicking in instead of left clicking and pressing "save as""

When a user left clicks on any web item, the default that their browser is setup to handle that file based on the extension, is initiated.  You web page had NO CONTROL over this whatsoever -- it is set up by the browser they are using as a DEFAULT action to handle that file type.  There could be 100 different actions depending on the user, the browser he has chosen, and the default action for that particular file type extension.  You cannot control this -- the user has that choice.

What you CAN do, is give the file type an extension that no browser knows how to handle -- let us say it is FILENAME. QXY  -- there is no QXY extension, so no browser will know how to handle this file, thus it will ask the user -- SAVE or open with (WHAT?) application.

That is the limit of what you can do with a left click.  With a right click on ANY WEB filename, it is the browser that decides the context menu for right clicks, again, you have no control over this.
@scratchyboy - your comment is not entirely true.  You can set proper headers for the file in order to get the browser to behave differently.  This requires you to use a server side script to actually deliver the file, but the file name and extension are irrelevant.  

For example, in .NET I have a url rewriting script that will take any image in certain directories and allow for a "download" box to appear as opposed to just delivering the raw image.  So take the following url:

http://mydomain.com/special-directory/photos/my-photo.jpg

Since the image is in the "special-directory", I am going to deliver that file with the proper headers to force the user to download it as opposed to simply viewing it in the browser.
ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account