Cordova 3.4.0中未定义FileTransfer错误

时间:2014-04-12 05:20:36

标签: cordova phonegap-plugins file-transfer

我在使用android的cordova 3.4.0中工作。我添加了FileTransfer插件用于图像上传,图像获取从库和相机工作正常,但当尝试上传其未上传时给出错误的Logcat: processMessage失败:错误:ReferenceError:FileTransfer未定义在file:///android_asset/www/js/cordova.js:1035 我在config.xml和AndroidMainfest.xml

中完成了所有设置

config.xml中

 <feature name="File">
    <param name="android-package" value="org.apache.cordova.file.FileUtils" />
    <param name="onload" value="true" />
 </feature>
 <feature name="FileTransfer">
    <param name="android-package" value="org.apache.cordova.filetransfer.FileTransfer" />
 </feature>

AndroidMainfest.xml

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

cordova_plugins.js

{
    "file": "plugins/org.apache.cordova.file-transfer/www/FileTransfer.js",
    "id": "org.apache.cordova.file-transfer.FileTransfer",
    "clobbers": [
        "window.FileTransfer"
    ]
},

图片上传代码

var ft = new FileTransfer();                     
var options = new FileUploadOptions();                      
options.fileKey="vImage1";                      
options.fileName=imagefile.substr(imagefile.lastIndexOf('/')+1);
options.mimeType="image/jpeg"; 
var params = new Object();
params.value1 = "test";
params.value2 = "param";                       
options.params = params;
options.chunkedMode = false;                       
ft.upload(imagefile, serviceURL, win, fail, options); 

那么代码有什么问题? ..提前致谢。

解决:

我已经解决了这个问题。我在html页面中添加了以下插件链接。

<script type="text/javascript" charset="utf-8" src="plugins/org.apache.cordova.file/www/File.js"></script>
<script type="text/javascript" charset="utf-8" src="plugins/org.apache.cordova.file-transfer/www/FileTransfer.js"></script>

2 个答案:

答案 0 :(得分:0)

我不知道你错了什么

但是这段代码工作正常

var options = new FileUploadOptions();
       options.fileKey = "file";
       options.fileName = uploadimageurl.substr(uploadimageurl.lastIndexOf('/') + 1);


       var params = {};
       params.value1 = "test";
       params.value2 = "param";

       options.params = params;
       var url=unlserviceurl+""+uploadarray.fileurl;
       var ft = new FileTransfer();
       ft.upload(uploadimageurl, encodeURI(url), win, fail, options);

var win = function (r) {
    console.log("Code = " + r.responseCode);
    console.log("Response = " + r.response);
    console.log("Sent = " + r.bytesSent);
}

var fail = function (error) {
    alert("An error has occurred: Code = " + error.code);
    console.log("upload error source " + error.source);
    console.log("upload error target " + error.target);
}

尝试使用此代码

答案 1 :(得分:0)

可以肯定的是:您是否已将cordova插件File和Filtransfer添加到项目中?