RudeServer Professional C++ CGI Development Libraries
CGI Parser Logo

  RudeCGI file upload tutorial part 2

 

RudeServer Home
Introduction
Download
API Documentation
Basic Usage
Installation
Compiling

Tutorials/Examples
   File Upload Tutorial
   
Visual C++ CGI Tutorial

License/Disclaimer
Discussion Forum
Acknowledgments
Revision History

 

File Upload Tutorial - part 2

<- Prev | Next ->

Extracting Files from your CGI application

This section shows you how to use the CGIParser object within your C++ CGI scripts to upload files. Assuming that you have an HTML form set up and pointing to your script, and CGIParser installed, here are the steps you need to take. We will assume that the field name for the uploaded file is testfile.
  1. Include the cgiparser.h header file in your application.

    #include <rude/cgiparser.h>

  2. Obtain the global CGIParser

    using namespace rude;
    CGIParser parser;

  3. See if the expected field contains a file - otherwise the submitter never chose a file and left it blank.

    if ( parser.isFile("testfile") )
    // then a file was submitted

  4. Assuming they have uploaded a file, obtain the information you want

    const char *filename = parser.filename("testfile");
    const char *original_path = parser.filepath("testfile");
    const char *content_type = parser.contenttype("testfile");
    const char *datalength = parser.length("testfile");
    const char *filedata = parser.value("testfile");

Example CGI Scripts

Example CGI Script - Giving them back what they gave you.

This script allows a user to upload a file, and immediately download it again. It is the perfect way to make sure you get back what you submitted. The name of the compiled executable should be "example.exe".

Download the source code: example.cpp.

Example CGI Script - Uploading and saving a file

This script allows a user to upload a file, and puts the file on the server in a directory called "data". You must create the data directory before this will work. If you are on linus, set the permissions of the data directory to 777 for the purpose of this example.The name of the compiled executable should be "example2.exe".

Download the source code: example2.cpp.

 

<- Prev | Next ->

     

Copyright ©2000 RudeServer