Reputation: 5800
Ok. So I've come up with the following code after searching a lot of forums and other posts in Stackoverflow. First of all I've tried using request.FILES.get('onefile')
but that only uploaded one file no matter how many I selected during the upload. So I've changed my code a bit to upload files with names like file1, file2, etc. The following are my codes, but I don't know why this is not working. No file is currently being uploaded and it shows an error listed under the following code.
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),
]
error
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
I'm not sure whether I've missed something. Please let me know if you need anything else. Thanks in advance!
Upvotes: 1
Views: 909
Reputation: 77892
I'm not sure whether I've missed something.
Yes : reading the error message - which tells you what the error is -, the traceback - which tells you where the error happened - and then re-reading your code. So you have:
Exception Type: TypeError Exception Value:
cannot concatenate 'str' and 'int' objects
Which means you tried to concatenate a string and an integer (which Python does not allow for a quite obvious reason).
and
Exception Location: /home/michel/django/upload/index/views.py in handle_uploaded_file, line 32
which means the error is in /home/michel/django/upload/index/views.py at line 32.
Line 32 of /home/michel/django/upload/index/views.py is :
with open('/home/michel/django/upload/media/file' + count, 'wb+') as destination:
Obviously, '/home/michel/django/upload/media/file' + count
is the culprit. Now you just have to fix this, either making a string of count
or using string formating - both explained in the FineManual(tm).
While you're at it, you may also want to read about the builtin enumate(sequence)
function.
Upvotes: 1
Reputation: 1063
It concatenate error,you cannot concatenate 'str' and 'int' objects.
I thinks problem is at this line
'/home/michel/django/upload/media/file' + count
'/home/michel/django/upload/media/file' + str(count)
Example:
a = 'test'
b = 1
c = a+b
Type Error: cannot concatenate 'str' and 'int' objects
a = 'test'
b = str(1) or '1'
c = a+b(works fine)
Upvotes: 1