在Windows Phone上使用PhoneGap创建文件并上传

时间:2015-10-14 11:17:27

标签: cordova phonegap-plugins

我在Windows Phone上使用Phonegap,我需要创建一个文件然后可以上传到服务器(我通常会发布数据,但我正在使用的服务器对POST请求的大小有限制因为一些不明原因)。有没有人对如何实现这一点有任何想法?

1 个答案:

答案 0 :(得分:0)

使用W3C文件API(available in Cordova)和Cordova File Transfer plugin的组合看起来可以做到这一点。

1)安装File Transfer plugin

  

cordova插件添加cordova-plugin-file-transfer

2)使用类似于以下内容的代码

我很想使用async之类的东西来提高它的可读性。

注意:此代码假定文件作为名为“file”的附件上传(您需要在服务器上查找该文件)。

// 1) Request access to the file system
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fileSystem){

    // 2) Create a new file
    fileSystem.root.getFile("file.json", {create: true, exclusive: false}, function(fileEntry){

        console.log("We now have access to the file: " + fileEntry.fullPath + " -- " + fileEntry.toURL());

        // 3) Create a file write object
        fileEntry.createWriter(function(writer){

            // 4) Write the information to the file
            writer.write("File contents go here");
            writer.onwriteend = function(evt) {

                // 5) We've written the data to the file. Now upload.
                var url = "http://serverurl.com";
                var fileURL = fileEntry.toURL();    // Local file URL

                var options = new FileUploadOptions();
                options.fileKey = "file";
                options.fileName = fileURL.substr(fileURL.lastIndexOf('/') + 1);
                options.mimeType = "text/plain";

                // Add any headers, if necessary
                var headers = {
                        "HEADER": "value"
                };
                options.headers = headers;

                console.log("Uploading file at: " + fileEntry.fullPath + " to " + fileURL);
                var ft = new FileTransfer();
                ft.upload(fileURL, encodeURI(url), function(r){

                    // Upload was successful. You can now do something with the response
                    // This code assumes the response is in JSON format.
                    console.log("Code = " + r.responseCode);
                    console.log("Response = " + r.response);
                    console.log("Sent = " + r.bytesSent);

                    var response = JSON.parse(r.response);
                    alert(response.success);

                }, 
                function(error){
                    // An error occurred when attempting to upload. Refer to the following page for an
                    // explanation of the error codes: https://github.com/apache/cordova-plugin-file-transfer
                    console.log("An error has occurred: Code = " + error.code);
                    console.log("upload error source " + error.source);
                    console.log("upload error target " + error.target);

                    alert("Unable to upload file");
                }, options);

            };

        }, function(error){
            console.log("Unable to create file writer: " + error);  
        });

    }, function(error){
        console.log("Unable to create file: " + error);
    });
}, function(error){
    console.log("Unable to access file system: " + error);
});