nginx 上传的文件始终带有 boundary 信息

我正在做一个文件上传的功能,使用的是 nginx 的上传,但是上传到临时目录的文件和源文件的 MD5 不一致,经过检查发现是临时文件被加入了 boundary 信息。

例如
源文件 a.txt:

aaaaaaaaaaaaaaaaaaaaaaa

临时文件 /tmp/1/000000001

------WebKitFormBoundaryHllRggx1NSzlMz0k^M
Content-Disposition: form-data; name="file"; filename="a.txt"^M
Content-Type: application/x-webarchive^M
^M
aaaaaaaaaaaaaaaaaaaaaaa
------WebKitFormBoundaryHllRggx1NSzlMz0k--^M

之后我又尝试了nginx 的上传模块 nginx-upload-module
文件也能上传成功,但上传的文件也是出现了这样的问题。


下面是我的代码和配置

1、nginx 配置

# user  nobody;
worker_processes  1;



#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;


    #access_log  logs/access.log  main;

    sendfile        off;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;


        location / {
            root   /opt/www;
            index  index.html index.htm;
        }



        location  = /v1/upload {
               aio on;
               directio 10M;
               client_body_temp_path      /tmp/nginx 1;
               client_body_in_file_only   on;
               client_body_buffer_size    100M;
               client_max_body_size 10000M;
         
               proxy_pass_request_headers on;
               proxy_set_body             off;
               proxy_redirect             off;
               proxy_ignore_client_abort  on;
               proxy_http_version         1.1;
               proxy_set_header           Connection "";
               proxy_set_header           Host $host;
               ##proxy_set_header         Host $http_host;
               proxy_set_header           X-Real-IP $remote_addr;
               proxy_set_header           X-Forwarded-For $proxy_add_x_forwarded_for;
               proxy_set_header           X-Forwarded-Proto $scheme;
               proxy_set_header           X-File-Name $request_body_file;
               proxy_pass                 http://127.0.0.1:3389/v1/upload;
              # proxy_redirect             default;
         
               proxy_connect_timeout       600;
               proxy_send_timeout          600;
               proxy_read_timeout          600;
               send_timeout                600;
         
               access_log                  off;
               error_log                  /var/log/nginx/nginx.upload.error.log;
         
           }

    }

}

2、python 代码

# -*- coding: utf-8 -*-

import tornado.ioloop
import tornado.web
import tornado.gen
import os
import logging


settings = {"debug": True}


class UploadHandler(tornado.web.RequestHandler):
    @tornado.gen.coroutine
    def post(self):
        print self.request.headers
        print self.request.body

        tmp_file = self.request.headers.get("X-File-Name", "")
        print tmp_file

        d = {
            "code": "200",
            "response": "",
            "success": True,
        }
        self.write(d)

    def get(self):
        self.write('ok')

if __name__ == "__main__":
    application = tornado.web.Application([
        (r"/v1/upload", UploadHandler),
    ], **settings)
    application.listen(3389)
    tornado.ioloop.IOLoop.current().start()

3、前端代码

<!DOCTYPE html>
<html>
<head>
    <title> file upload test</title>
</head>
<body>

<form action="/v1/upload" enctype="multipart/form-data" method="post">
    <input type="file" name="file">
    <input type="submit" name="submit"> 
</form>

</body>
</html>
阅读 4.1k
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题