goAhead上实现文件上传到嵌入式web服务器上

作者:reille

本博客网址:http://blog.csdn.net/reille/,转载本博客原创文章请注明出处。

本文内容概要:详细描述了在goAhead 2.5上如何实现文件上传(上传文件)到服务器端的功能。

开发环境:

        宿    主   机:window XP;

        虚    拟   机:ubuntu9.10;

        交叉编译器:arm-uclibc-gcc(arm-linux-gcc-4.3.2可以顺利编译通过)

注:移植好的源代码下载地址:移植好的goAhead源码包下载地址

———————————————————————————————————————————————————————————————————

1. 说明

最近调试web文件上传到服务器功能,但在调试时,处理函数总是获取不到文件路径,百思不得其解,查了网上许多文章,但大多提到的是前端文件上传的原理、实现方式等,而未提供服务器端处理的实现(利用C函数实现)。此外,由于对web不了解,花了些时间研究web程序。

2. goAhead实现文件上传的方法

总得来说,goAhead上实现文件上传功能是比较容易的。因为有现成的代码可用,稍微移植下即可。

2.1 实现原理

使用html form即表单提交文件上传请求,web服务器核心处理接收客户端Post过来的文件数据(注意post的是二进制数据),最后,web服务器把接收到文件数据以二进制格式写到服务器本端存储系统。

2.2 前端设计

前端设计比较简单,就是设计一个form,type属性为file,本人是在goAhead-2.5附带的wwwdemo的asptest.asp网页上增加了一个这样的form。

  1. <html> 
  2. <!- Copyright (c) Go Ahead Software Inc., 1999-2010. All Rights Reserved. -> 
  3. <head> 
  4.  
  5. <!-- del by gyr 2011.10.15 
  6. <title>ASP Test Page</title> 
  7. --> 
  8. <title> new document </title> <!-- add by gyr 2011.10.15 --> 
  9.  
  10. <link rel="stylesheet" href="/style/normal_ws.css" type="text/css"> 
  11. <% language=javascript %> 
  12.  
  13. function uploadFileSubmit() 
  14. //  alert(document.getElementById("document.softupdate")); 
  15.     return; 
  16.  
  17. </head> 
  18.  
  19. <body> 
  20.  
  21. <h1>ASP / JavaScript™ Test</h1> 
  22. <h2>Expanded ASP data: <% aspTest("Peter Smith", "112 Merry Way"); %></h2> 
  23.  
  24. <P> 
  25. <% var z; \ 
  26.    for (z=0; z<5; z=z+1) \ 
  27.      { \ 
  28.      if (z<=2) \ 
  29.         write(z+" is less than 3<br>"); \ 
  30.      else if (z==3) \ 
  31.         write(z+" is equal to 3<br>"); \ 
  32.      else \ 
  33.         write(z+" is greater than 3<br>"); \ 
  34.      } \ 
  35. %> 
  36. </P> 
  37.  
  38.  
  39.  
  40.  
  41. <span style="color: rgb(51, 51, 255);"><!-- added start for test upload file by gyr 2011.10.15 --> 
  42. <h1>GoForm upload file test</h1> 
  43. <form id="softupdate" action=/goform/formUploadFileTest method=POST enctype="multipart/form-data"> 
  44.     <table> 
  45.         Select file: <td> <input id="fileupload" type="file" name="fileupload" size=60 value="">    </td> 
  46.         <td> <input id="fileuploadsubmit" type="submit" name="update" value="update" onClick="uploadFileSubmit()">  </td> 
  47.     </table> 
  48. </form> 
  49. <!-- added end for test upload file by gyr 2011.10.15 --> 
  50.  
  51. </span> 
  52.  
  53. </body> 
  54. </html> 
<html>
<!- Copyright (c) Go Ahead Software Inc., 1999-2010. All Rights Reserved. ->
<head>

<!-- del by gyr 2011.10.15
<title>ASP Test Page</title>
-->
<title> new document </title> <!-- add by gyr 2011.10.15 -->

<link rel="stylesheet" href="/style/normal_ws.css" type="text/css">
<% language=javascript %>

function uploadFileSubmit()
{
//	alert(document.getElementById("document.softupdate"));
	return;
}

</head>

<body>

<h1>ASP / JavaScript™ Test</h1>
<h2>Expanded ASP data: <% aspTest("Peter Smith", "112 Merry Way"); %></h2>

