GIDForums  

Go Back   GIDForums > Webmaster Forums > Web Design Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 01-Apr-2004, 08:23
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light

Force a file download


Sorry for all the simple questions, but I am pretty new to this.

Is there a way to force a file download?

I have been putting files for download on my site with the following:

HTML Code:
<A HREF="http://www.dbcservices.com/files/warning_rapmusic.gif">warning_rapmusic</A>

This works, but when you left click on it, it will show you the graphic. Obviously, you can right click and download it and I have seen other pages where this is necessary, but I am thinking there has got to be a way to force a download when the link is clicked.
  #2  
Old 01-Apr-2004, 08:33
JdS's Avatar
JdS JdS is offline
Senior Member
 
Join Date: Aug 2001
Location: KUL, Malaysia
Posts: 3,371
JdS will become famous soon enough
I think it has to do with the Content-Type headers for files in that particular folder. If you can change that to be not auto set at "image/gif", then you will force a download. I am just guessing, it may be not as simple as that but surely it's something for you to look into..
  #3  
Old 01-Apr-2004, 11:24
Garth Farley Garth Farley is offline
Awaiting Email Confirmation
 
Join Date: May 2002
Location: Ireland
Posts: 638
Garth Farley is a jewel in the roughGarth Farley is a jewel in the roughGarth Farley is a jewel in the rough
Yup, J was there I think. You've to somehow tell IE not to be cleaver, and simply download the file, not view it itself. I think this can be done by adding this header (but keeping the content-type intact):
Code:
Content-Disposition: attachment; filename="image.gif"

There is PHP code on forums.devshed.com which would do you perfectly. But I can't find anything to do with changing Apache's settings for a single directory.
GF
  #4  
Old 03-Apr-2004, 10:05
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Thanks to both of you, J and Garth.

That is exactly what I needed. Unfortunately, I am a glutton for punishment and I am doing this entire thing in C, so I had to write my own routine to place the header. I think it works. I have a test site that I placed under the Web site review forum. For those interested, here is a poorly written C file that will handle this. You just need to pass the file location as the first parameter.

CPP / C++ / C Code:
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>

int main(int argv, char* argc[])
{
	void* 	buffer = malloc(1000);
	int		read_count;
	int		handle;
	char	filename;

	if(argv<2){
		fprintf(stderr,"This function should not be called without parameters.\n");
		return 1;
	}
	if( (handle = open(argc[1],O_RDONLY) ) == -1 ){
		fprintf(stderr,"Bad file name");
		return 1;
	}
	if(strrchr(argc[1],'/'))
		printf("Content-Disposition: attachment; filename=\"%s\"\n\n",(strrchr(argc[1],'/')+1));
	else
		printf("Content-Disposition: attachment; filename=\"%s\"\n\n",argc[1]);
	do{
		read_count = read( handle, buffer, 1000);
		if(read_count>0)
			fwrite(buffer,1,read_count,stdout);
	}while(read_count == 1000);

	close(handle);

	return 0;
}

One thing that I have learned in all of this is that it is crucial to have a newline after the header definition. So the \n\n at the end of the printf statements are necessary.
 
 

Recent GIDBlogToyota - 2009 May Promotion by Nihal

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
CD burner wont burn!! robertli55 Computer Hardware Forum 1 18-Jun-2004 11:53
Yet another CD burner problem: Lite-On LSC-24082K Erwin Computer Hardware Forum 1 22-May-2004 12:28
Re: Programming Techniques WaltP C Programming Language 0 10-Mar-2004 00:56
How to allow download file in Apache Web server? Zaro Apache Web Server Forum 1 24-Feb-2004 03:41
read specified range of lines from file etron C Programming Language 8 18-Feb-2004 09:04

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 20:29.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.