【发布时间】:2018-02-05 12:23:57
【问题描述】:
我使用 Django restful api + angularjs 。 现在,我通过 Django restful api 附加使用 angularjs 将文件上传到服务器 在web前端,Html是:
<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);})
}
});
一切正常。但是 Django api 无法获取数据 代码是:
class UploadFile(APIView):
def post(self,request,format=None):
logger.info("file content");
logger.info(request.data)
return Response("upload done")
如何获取数据?
【问题讨论】:
标签: python angularjs django rest