<P>
<% var z; \
   for (z=0; z<5; z=z+1) \
     { \
     if (z<=2) \
		write(z+" is less than 3<br>"); \
     else if (z==3) \
		write(z+" is equal to 3<br>"); \
     else \
		write(z+" is greater than 3<br>"); \
     } \
%>
</P>




<!-- added start for test upload file by gyr 2011.10.15 -->
<h1>GoForm upload file test</h1>
<form id="softupdate" action=/goform/formUploadFileTest method=POST enctype="multipart/form-data">
	<table>
		Select file: <td> <input id="fileupload" type="file" name="fileupload" size=60 value=""> 	</td>
		<td> <input id="fileuploadsubmit" type="submit" name="update" value="update" onClick="uploadFileSubmit()">	</td>
	</table>
</form>
<!-- added end for test upload file by gyr 2011.10.15 -->



</body>
</html>


其中,enctype参数用来设置表单的MIME编码方式,在进行文件(或同时包含文本框)上传时,必须将其属性设置为"multipart/form-data";formUploadFileTest 是web服务器定义的一个处理函数,用于把web服务器接收到的上传文件数据写到存储系统。

2.3 goAhead增加文件上传功能

goAhead-2.5的源码中,是没有包含文件上传功能的,因此需要对goAhead-2.5增加文件上传功能。本人使用v2.1.1版本的补丁,可从下载:http://velep.com/archives/321.html

打补丁的时候不是很方便,需要利用对比工具,把文件上传功能的源码增加到goAhead-2.5中。

2.4 把上传文件写到存储系统

在goAhead-2.5的源码main.c中增加文件上传form的处理函数:formUploadFileTest (),代码如下:

  1. /******************************************************************************/ 
  2. /*
  3. * for test html upload file to web server
  4. * add by gyr 2011.10.15
  5. */ 
  6.  
  7. static void formUploadFileTest(webs_t wp, char_t *path, char_t *query) 
  8.     FILE *       fp; 
  9.     char_t *     fn; 
  10.     char_t *     bn = NULL; 
  11.     int          locWrite; 
  12.     int          numLeft; 
  13.     int          numWrite; 
  14.  
  15.     printf("\n...................formUploadFileTest...................\n\n"); 
  16.  
  17.     a_assert(websValid(wp)); 
  18.     websHeader(wp); 
  19.  
  20.     fn = websGetVar(wp, T("filename"), T("")); 
  21.     if (fn != NULL && *fn != '\0') { 
  22.         if ((int)(bn = gstrrchr(fn, '/') + 1) == 1) { 
  23.             if ((int)(bn = gstrrchr(fn, '\\') + 1) == 1) { 
  24.                 bn = fn; 
  25.             } 
  26.         } 
  27.     } 
  28.  
  29.     printf("fn=%s, bn=%s  .......\n", fn, bn); 
  30.  
  31.     websWrite(wp, T("Filename = %s<br>Size = %d bytes<br>"), bn, wp->lenPostData); 
  32.  
  33.     if ((fp = fopen((bn == NULL ? "upldForm.bin" : bn), "w+b")) == NULL) { 
  34.         websWrite(wp, T("File open failed!<br>")); 
  35.     } else
  36.         locWrite = 0; 
  37.         numLeft = wp->lenPostData; 
  38.         while (numLeft > 0) { 
  39.             numWrite = fwrite(&(wp->postData[locWrite]), sizeof(*(wp->postData)), numLeft, fp); 
  40.             if (numWrite < numLeft) { 
  41.                 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); 
  42.             break
  43.             } 
  44.             locWrite += numWrite; 
  45.             numLeft -= numWrite; 
  46.         } 
  47.  
  48.         if (numLeft == 0) { 
  49.             if (fclose(fp) != 0) { 
  50.                 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); 
  51.             } else
  52.                 websWrite(wp, T("File Size Written = %d bytes<br>"), wp->lenPostData); 
  53.             } 
  54.         } else
  55.             websWrite(wp, T("numLeft=%d locWrite=%d Size=%d bytes<br>"), numLeft, locWrite, wp->lenPostData); 
  56.         } 
  57.     } 
  58.  
  59.     websFooter(wp); 
  60.     websDone(wp, 200); 
  61.  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值