Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Uploading Binary Files

Status
Not open for further replies.

AbidingDude

Programmer
Oct 7, 2012
74
US
Hello, I'm relatively new to CGI programming. I just finished several routines learning how to parse query strings. (Coded in straight C.) Now I'm trying to learn about multipart/form-data. I thew together a quick HTML form with some text fields and a file upload button and attached a simple text file and hit submit. My CGI simply does a text dump, so I see the boundary-separated info, including the file contents. But when I try try to upload anything besides a text file, something hangs. I never see the output. I don't think it's my CGI, but it seems to be sensitive to file extensions. I took my file "stuff.txt" (which uploaded successfully), renamed the extension: "stuff.abc", and the form hangs. I actually have to reboot my server to fix it. I can't find anything in my server settings that pertain to this. Any help is appreciated!
 
So it doesn't just seem to be binary files. I originally tested this with a very small text file (< 100 bytes). Then, I tried it with a slightly larger text file (100 - 200 bytes). It hangs. My CGI is just a text dump:

C:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <io.h>
#include <fcntl.h>

int main(void)
{
	int n,check;

	printf("Content-Type: text/plain; charset=UTF-8\n\n");

	check=setmode(fileno(stdin), O_BINARY);
	if(check==-1){
		puts("setmode() error!");
		return 1;
	}
	while((n=fgetc(stdin))!=EOF)
		fputc(n,stdout);

	return 0;
}

So, I just tried making an in-between-size file - about 80 bytes. The CGI only displays part of it??
 
Ok, so I thought I would try dumping the info to a file instead of stdout.

C:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <io.h>
#include <fcntl.h>

int main(void)
{
	int n,check;
        FILE *td;

	printf("Content-Type: text/plain; charset=UTF-8\n\n");

	td=fopen("textdump.txt","wt");
	if(td==NULL){
		puts("Cannot create 'textdump' file!");
		return 1;
	}
	check=setmode(fileno(stdin), O_BINARY);
	if(check==-1){
		puts("setmode() error!");
		return 1;
	}
	while((n=fgetc(stdin))!=EOF)
		fputc(n,td);

        fclose(td);
	return 0;
}

And everything worked fine (even though I changed stdin to binary and am writing to a text file). The text file has all the form info.
So the problem seems to have been writing to stdout. But why? What is causing output to the browser client area to get "bogged down" if that's even what's happening?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top