I am trying to create a multiuploader for Django that sends the files to the Dropbox API. Ther issue I have right now is that it works fine for any file but images. It does upload the file to my dropbox account, but the images cannot be opened. They seem to be corrupted.
This is the code that I am using
def multiuploader(request):
if request.method == 'POST':
user_profile = UserProfile.objects.get(user = request.user)
sess = session.DropboxSession(settings.DROPBOX_AUTH_KEY, settings.DROPBOX_AUTH_SECRET, access_type=settings.DROPBOX_ACCESS_TYPE)
sess.set_token(user_profile.dropbox_profile.access_token['key'], user_profile.dropbox_profile.access_token['secret'])
drop_client = client.DropboxClient(sess)
files = request.FILES.getlist(u'files[]')
for file in files:
folder = Project.objects.get(id=request.POST['project_id']).title
result_db = drop_client.put_file(settings.APP_NAME + '/' + folder + '/' + file.name, file.file)
destination = open(file.name , 'wb+')
for chunk in file.chunks():
destination.write(chunk)
destination.close()
#generating json response array
result = []
result.append({"files": [
{
"name": result_db['path'][result_db['path'].rfind('/') + 1:],
"size": result_db['bytes'],
"mime": result_db['mime_type']
}
]})
response_data = simplejson.dumps(result)
#checking for json data type
if "application/json" in request.META['HTTP_ACCEPT_ENCODING']:
mimetype = 'application/json'
else:
mimetype = 'text/plain'
return HttpResponse(response_data, mimetype=mimetype)
else: #GET
return HttpResponse('Only POST accepted')
Any idea?