public async takePicture(sourceType) {
const options = {
quality: 100,
sourceType: sourceType,
saveToPhotoAlbum: false,
correctOrientation: true,
};
try {
const imagePath = await this.camera.getPicture(options);
let uploadedImage;
if (this.platform.is("android") && sourceType === this.camera.PictureSourceType.PHOTOLIBRARY) {
const filePath = await this.filePath.resolveNativePath(imagePath);
const correctPath = filePath.substr(0, filePath.lastIndexOf("/") + 1);
const currentName = imagePath.substring(imagePath.lastIndexOf("/") + 1, imagePath.lastIndexOf("?"));
uploadedImage = await this.copyFileToLocalDir(correctPath, currentName, this.createFileName());
} else {
const currentName = imagePath.substr(imagePath.lastIndexOf("/") + 1);
const correctPath = imagePath.substr(0, imagePath.lastIndexOf("/") + 1);
uploadedImage = await this.copyFileToLocalDir(correctPath, currentName, this.createFileName());
}
console.log('PATH', cordova.file.dataDirectory + uploadedImage);
this.crop.crop(cordova.file.dataDirectory + uploadedImage, {quality: 75}).then(
newImage => {
this.uploadedImage = newImage.replace("file://", "");
},
error => {
this.presentToast("Error while selecting image.");
}
);
} catch (err) {
this.presentToast("Error while selecting image.");
}
}
public async copyFileToLocalDir(namePath, currentName, newFileName): Promise<string> {
const externalStoragePath: string = cordova.file.dataDirectory;
try {
const entry = await this.file.resolveLocalFilesystemUrl(namePath + currentName);
const dirEntry: any = await this.file.resolveLocalFilesystemUrl(externalStoragePath);
entry.copyTo(dirEntry, newFileName, () => { }, () => {
this.presentToast("Error while storing file.");
});
return newFileName;
} catch (error) {
this.presentToast("Error while storing file.");
}
}
我把文件放在一个类似文件的路径中:///var/mobile/Containers/Data/Application/1FF313F5-5736-44D0-968D-37889E3ED537/Library/NoCloud/1536106729187.jpg或/var/mobile/../tmp/某物.jpg
我试着用以下方式上传:
const options = {} as any;
const formData = new FormData();
const file = new File();
formData.append("fsFile", this.uploadedImage, "yes");
this.httpClient.post("/files/upload_tmp", formData, options)
.toPromise()
.catch((e) => {
console.log(e);
});
谢谢你