Angular 如何使用ionic将图像从相机存储到服务器
要从相机中检索图像并将其显示在第页。单击“保存”按钮,希望在服务器上存储相同的图像 保存时出现以下错误Angular 如何使用ionic将图像从相机存储到服务器,angular,ionic-framework,ionic4,Angular,Ionic Framework,Ionic4,要从相机中检索图像并将其显示在第页。单击“保存”按钮,希望在服务器上存储相同的图像 保存时出现以下错误 FileTransferError: body: null code: 1 exception: null http_status: null source: "data:image/jpeg;base64,iVBORw0KGgoAAAANSUhEUgAAAUA..." target: "https://www.myweb.com/api/product/storesimage/images"
FileTransferError:
body: null
code: 1
exception: null
http_status: null
source: "data:image/jpeg;base64,iVBORw0KGgoAAAANSUhEUgAAAUA..."
target: "https://www.myweb.com/api/product/storesimage/images"
这是我的拍照代码
takePhoto(){
const options: CameraOptions = {
quality: 100,
destinationType: this.camera.DestinationType.FILE_URI,
encodingType: this.camera.EncodingType.JPEG,
mediaType: this.camera.MediaType.PICTURE
}
this.camera.getPicture(options).then((imageData) => {
this.Photo = 'data:image/jpeg;base64,' + imageData;
}, (err) => {
});
}
保存图像代码
saveImage() {
const uploadOpts: FileUploadOptions = {
fileKey: "file",
fileName: 'name.jpg',
chunkedMode: false,
mimeType: "multipart/form-data"
}
const fileTransfer: FileTransferObject = this.transfer.create();
fileTransfer.upload(this.Photo, 'https://www.myweb.com/api/product/storesimage/images', uploadOpts)
.then((data) => {
console.log(data);
}, (err) => {
console.log(err);
});
}
使用ionic文件传输时,请查看ionic文档以了解详细信息。如你所知。 看 还有一个问题是,您必须提供tansfer服务。在app.module或组件的模块中添加提供程序: 从“./提供程序/文件传输”导入{Transfer}; 在你的app.module中。以及 提供程序:[{provide:ErrorHandler,useClass:IonicerHandler},存储,传输] 如果没有提供程序,则会出现空指针异常!获取自己的服务实例需要提供程序条目。 我希望这有帮助