SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Zealot
    Join Date
    Oct 2006
    Posts
    153
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Why can I create a simple right click and save as download link?

    Hi
    I have been trying to create a simple right click and save as download link. As of now the link just contains some text. But eventually I would like it to contain a zip file but as of now I am testing with just a little text. A picture of what this text looks like in Notepad is directly below.



    I try to create this little bit of downloadable text with the following code.

    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    	<head>
    		<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    		<title>myTxt</title>
    	</head>
    	<body>
    		<a href="marcs-website-design.com/a/my.txt">Link my.txt</a> 
    	</body>
    </html>
    And if you look at my FTP client pain that shows what's on my web sight indeed tells me that I have successfully uploaded both this code above and the little text file in a sub-directory of my web space. See picture below this text.



    If I try to use this simple right click and save as download link in Firefox I get the following result, which looks like nothing in Notepad and is in HTML format so I am showing you it as my Aptana Web editor renders it in its Firefox preview which is also the way it is rendered if you just left click on the link of the code shown above instead of the right click and save. See picture below this text.



    A picture of what happens in Internet Explorer is directly below



    But if you left click rather than right click in Internet Explorer you get exactly the same rendering as this file in Notepad on the top of this post, but this isn't what I want I wanted to be right click-able and down-loadable. So why can't I do this with a simple code I am using? I have read you could do this with PHP server script, but I would have to learn PHP and get PHP capable web-space both of which I do not want to do. So do you have any ideas for alternative code that would work or even how to enable the code I have now am using with client side JavaScript?

    Here is the link to the download by right clicking page I've been rambling about.

  2. #2
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,290
    Mentioned
    198 Post(s)
    Tagged
    3 Thread(s)
    Without the "http://" you are using a relative path instead of an absolute path. Try
    HTML Code:
    <a href="http://marcs-website-design.com/a/my.txt">Link my.txt</a>

  3. #3
    SitePoint Zealot
    Join Date
    Oct 2006
    Posts
    153
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Great that was it.
    It works great just the way I want in Firefox and Internet Explorer.

    I am wondering if you or someone could show me how I might take it even further. Would it be possible to create a link that evokes a JavaScript function that downloads that same text file. I mean something like "click here to download my little text file" and have it auto down-loadable with a left click instead of for right click.

  4. #4
    SitePoint Guru
    Join Date
    Oct 2008
    Location
    Melbourne
    Posts
    754
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can indeed and you don't have to use any JavaScript either!

    If your web server is an Apache server you can usually do this with some instructions in a .htaccess file. I have one in the episodes directory for a podcast blog that looks like this:

    Code:
    <Files *.mp3>
    Header set Content-Disposition attachment
    AddType application/octet-stream
    </Files>
    This forces the download of ALL mp3 files. In your case, however, you'd use this instead:

    Code:
    <Files *.txt>
    Header set Content-Disposition attachment
    AddType application/octet-stream 
    </Files>
    Or if it was just that file:

    Code:
    <Files my.txt>
    Header set Content-Disposition attachment
    AddType application/octet-stream 
    </Files>
    Check out the Apache Configuration forum for ideas.
    "I'm Commander Shepard, and this is
    my favourite post on the internet."

    We'll miss you, Dan Schulz.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •