使用phonegap upload filetransfer和django时获得禁止响应
我想使用phonegap的filetransfer上载方法上载图像这是我的html客户端代码: 文件传输示例使用phonegap upload filetransfer和django时获得禁止响应,django,cordova,upload,django-csrf,Django,Cordova,Upload,Django Csrf,我想使用phonegap的filetransfer上载方法上载图像这是我的html客户端代码: 文件传输示例 <script type="text/javascript" charset="utf-8" src="phonegap-1.0.0.js"></script> <script type="text/javascript" charset="utf-8"> // Wait for PhoneGap to load // doc
<script type="text/javascript" charset="utf-8" src="phonegap-1.0.0.js"></script>
<script type="text/javascript" charset="utf-8">
// Wait for PhoneGap to load
//
document.addEventListener("deviceready", onDeviceReady, false);
// PhoneGap is ready
//
function onDeviceReady() {
// Retrieve image file location from specified source
navigator.camera.getPicture(uploadPhoto,
function(message) { alert('get picture failed'); },
{ quality: 50,
destinationType: navigator.camera.DestinationType.FILE_URI,
sourceType: navigator.camera.PictureSourceType.PHOTOLIBRARY }
);
}
function uploadPhoto(imageURI) {
var options = new FileUploadOptions();
options.fileKey="file";
options.fileName=imageURI.substr(imageURI.lastIndexOf('/')+1);
options.mimeType="image/jpeg";
var params = new Object();
params.value1 = "test";
params.value2 = "param";
options.params = params;
var ft = new FileTransfer();
ft.upload(imageURI, "http://myserverUrl/uploadview", win, fail, options);
}
function win(r) {
console.log("Code = " + r.responseCode);
console.log("Response = " + r.response);
console.log("Sent = " + r.bytesSent);
}
function fail(error) {
alert("An error has occurred: Code = " = error.code);
}
</script>
</head>
<body>
<h1>Example</h1>
<p>Upload File</p>
</body>
</html>
//等待PhoneGap加载
//
文件。添加的监听器(“deviceready”,OnDeviceraddy,false);
//PhoneGap已经准备好了
//
函数ondevicerady(){
//从指定源检索图像文件位置
navigator.camera.getPicture(上传照片,
函数(消息){alert('get picture failed');},
{质量:50,
destinationType:navigator.camera.destinationType.FILE\u URI,
sourceType:navigator.camera.PictureSourceType.PHOTOLIBRARY}
);
}
函数上载照片(imageURI){
var options=new FileUploadOptions();
options.fileKey=“file”;
options.fileName=imageURI.substr(imageURI.lastIndexOf('/')+1);
options.mimeType=“image/jpeg”;
var params=新对象();
params.value1=“测试”;
params.value2=“param”;
options.params=参数;
var ft=新文件传输();
ft.upload(imageURI,“http://myserverUrl/uploadview“、胜利、失败、选择权);
}
函数win(r){
console.log(“Code=“+r.responseCode”);
console.log(“Response=“+r.Response”);
console.log(“Sent=“+r.bytesent”);
}
功能失败(错误){
警报(“发生错误:Code=“=error.Code”);
}
例子
上传文件
在服务器端,我有一个禁止的HttpResponse,我知道这是由于csrf属性造成的,但我不知道如何解决它,因为我的客户端不在django模板中,一种方法是禁用该视图的csrf检查。 选中此项: