4

Here is my phonegapfile file : this file is sending data to django server but i am not able to store the file in server.what will be python views function for catching and storing the file?

<!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')

That upload request hit the server and i am getting the print "-----------------hitting the url" So how can i catch the file here? or otherwise is there any way to store the file in my server folder??

1
  • I'd suggest diving into the debugger: Just add 'import pdb; pdb.set_trace()' between the print statement and the to_json statement. Start your django server with "./manage.py runserver", upload the file and you will have a nice debugger on your terminal where you can inspect things like 'dir(request)' etc. Commented Apr 24, 2013 at 8:32

1 Answer 1

3

If you are using JQuery to send your file to server using POST than use request.FILES['file'] in place of 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()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.