我试着使用cordova-plugin-file-transfer上传我的base64格式的图片到我的服务器上,直到现在它还不能工作。我的代码是这样的:
photoBase64 = photoBase64.replace('data:image/png;base64,', '');
var url = "http://MYURL.com/path";
var options = new FileUploadOptions();
options.fileKey = "file";
options.fileName = "photoName.png";
options.mimeType = "image/png";
var ft = new FileTransfer();
ft.upload(photoBase64,
encodeURI(url),
function(result) {
console.log("Code = " + result.responseCode);
console.log("Response = " + result.response);
console.log("Sent = " + result.bytesSent);
resolve("OK");
},
function(error) {
alert("An error has occurred: Code = " + error.code);
console.error("ERROR", error);
console.log("upload error source " + error.source);
console.log("upload error target " + error.target);
reject("ERROR");
},
options);我得到了以下代码的错误:

如何使用cordova- base64 -file-transfer上传图片插件?
提前感谢!
发布于 2017-05-12 06:41:55
我迟到了一年,但我只是通过反复试验才找到了答案:
您必须在字符串中保留"data:image/png;base64“。我假设如果没有该格式,它就不是有效的url。
具体到您的情况,请删除此行:
photoBase64 = photoBase64.replace('data:image/png;base64,', '');对我来说,让我的上传工作真的就这么简单。
发布于 2016-05-06 19:36:47
文件传输插件不接受Base64字符串。您必须使用文件的位置( file :://android/etc)。有关获取文件的文件插件(此插件随filetransfer插件自动安装)的更多信息,请参阅此处:
https://github.com/apache/cordova-plugin-file
如果您确实希望只使用api字符串,则必须使用$http.post并在接收端编写一个base64来重新创建文件。
https://stackoverflow.com/questions/37063410
复制相似问题