0

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?

Markinhos
  • 736
  • 1
  • 6
  • 13

1 Answers1

0

I'm not a Django expert, but the SO question Django multiple files in one input can't be read by server suggests that you should be using request.FILES.getList(<name>) and iterating over the files you get there.

Community
  • 1
  • 1
user94559
  • 59,196
  • 6
  • 103
  • 103
  • Thanks, but I already tried it before. It turns out that for this request is not supported and gives `{Attribute error}'MultiValueDict' object has no attribute 'getList'` – Markinhos Nov 07 '13 at 18:06
  • Well, last error was a stupid mistake. The correct function is getlist, but the problem persist. – Markinhos Nov 07 '13 at 18:25
  • Maybe you can update the code in your question then, to match your current code? Presumably you now have a loop somewhere, right? – user94559 Nov 07 '13 at 19:05
  • updated. Now there is a loop over the files, but as I said before the error is still there. – Markinhos Nov 08 '13 at 09:22
  • Have you tried looking at the length of each `file` object to see if it matches what you expect? (I'm trying to separate out the Dropbox part from the Django part.) – user94559 Nov 08 '13 at 15:39
  • I have put some code to save the file in order to save the file in the system, and it works fine. This is very strange to me because the app hosted in Heroku works fine. It has to be some change on Dropbox API, because this used to work. – Markinhos Nov 11 '13 at 20:12
  • Are you 100% sure the (working) code you're running in Heroku is identical to the code you're running locally? – user94559 Nov 12 '13 at 03:53
  • I had updated the Dropbox SDK, but I almost sure I did it on both sides. Anyway this solved, as I updated the code to use OAuth2. – Markinhos Nov 12 '13 at 18:24