使用base64将大图像发送到nginx + uwsig + django服务器

时间:2019-01-10 11:38:18

标签: python django python-requests

我正在使用base64将图像上传到Django服务器。当图像大小大于2M时,服务器无法获取图像。

我设置了uwsginginx的配置,并将上传大小设置为75M,但无法正常工作。

客户:

image1 = base64.b64encode(open(file_path, 'rb').read())
r = requests.post(url, data={"image": image1})

服务器:

result = request.POST.get("image")
Nginx:

```bash
server {
# the port your site will be served on
listen     ****;
# the domain name it will serve for
#server_name .example.com; # substitute your machine's IP address or FQDN
server_name ****;
charset utf-8;

# max upload size
client_max_body_size 75M;
}
```
uwsgi:

```bash
# ocr.ini file
# Django-related settings
# the base directory (full path)
chdir= /root/ubuntu
# Django's wsgi file
module= mysite.wsgi
# the virtualenv (full path)
home= /root/ubuntu
# process-related settings
# master
master= true
# maximum number of worker processes
processes= 32
max-requests= 10000
daemonize= /tmp/a.log
pidfile= /tmp/a.pid
#reload-on-as = 126
#reload-on-rss = 126
#enable-threads= true
# the socket (use the full path to be safe
socket= /root/ubuntu/mysite.sock
# ... with appropriate permissions - may be needed
chmod-socket= 666
# clear environment on exit
vacuum= true
limit-post= 20000000

harakiri=30
post-buffering=20000000
py-autoreload = 1
```

错误:     [1]:https://i.stack.imgur.com/HXDVY.png

1 个答案:

答案 0 :(得分:0)

您可能正在达到服务器库中某个位置的硬限制。如果您尝试上传如此大的文件,那么使用POST传递data参数中的所有内容是一个坏主意(而且也很慢)。

您应该使用files关键字参数来与requests发送,而将FILES属性用于Django。

客户:

r = requests.post(files={'image': open(file_path,'rb')})

服务器:

img = request.FILES.get("xxx") # <-- file name here

with open('path/to/destination', 'wb') as dest:
    for chunk in img.chunks():
        dest.write(chunk)

请查看Django文档上的File Uploads页面。