我使用Django restful api + angularjs。 现在,我追加Django在宁静的API 使用angularjs上传文件到服务器上的Web前端,在HTML是:Django restful api无法获取文件内容
<input type="file" data-upload-file id="uploadInputFile">
的angularjs的代码是:
omapp.directive("uploadFile",function(httpPostFactory){
return {
restrict:"A",
scope:true,
link:function(scope,element,attr){
element.bind('change',function(){
var formData = new FormData();
formData.append('file', element[0].files[0]);
console.log(formData.get('file'))
httpPostFactory('/api/uploadfile/',formData.get('file'),function (callback) {
console.log(callback);
})
})
}
};
});
omapp.factory('httpPostFactory', function ($http,$cookies) {
return function (file, data, callback) {
var req = {
method:"POST",
url:file,
headers:{
'X-CSRFToken':$cookies.get('csrftoken')
},
data:{file:data}
}
$http(req)
.then(function successcallback(response){callback(response);})
}
});
一切都OK 。但Django的API无法获取数据 的代码是:
class UploadFile(APIView):
def post(self,request,format=None):
logger.info("file content");
logger.info(request.data)
return Response("upload done")
我怎样才能得到的数据?
“request.FILES [”文件']“,抓到一个错误:angular.js:14727可能未处理的拒绝... –
未处理的拒绝,它可能是500个关键错误.check您的浏览器控制台,您的请求数据包含文件内容, – Robert
我收到一个新错误:MultiValueDictKeyError at/api/uploadfile/ “'file'” –