1

我正在使用 xcode 在 mac os 中开发一个 phonegap 项目。在xcode中,如果我创建一个基于cordova的应用程序,它会自动创建cordova-1.6.0.js。我正在使用 fileupload 插件将 svg 文件发送到我的服务器。在 fileupload.js 中,我编写了如下的警报 fileuplaoder 函数,

 var FileUploader = function() { 
    alert("gi");
}

此警报正在工作,但是当我在上传功能下给出警报时,

FileUploader.prototype.upload = function(server, file, params, fileKey, fileName, mimeType, success, fail, progress) {
    alert("upload");
    this._doUpload('upload', server, file, params, fileKey, fileName, mimeType, success, fail, progress);
};

此警报不起作用。我在 html 页面中对这个插件的调用是,

window.plugins.fileUploader.upload('http:192.168.1.54:8080/downloadFiles', '/Users/User/Library/Application Support/iPhone Simulator/5.0/Applications/408DBBC7-67F7-4E8B-B41C-663CDC0377B5/Documents/image5_1.jpg.txt.svg', {foo: 'bar'}, 'myPhoto', 'image5_1.jpg.txt.svg', 'image/svg', 
                                                function(result) {
                                                console.log('Done: ' + result);
                                                }, 
                                                function(result) {
                                                console.log("Error: " + result);
                                                }, 
                                                function(loaded, total) {
                                                var percent = 100 / total * loaded;
                                                console.log('Uploaded  ' + percent);

                                                }
                                                );

在 fileupload.js 中有 cordova.addConstructor 方法。但是在我生成的cordova.1.6.0.js 文件中没有这样的方法。我不知道发生了什么。请帮助我使用这个插件。

4

1 回答 1

0

我找到了解决方案。科尔多瓦文件 api 本身有一个上传和下载选项。它工作正常。代码是,

document.addEventListener("deviceready", onDeviceReady, false);

            // Cordova is ready
            //
            function onDeviceReady() {
                window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);
            }

            function gotFS(fileSystem) {
                fileSystem.root.getFile("image5_2.jpg.svg", {create: true, exclusive: false}, gotFileEntry, fail);
            }

            function gotFileEntry(fileEntry) {
                var localpath=fileEntry.fullPath;
                uploadPhoto(localpath);
                //fileEntry.createWriter(gotFileWriter, fail);
            }

            function uploadPhoto(imageURI) {
                alert(imageURI);
                var options = new FileUploadOptions();
                options.fileKey="file";
                options.fileName="image5_2.jpg.svg";
                options.mimeType="image/svg+xml";

                var params = new Object();
                params.value1 = "test";
                params.value2 = "param";

                options.params = params;

                var ft = new FileTransfer();
                ft.upload(imageURI, "http://192.168.1.54:8080/POC/fileUploader", 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);
            }

请使用这个。

于 2012-06-16T09:46:04.903 回答