4

これが私のphonegapfileファイルです:このファイルはデータをdjangoサーバーに送信していますが、ファイルをサーバーに保存できません.ファイルをキャッチして保存するためのpythonビュー関数は何ですか?

<!DOCTYPE HTML>
<html>
<head>
<title>File Transfer Example</title>

<script type="text/javascript" charset="utf-8" src="cordova-2.5.0.js"></script>
<script type="text/javascript" charset="utf-8">

// Wait for Cordova to load
//
document.addEventListener("deviceready", onDeviceReady, false);

// Cordova 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 = {};
    params.value1 = "test";
    params.value2 = "param";

    options.params = params;

    var ft = new FileTransfer();
    ft.upload(imageURI, encodeURI("http://something.com/uploadphonegapfil/"),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);
    console.log("upload error source " + error.source);
    console.log("upload error target " + error.target);
}
</script>
</head>
<body>
<h1>Example</h1>
<p>Upload File</p>
</body>
</html>

Views.py

@csrf_exempt

def uploadPhonegapFile(request):
    print "-------- hitting the url"
    to_json={}
    return HttpResponse(simplejson.dumps(to_json), mimetype= 'application/json')

そのアップロード要求がサーバーにヒットし、「------------------ URL をヒットしています」という出力が表示されるので、ここでファイルをキャッチするにはどうすればよいですか? または、サーバーフォルダーにファイルを保存する方法はありますか??

4

1 に答える 1

3

JQuery を使用して POST を使用してファイルをサーバーに送信する場合はrequest.FILES['file']request.POST[].

if request.method == 'POST':
    filename=request.FILES['file']
    form = SomeForm(request.POST, request.FILES)
    if form.is_valid():
        dest_file = open('C:/system/upload/'+ str(filename), 'wb+')
        path = 'C:/system/upload/upload/'+ str(filename)
        for chunk in  request.FILES['file'].chunks():
            dest_file.write(chunk)
        dest_file.close()
于 2013-04-25T10:12:25.940 に答える