2.3 goAhead增加文件上传功能
goAhead-2.5的源码中,是没有包含文件上传功能的,因此需要对goAhead-2.5增加文件上传功能。本人使用v2.1.1版本的补丁,可从下载:
免费下载地址在 http://linux.linuxidc.com/
用户名与密码都是www.linuxidc.com
具体下载目录在 /pub/2011/10/17/goAhead上实现文件上传到嵌入式Web服务器上/
打补丁的时候不是很方便,需要利用对比工具,把文件上传功能的源码增加到goAhead-2.5中。
2.4 把上传文件写到存储系统
在goAhead-2.5的源码main.c中增加文件上传form的处理函数:formUploadFileTest (),代码如下:
- /******************************************************************************/
- /*
- * for test html upload file to web server
- * add by gyr 2011.10.15
- */
- static void formUploadFileTest(webs_t wp, char_t *path, char_t *query)
- {
- FILE * fp;
- char_t * fn;
- char_t * bn = NULL;
- int locWrite;
- int numLeft;
- int numWrite;
- printf("\n...................formUploadFileTest...................\n\n");
- a_assert(websValid(wp));
- websHeader(wp);
- fn = websGetVar(wp, T("filename"), T(""));
- if (fn != NULL && *fn != '\0') {
- if ((int)(bn = gstrrchr(fn, '/') + 1) == 1) {
- if ((int)(bn = gstrrchr(fn, '\\') + 1) == 1) {
- bn = fn;
- }
- }
- }
- printf("fn=%s, bn=%s .......\n", fn, bn);
- websWrite(wp, T("Filename = %s<br>Size = %d bytes<br>"), bn, wp->lenPostData);
- if ((fp = fopen((bn == NULL ? "upldForm.bin" : bn), "w+b")) == NULL) {
- websWrite(wp, T("File open failed!<br>"));
- } else {
- locWrite = 0;
- numLeft = wp->lenPostData;
- while (numLeft > 0) {
- numWrite = fwrite(&(wp->postData[locWrite]), sizeof(*(wp->postData)), numLeft, fp);
- if (numWrite < numLeft) {
- websWrite(wp, T("File write failed.<br> ferror=%d locWrite=%d numLeft=%d numWrite=%d Size=%d bytes<br>"), ferror(fp), locWrite, numLeft, numWrite, wp->lenPostData);
- break;
- }
- locWrite += numWrite;
- numLeft -= numWrite;
- }
- if (numLeft == 0) {
- if (fclose(fp) != 0) {
- websWrite(wp, T("File close failed.<br> errno=%d locWrite=%d numLeft=%d numWrite=%d Size=%d bytes<br>"), errno, locWrite, numLeft, numWrite, wp->lenPostData);
- } else {
- websWrite(wp, T("File Size Written = %d bytes<br>"), wp->lenPostData);
- }
- } else {
- websWrite(wp, T("numLeft=%d locWrite=%d Size=%d bytes<br>"), numLeft, locWrite, wp->lenPostData);
- }
- }
- websFooter(wp);
- websDone(wp, 200);
- }
