我似乎找不到任何信息,我得到的错误,当我试图复制一个文件与相机采取的数据存储。以下是我的功能(这几乎是每个人似乎都做的):
import { File } from '@ionic-native/file';
// stuff here
constructor(public navCtrl: NavController, public navParams: NavParams, private StorageProvider: StorageProvider, public viewCtrl: ViewController, private camera: Camera, public platform: Platform, private file: File{
}
// other stuff here
copyFileToLocalDir() {
let d = new Date();
let n = d.getTime();
let newFileName = n + ".jpg";
console.log('new name',newFileName);
this.file.copyFile(this.imagePath, this.imageName, cordova.file.dataDirectory, newFileName)
.then(success => {
this.lastImage = newFileName;
}, error => {
console.log('error saving');
});
}导致错误的行是:this.file.copyFile(...
在模拟器或设备上运行时,我遇到的错误是:错误: Uncaught (在承诺中):无效操作
离子信息:
cli packages: (/Users/billb/dev/customer-mkt-app/node_modules)
@ionic/cli-utils : 1.12.0
ionic (Ionic CLI) : 3.12.0
global packages:
cordova (Cordova CLI) : 7.0.1
local packages:
@ionic/app-scripts : 3.0.0
Cordova Platforms : android 6.2.3 ios 4.4.0
Ionic Framework : ionic-angular 3.3.0
System:
Android SDK Tools : 26.1.1
ios-deploy : 1.9.0
ios-sim : 5.0.10
Node : v7.8.0
npm : 4.6.1
OS : macOS Sierra
Xcode : Xcode 9.0 Build version 9A235
Misc:
backend : legacy有什么想法吗?这个函数copyFileToLocalDir()主要取自文件插件的几个例子,包括插件作者。
发布于 2017-10-06 21:51:03
因此,从上面大卫的评论,这促使我深入挖掘,更好地理解什么文件插件正在做的。多亏了here和here这两个问题的答案,我终于明白了这一点。这是我的最后代码:
copyFileToLocalDir() {
let d = new Date();
let n = d.getTime();
let newFileName = n + ".jpg";
// cordova.file.dataDirectory
let externalStoragePath: string = cordova.file.dataDirectory;
this.file.resolveLocalFilesystemUrl(this.imagePath + this.imageName)
.then((entry: any)=>{
console.log('entry',entry);
this.file.resolveLocalFilesystemUrl(externalStoragePath)
.then((dirEntry: any)=>{
entry.copyTo(dirEntry, newFileName, this.successCopy, this.failCopy);
}).catch((error)=>{
console.log(error);
});
}).catch((error)=>{
console.log(error);
});}
您必须创建两个对象,一个是当前文件(entry),另一个是将文件复制到(dirEntry)的路径。这是解决这个问题的关键。
https://stackoverflow.com/questions/46594304
复制相似问题