【问题标题】:Django request.FILES gets name, filename but not file (blob)Django request.FILES 获取名称、文件名但不是文件(blob)
【发布时间】:2020-08-17 02:17:03
【问题描述】:

我正在尝试通过 POST 请求将音频文件发送到服务器(aws、ec2)并且我正在使用 Django,但我的 request.FILES 没有收到 blob 文件,但它确实收到了密钥和文件名。 当我在 localhost 上运行它时一切正常。

如何获取文件?

我正在 chrome://flags/#unsafely-treat-insecure-origin-as-secure 上启用我的网站,以便我可以访问麦克风。

使用 RecorderJs 生成包含 WAV 格式录制音频的 Blob 对象。

Main.js

rec.exportWAV(function(blob){
...
var fd = new FormData();
fd.append('text', speech);
fd.append('audio', blob, 'test.wav');
$.ajax({
 type: 'POST',
 enctype: 'multipart/form-data',
 url: url,
 data: fd,
 processData: false,
 contentType: false,
 success: function(response) {
  console.log(response);
 }
})
...

语音是字符串,

控制台中的 blob 是 Blob {size: 221228, type: "audio/wav"},所以它确实存在。

Views.py:

@csrf_exempt
def get_blob(request):
    thislist = []
    for key in request.POST:
       thislist.append(request.POST.get(key))
    for key in request.FILES:
       thislist.append(request.FILES.get(key).name)

    json_stuff = json.dumps({"check": thislist})
    return HttpResponse(json_stuff, content_type="application/json")

我尝试过使用和不使用 enctype,没有任何区别。 我尝试将 contentType 设置为 multipart/form-data,没有任何区别。

formdata 似乎发送正确,因为我可以正确获取语音(request.POST)。

我可以从 request.FILES ('audio') 中获取密钥,并获取文件名 ('test.wav')。

如果我尝试request.FILES['audio'].read(),它会显示 MultiValueDictError。

如果我尝试request.FILES.get('audio').read(),它会显示 AttributeError,'NoneType' 对象没有属性 'read'。

当我打印 request.POST 时,我确实得到了带有“文本”的字典:无论我说什么文本

当我打印 request.FILES 时,我得到一个空字典,即使我可以通过它获取密钥和文件名 for key in request.FILES:request.FILES['audio'].filename

有人知道发生了什么和/或可以帮助我解决问题吗?

【问题讨论】:

    标签: javascript python jquery django ajax


    【解决方案1】:

    您可以使用read() 方法:

    @csrf_exempt ## it doesn't work for post requests in later Django versions, so you need to disable it in another way or add a token to all post requests
     def get_blob(request):
        thislist = []
        for key in request.POST:
           thislist.append(request.POST.get(key))
        for key in request.FILES:
           thislist.append(request.FILES.get(key).name)
        # I am not sure that "name" exists in the request — you may use any filename, although the following works with multipart requests.
        with open(request.FILES["name"],"wb+") as f:
           f.write(request.FILES['file'].read())
        json_stuff = json.dumps({"check": thislist})
        return HttpResponse(json_stuff, content_type="application/json")
    
    

    顺便说一句, @csrf_exempt — 在以后的 Django 版本中不适用于发布请求,因为在调用视图之前之前检查了令牌。因此,您可能需要禁用 CSRF 中间件或只为所有请求添加正确的“X-CSRFToken”令牌。

    【讨论】:

      猜你喜欢
      • 2011-03-07
      • 2015-03-26
      • 2014-06-22
      • 1970-01-01
      • 2019-03-22
      • 2014-04-10
      • 2012-10-24
      • 1970-01-01
      相关资源
      最近更新 更多