我想在Ionic 3中使用不同的扩展名(.jpg,.png,.doc,.pdf,)上传多个文件。我如何一次在服务器上上传多个文件。如果有任何解决方案,请尽快在这里分享。会非常感激。
我的代码在这里:
addImageORFileToEntry() {
console.log("addImageORFileToEntry");
if (this.plt.is('android')) {
console.log("add files or image in android");
this.fileChooser.open().then(uri => {
// console.log("file uri", uri);
this.filePath.resolveNativePath(uri).then((filePathResult)=>{
console.log("file filePathResult", filePathResult);
var fileName = filePathResult.substr(filePathResult.lastIndexOf('/') + 1);
console.log("android file fileName", fileName);
this.displayFile = fileName;
console.log("this.displayFile" +JSON.stringify(this.displayFile));
let onlyfilename = this.displayFile.split(".");
// console.log("onlyfilename[0]" +onlyfilename[0]);
let name = onlyfilename[0];
var fileExtension = fileName.substr(fileName.lastIndexOf('.') + 1);
console.log("android file fileExtension", fileExtension);
this.extension = fileExtension;
let filePathurl : string = filePathResult;
// console.log("file uri filePathurl", filePathurl);
this.base64.encodeFile(filePathurl).then((base64File: string) => {
// console.log("file uri base64File", base64File);
this.fileData = base64File;
var newStringVal = this.fileData.replace(/ /g, "");
// this.fileData = this.fileData.replace(/\s+/g, "");
console.log("file uri this.fileData", this.fileData);
let splitbase64 = this.fileData.split(',');
// console.log("add files in android splitbase64", splitbase64[1]);
this.getSplitbase64 = splitbase64[1];
// console.log("add files in android this.getSplitbase64", this.getSplitbase64);
// this.extensions.push({'extensions': this.extension})
this.photos.push({'filename' : name, 'filestring': newStringVal, 'filetype': this.extension});
console.log("file uri photos", this.photos);
}, (err) => {
console.log(err);
});
});
}).catch(err => {
console.log('Error', err);
});
} else if (this.plt.is('ios')) {
console.log("add files or image in ios");
this.filePicker.pickFile().then(uri => {
console.log("ios file uri" +JSON.stringify(uri));
let correctPath = uri.substr(0, uri.lastIndexOf('/') + 1);
console.log("ios file correctPath" +JSON.stringify(correctPath));
let currentName = uri.substring(uri.lastIndexOf('/') + 1);
console.log("ios file currentName" +JSON.stringify(currentName));
let onlyfilename = currentName.split(".");
console.log("onlyfilename[0]" +onlyfilename[0]);
let name = onlyfilename[0];
console.log("name" +name);
let extension = onlyfilename[1];
console.log("extension" +extension);
this.file.readAsDataURL("file:///" + correctPath, currentName).then((base64File: string) =>{
// console.log("ios file base64" +JSON.stringify(base64));
this.fileData = base64File;
var newStringVal = this.fileData.replace(/ /g, "");
console.log("ios file fileData" +this.fileData);
this.photos.push({'filename' : name, 'filestring': newStringVal, 'filetype': extension});
console.log("file uri photos", this.photos);
})
});
}
}
并且当我使用带有rowData的http post请求上传文件时,出现错误:
我认为大文件无法转换为有效的base64字符串。 它适用于某些文件,不适用于某些文件。我该如何解决这个问题。请帮我。