【发布时间】:2015-10-04 08:40:14
【问题描述】:
到目前为止,我已经通过终端使用 django rest 框架上传文件,像这样:
curl -X POST -S -H -u -F "file=@image.jpg;type=image/jpg" http://127.0.0.1/upload.
但是我怎样才能通过 javascript json 使用这个命令呢? 我收到不支持的文件代码 415 之类的错误?
谁能帮帮我?谢谢
【问题讨论】:
-
你能解释一下你到底想做什么吗?
-
我使用 django rest 框架通过 phonegap 与我的后端 Django 进行通信。现在我想上传文件以使用 django rest 框架。通过终端它可以工作。但我无法在前端使用标准表单并通过 post JSON 或类似的方式上传。
-
这是我的后端:
class FileUploadView(APIView): parser_classes = (JSONParser, MultiPartParser, FormParser,) def put(self, request, format='jpg'): print request.FILES up_file = request.FILES['file'] destination = open('/var/www/Test/media/' + up_file.name, 'wb+') for up_file.chunks() 中的块:destination.write(chunk) 目标.close() 返回响应(up_file.name, status.HTTP_201_CREATED) -
这是我的前端:function upload(){ var photo = document.getElementById("upload"); var 文件 = photo.files[0]; var ajaxRequest = $$.ajax({ type: "PUT", url: "127.0.0.1/upload", contentType: 'application/json', processData: false, file: files, }); } }
标签: javascript python json django django-rest-framework