如何使用PhoneGap的在Django服务器上传文件?如何使用、上传文件、服务器、Django

由网友(青春散场ミ不诉离殇)分享简介:下面是我的phonegapfile文件:这个文件将数据发送到Django的服务器,但我不能够存储在server.what该文件将蟒蛇的看法功能用于捕获和存储文件Here is my phonegapfile file : this file is sending data to django server but i...

下面是我的phonegapfile文件:这个文件将数据发送到Django的服务器,但我不能够存储在server.what该文件将蟒蛇的看法功能用于捕获和存储文件

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

Views.py

@csrf_exempt

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

这是上传请求点击服务器和我正在打印-----------------打的URL 那么,如何能赶上这里的文件? 或有什么办法将文件保存在我的服务器上的文件夹?

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??

推荐答案

如果您正在使用jQuery使用POST不是用你的文件发送到服务器 request.FILES ['文件'] request.POST []

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()
阅读全文

相关推荐

最新文章