Upload files to the embedded web server on the goahead.

Source: Internet
Author: User
Summary: describes in detail how to upload (upload) files to the server on goahead 2.5.

Development Environment:

Host Machine: Window XP;

Virtual Machine: ubuntu9.10;

Cross Compiler: Arm-uclibc-GCC (arm-linux-gcc-4.3.2 can be compiled smoothly through)

-------------------------------------------------------------------

1. Description

Recently, I debugged the Web file upload function to the server. However, during debugging, the handler function always failed to obtain the file path, so I had to find many articles on the Internet, however, the principle and implementation method of front-end File Upload are mostly mentioned, but the server-side processing implementation is not provided (implemented using the C function ). In addition, due to lack of understanding about the web, it took some time to study web programs.

2. How to upload files using goahead

In general, it is easier to implement the file upload function on the goahead. Because there is a ready-made code available, just port it a little.

2.1 Implementation Principle

Use HTML form to submit File Upload requests. The core of the Web server processes the file data received by the post client (note that the post is binary data). Finally, the Web server writes the received file data in binary format to the Local Storage System of the server.

2.2 front-end design

Front-end design is relatively simple, is to design a form, type attribute for file, I am in the goAhead-2.5 with wwwdemo asptest. asp web page added such a form.

<! -Copyright (c) Go ahead Software Inc., 1999-2010. All Rights Reserved.-> <! -- Del by gyr 2011.10.15 <title> Asp test page </title> <title> new document </title> <! -- Add by gyr 2011.10.15 --> <linkrel = "stylesheet" href = "/style/normal_ws.css" type = "text/CSS"> <% Language = JavaScript %> function uploadfilesubmit () {// alert (document. getelementbyid ("document. softupdate "); Return ;}< h1> Asp/JavaScript? Test The enctype parameter is used to set the form's mime encoding method. When uploading a file (or containing a text box at the same time), you must set its attribute to "multipart/form-Data "; formuploadfiletest is a processing function defined by the Web server to write the uploaded file data received by the Web server to the storage system.

2.3 added the file upload function for goahead.

GoAhead-2.5 source code, is not included in the file upload function, therefore need to add file upload function to the goAhead-2.5. I use v2.1.1 patch, can download from: http://download.csdn.net/detail/reille/3687321

Patch is not very convenient, need to use the comparison tool, the file upload function source code added to the goAhead-2.5.

2.4 write uploaded files to the storage system

In the goAhead-2.5 source code main. C add File Upload form handler: formuploadfiletest (), the Code is as follows:

/*************************************** ***************************************/ * For test HTML Upload File to Web serverstaticvoid formuploadfiletest (webs_t WP, char_t * path, char_t * query) {char_t * fN; char_t * bn = NULL; printf ("\ n ................... formuploadfiletest ................... \ 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> "); locwrite = 0; numleft = WP-> lenpostdata; 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);} locwrite + = numwrite; numleft-= numwrite;} 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); webswrite (WP, T ("file size written = % d bytes <br>"), WP-> lenpostdata);} webswrite (WP, T ("numleft = % d locwrite = % d size = % d bytes <br>"), numleft, locwrite, WP-> lenpostdata) ;}} websfooter (WP ); websdone (WP, 200 );}

3. Materials

3.1 http://blog.csdn.net/reille/article/details/6871827 post reposted on this blog

3.2 http://www.hackchina.com/r/57970/v2.1.1-_-web-_-upload.htm__html front-end implementation reference

3.3 http://www.hackchina.com/r/57970/v2.1.1-_-LINUX-_-upldForm.c__html server implementation reference

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.