使用phonegap上传文件传输和django时获得禁止响应

时间:2013-11-18 13:06:06

标签: 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
    //
    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>

在服务器端我有一个forbiddenhttpresponse,我知道这是由于csrf属性但我不知道如何解决它,因为我的客户端不在django模板

1 个答案:

答案 0 :(得分:0)

一种方法是禁用该视图的CSRF检查。 检查一下:

https://stackoverflow.com/a/16458216/273119