Chitika

Thursday, November 3, 2011

ASP file upload

Use following HTML for file upload:

  <form name="frmSend" method="POST" enctype="multipart/form-data" accept-charset="utf-8">
            Bulk Upload: <input name="bulkfile" type="file" />
            <input type="submit" value="Upload" / id=submit1 name=submit1>
</form>

Obtain zip file from http://www.freeaspupload.net/freeaspupload/download.asp. Extract it and place freeASPUpload.asp in the directory containing scripts.

I had to comment out /disable line 122

Open the script to have upload functionality and add following line:

'At start of script


<%@ Language=VBScript %>
<%

    Response.Expires = -1
    Server.ScriptTimeout = 600
    ' All communication must be in UTF-8, including the response back from the request
    Session.CodePage  = 65001

.
.
.
%>

'then

<!-- #include file="freeaspupload.asp" -->

Dim uploadsDirVar
 uploadsDirVar = Session("UploadPath")

            if Request.ServerVariables("REQUEST_METHOD") = "POST" then
           
                Dim Upload, origName, savName, fullName
               
                Set Upload = New FreeASPUpload
                Upload.SaveOne uploadsDirVar,0,origName,savName

ASP/VBS reading excel file

Following is the code to read excel file, inspired from http://forums.aspfree.com/code-bank-54/classic-asp-read-excel-file-into-recordset-141448.html

Dim objConnXls, objRSXls, strSQLXls

Set objConnXls = Server.CreateObject("ADODB.Connection")
objConnXls.Open "DRIVER={Microsoft Excel Driver (*.xls)}; IMEX=1; HDR=NO; "& _
"Excel 8.0; DBQ=" & fullName & "; "
           
strSQLXls = "select * from [data]"

set objRSXls = objConnXls.Execute(strSQLXls)

'Fields can be processed as follows:

Dim username, userpassword, firstname, lastname, gender, profession_id, email, dob, country
                   
 username = objRSXls.Fields(0).value
 userpassword = objRSXls.Fields(1).value
 firstname = objRSXls.Fields(2).value
 lastname = objRSXls.Fields(3).value
 gender = objRSXls.Fields(4).value
 profession_id = objRSXls.Fields(5).value
 email = objRSXls.Fields(6).value
 dob = objRSXls.Fields(7).value
 country = objRSXls.Fields(8).value

objRSXls.Close

C++: CGI GET Request

GET request is easy to process, as Query String is passed in environment variable.
Follow is the code to process GET Request:

void ProcessQueryString(map<string,string>& query) {
    char *queryStr = getenv("QUERY_STRING");

    char *ptr=NULL;
    char *savPtr;
    ptr=strtok_r(queryStr, "&=", &savPtr);
    while (ptr != NULL) {
        string key(ptr);
        ptr = strtok_r(NULL, "&=", &savPtr);
        if(ptr!=NULL && strlen(ptr) > 0) {
            string value(ptr);
            query[key]=value;
        }
        ptr=strtok_r(NULL, "&=", &savPtr);

    }
}

C++: CGI POST Data

Following is the code to process HTTP-Post request, it stores the result in map<string, string>, idea is taken from http://www.openroad.org/cgihelp/cgi.html. To decode code from HTTP encoding to is taken from http://www.cs.tut.fi/~jkorpela/forms/cgic.html.


void ProcessPostQueryString(map<string,string>& query) {


        //FILE *fp = fopen("post.txt", "w+");
        char tmp[5*1024];

        char *ptmp = getenv("CONTENT_LENGTH");

        if(ptmp == NULL || strlen(ptmp) < 1) {
                return;
        }


        int contentLength = atoi(getenv("CONTENT_LENGTH"));
        char queryStr[5*1024];

        //sprintf(tmp, "content length[%d]\n", contentLength);
        //fputs(tmp, fp);
        //fflush(fp);

        if(contentLength > sizeof(queryStr)) {

                throw "only 5K of Post query string supported";
        }

        //fputs("post data: ", fp);
        int length=contentLength;
        //while(length > 0)
        {
                int nread = fread(queryStr, length, 1, stdin);
                //fputs(queryStr, fp);

                length -= nread;
        }
        queryStr[contentLength] = 0;

        //fputs("\n", fp);
        //fflush(fp);

        char *ptrSentence=NULL,
                 *ptrWord=NULL;
        char *savPtr1, *savPtr2;
        ptrSentence=strtok_r(queryStr, "&", &savPtr1);
        while (ptrSentence != NULL) {


                //cout << '[' << ptrSentence << ']' << endl;
                ptrWord = strtok_r(ptrSentence, "=", &savPtr2);
                if(ptrWord == NULL && strlen(ptrWord) < 1)  continue;
                string key(ptrWord);
                ptrWord = strtok_r(NULL, "=", &savPtr2);
                if(ptrWord !=NULL && strlen(ptrWord) > 0) {

                        int len=strlen(ptrWord);
                        HTMLDecode(ptrWord, ptrWord+len, tmp);
                        tmp[len]=0;

                        string value(tmp);
                        query[key]=value;

                        sprintf(tmp, "(%s)=(%s)\n", key.c_str(), value.c_str());
                        //fputs(tmp, fp);
                        //fflush(fp);
                }

                ptrSentence=strtok_r(NULL, "&", &savPtr1);
                if(ptrSentence==NULL)  break;

        }

        //fclose(fp);
}

Following is the code to decode HTML encoded string:

int HTMLDecode(char *src, char *last, char *dest)
{
    int sz=0;
 for(; src != last; src++, dest++, sz++)
   if(*src == '+')
     *dest = ' ';
   else if(*src == '%') {
     int code;
     sscanf(src+1, "%2x", &code) != 1;
     *dest = code;
     src +=2; }
   else
     *dest = *src;

 *dest=0;
 return sz;
}