goahead文件上传

1.goahead文件上传需要修改与添加的源码

  • 在goahead.c文件中添加一个并注册一个接受并客户端(浏览器)文件的action,代码示例:
static void uploadFileAction(Webs *wp)
{
    WebsKey         *s;
    WebsUpload      *up;
    char            *upfile;
    websSetStatus(wp, 200);
    websWriteHeaders(wp, -1, 0);
    websWriteHeader(wp, "Content-Type", "text/plain");
    websWriteEndHeaders(wp);
    /***
    *保存上传的文件并打印上传文件的具体信息
    /
    if (scaselessmatch(wp->method, "POST")) {
        for (s = hashFirst(wp->files); s; s = hashNext(wp->files, s)) {
            up = s->content.value.symbol;
            websWrite(wp, "FILE: %s\r\n", s->name.value.string);
            websWrite(wp, "FILENAME=%s\r\n", up->filename);
            websWrite(wp, "CLIENT=%s\r\n", up->clientFilename);
            websWrite(wp, "TYPE=%s\r\n", up->contentType);
            websWrite(wp, "SIZE=%d\r\n", up->size);
            upfile = sfmt("%s/tmp/%s", websGetDocuments(), up->clientFilename);//文件存放的地方
            if(access(up->filename, 0) !=0)
            {
                   error("uploadTest() %s DOESN'T EXISIT!",up->filename);
            }
            if(access(upfile, 0) !=0)
            {
                   error("uploadTest() %s DOESN'T EXISIT!",upfile);
            }
           if (rename(up->filename, upfile) < 0)
            {
                error("Cannot rename uploaded file: %s to %s, errno %d", up->filename, upfile, errno);
            }
            wfree(upfile);
        }
        websWrite(wp, "\r\nVARS:\r\n");
        for (s = hashFirst(wp->vars); s; s = hashNext(wp->vars, s)) 
        {
            websWrite(wp, "%s=%s\r\n", s->name.value.string, s->content.value.string);
        }
    }
    websDone(wp);
}
  • 如果是在windows执行goahead服务器,那就修改Goahead-windows-default-me.h或Goahead-windows-static-me.h文件,如果是在linux系统下执行goahead服务器就修该Goahead-linux-default-me.h或Goahead-linux-static-me.h。修改如下:

修改宏ME_GOAHEAD_UPLOAD_DIR,这个宏是用来处理文件上传时,设置的一个暂存的文件夹,修改成设置的暂存文件的目录,例如 修改为:#define ME_GOAHEAD_UPLOAD_DIR “D:/test”

  • 修改http.c文件
    删除或注释掉static void parseHeader(Webs *wp)函数中的如下代码(为了存放大于2M的文件):
   if (wp->rxLen > ME_GOAHEAD_LIMIT_POST) {
                    websError(wp, HTTP_CODE_REQUEST_TOO_LARGE | WEBS_CLOSE, "Too big");
                    return;
               }

2.前台代码示例


<html>
    <head>
        <meta charset="UTF-8">
        <title>title>
    head>
    <body>
        <form action="/action/uploadTest" method ="post" enctype="multipart/form-data">
            <input type="file" name="files[]" multiple/>

            <input type="submit" value="提交">
        form>
    body>
html>

3.结果

你可能感兴趣的:(Goahead)