0

我正在尝试为 Django 创建一个多上传器,将文件发送到 Dropbox API。我现在遇到的另一个问题是它适用于除图像以外的任何文件。它确实将文件上传到我的保管箱帐户,但无法打开图像。他们似乎被破坏了。

这是我正在使用的代码

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')

任何想法?

4

1 回答 1

0

我不是 Django 专家,但是 SO question Django multiple files in one input can't be read by server建议您应该使用request.FILES.getList(<name>)并迭代您到达那里的文件。

于 2013-11-07T15:59:33.837 回答