我正在使用base64
将图像上传到Django服务器。当图像大小大于2M时,服务器无法获取图像。
我设置了uwsgi
和nginx
的配置,并将上传大小设置为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
```
答案 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页面。