GCGI File Upload Test

The following GCGI test program will upload the file you specify, and then immediatley send it back to your browser. If you upload an image or text file, the browser will display them, if you upload anything else, your browser should prompt you to save it to your local machine.

File Upload:

Here is the code to make this happen:

#include <stdlib.h>
#include <stdio.h>
#include "gcgi.h"

const char* const strTypes[] = {

const char* const strEncodings[] = {

int main (int argc, char *argv[], char *envp[]) 
  MimeEncoding  enc;
  MimeType      type;
  char         *file, *fname, *subtype;
  int           len;

   /* Set Limits: field limit, query limit */
  gcgiSetLimits(1048576, 0);

  if (initCgi() < 0) {
    fprintf(stderr, "Unable to parse CGI query.\n");
    return -1;

  gcgiFieldSize("uploaded_file", &len);
  file = (char *) malloc(len);

  gcgiFetchData("uploaded_file", file, len, &type, &subtype, &enc, &fname, &truncated);

  if (truncated) {
    fprintf(gcgiOut, "Content-Type: text/plain\r\n\r\n");
    fprintf(gcgiOut, "File size has exceeded limit.  Pick something smaller.\n");
  else {
    fprintf(gcgiOut,"Content-Type: %s/%s\r\n", strTypes[type], subtype);
    fprintf(gcgiOut,"Content-Disposition: attachment; filename=\"%s\"\r\n", fname);
    fprintf(gcgiOut,"Content-Length: %d\r\n\r\n", len);

    fwrite(file, sizeof(char), len, gcgiOut);



  return 0;

Julian M Catchen
Last modified: Sun Mar 24 11:32:40 MST 2002