// File Upload Example // by Matt Flood // Copyright 2002, 2003 // // this CGI script prompts the user to upload a file // When submitted, the program returns the users file in a new window. // The file should not be corrupted by the upload or download. // // uncomment the following line if you are using windows, otherwise leave it alone. //#define WINDOWS #include "cgiparser.h" #include #include #ifdef WIN32 #include #include // For WINDOWS BINARY MODE #endif using namespace rude; int main(void) { // obtain the global CGI Parser object // CGIParser parser; // Check if a file was uploaded - identified by "testfile" // if( parser.exists("testfile") && parser.isFile("testfile") ) { // If a file was uploaded, return the file to the user // print response headers // std::cout << "Content-Type: " << parser.contenttype("testfile") << "\n"; std::cout << "Content-Length: " << parser.length("testfile") << "\n"; // end of header newline // std::cout << "\n"; // output the file data to the submitter // const char *data = parser.value("testfile"); int length = parser.length("testfile"); #ifdef WINDOWS int result = setmode( fileno( stdout ), O_BINARY ); if(result == -1) exit 1; #endif for(int x=0; x< length; x++) { fputc(data[x],stdout ); } } else { // if a file was not uploaded, present the user with an html page // std::cout << "Content-Type: text/html\n\n"; // Display a form for user to enter file into. It is a recursive form that points back to this script - named example3.exe // std::cout << "
\n" << "\n" << "\n" << "
\n"; } return 0; }