【发布时间】:2015-08-08 03:57:47
【问题描述】:
好的。因此,在 Stackoverflow 中搜索了很多论坛和其他帖子后,我想出了以下代码。首先,我尝试使用request.FILES.get('onefile'),但无论我在上传过程中选择了多少个文件,它都只上传了一个文件。因此,我对代码进行了一些更改,以上传名称为 file1、file2 等的文件。以下是我的代码,但我不知道为什么这不起作用。当前没有文件正在上传,它显示以下代码下列出的错误。
upload2.html
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<form method="post" action="/index/multi/" enctype="multipart/form-data"> {% csrf_token %}
<input type="file" name="onefile" multiple>
<input type="submit" value="Upload">
</form>
</body>
</html>
views.py
from django.shortcuts import render
from django.http import HttpResponse
def upload2(request):
return render(request, "upload2.html", {})
def multi(request):
count = 1
for x in request.FILES.getlist('onefile'):
print request.FILES.getlist('onefile')
def handle_uploaded_file(f):
with open('/home/michel/django/upload/media/file' + count, 'wb+') as destination:
for chunk in f.chunks():
destination.write(chunk)
handle_uploaded_file(x)
count = count + 1
return HttpResponse('Uploaded!')
urls.py
from django.conf.urls import url
from index import views
urlpatterns = [
url(r'^upload2/$', views.upload2),
url(r'^multi/$', views.multi),
]
错误
TypeError at /index/multi/
cannot concatenate 'str' and 'int' objects
Request Method: POST
Request URL: http://localhost:8000/index/multi/
Django Version: 1.8.1
Exception Type: TypeError
Exception Value:
cannot concatenate 'str' and 'int' objects
Exception Location: /home/michel/django/upload/index/views.py in handle_uploaded_file, line 32
我不确定我是否错过了什么。如果您需要其他任何东西,请告诉我。提前致谢!
【问题讨论】: