Chinaunix首页 | 论坛 | 博客
  • 博客访问: 477598
  • 博文数量: 144
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 508
  • 用 户 组: 普通用户
  • 注册时间: 2014-09-10 13:18
个人简介

Keep looking Donot settle

文章分类

全部博文(144)

文章存档

2019年(1)

2016年(31)

2015年(51)

2014年(61)

分类: LINUX

2015-04-03 15:16:14

原文地址:文件上传与下载 作者:ojhsky

goahead 版本: goahead-3.1.3-0

文件上传

所谓文件上传,就是把文件从PC传送到http服务器上。

  1. websFormDefine("http_upload", get_http_upload_file);

  1. void get_http_upload_file(webs_t wp, char_t *path, char_t *query)
  2. {
  3.     WebsKey *s;
  4.     WebsUpload *up;
  5.     char *upfile;
  6.     char cmd_buf[64] = {0};
  7.     pid_t pid;
  8.     int rtn;

  9.     if (scaselessmatch(wp->method, "POST")) {
  10.         for (s = hashFirst(wp->files); s; s = hashNext(wp->files, s)) {
  11.             up = s->content.value.symbol;
  12.             if (!strcmp(up->clientFilename, "")) {
  13.                 websReportError(wp, "Please select file!", NULL);
  14.                 break;
  15.             }

  16.             upfile = sfmt("/tmp/%s", up->clientFilename);
  17.             rename(up->filename, upfile);

  18.             /*if value of up->clientFilename is 123.rar , now, we got a file /tmp/123.ara already */
  19.         }
  20.     }

  21.     return;
  22. }
也就是说,执行了rename操作后,我们就得到了从浏览器传到服务器的文件.

文件下载

这里所说的下载,是指从HTTP服务器把文件传到浏览器所在PC上。

要实现下载,以流媒体为例,主要的是下面两行:

  1. Contentp-type: application/octet-stream;
  2. Content-Disposition: attachment; filename=config.gz
估计这两行之后的,都被认为是文件的内容。


假设抓包发现服务器发给浏览器的内容如下:

  1. HTTP/1.1 200 OK
  2. Server: GoAhead-http
  3. Date: Wed Sep 4 02:02:58 2013
  4. Transfer-Encoding: chunked
  5. Connection: keep-alive
  6. Contentp-type: application/octet-stream;
  7. Content-Disposition: attachment; filename=config.gz

  8. <html>
  9. <script>location.href='../backup.htm'</script></html>
那么浏览器会谈出一个框让你选择保存路径,文件名默认是config.gz,而config.gz的内容就是:

  1. <html>
  2. <script>location.href='../backup.htm'</script></html>

注意

在projects/goahead-xxxx-xxxx-bit.h中对goahead需要用的各种资源做了限制,如POST的文件最大为16384字节

  1. #ifndef BIT_GOAHEAD_LIMIT_POST
  2.     #define BIT_GOAHEAD_LIMIT_POST 16384
  3. #endif
所以如果你要POST给goahead的文件大小超过了16384,那么你就得把这个限制放大点。



阅读(1369) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~