我想使用FormData将图像上传到我的后端,但由于Ionic DEVAPP和Ionic VIEW不支持文件,文件传输和文件上传插件,我需要只使用Angular Http或HttpClient
使用DestinationType.FILE_URI时,我可以从文件中获取内部网址 并将其显示在img对象上,但是如果没有本机文件,文件路径和文件传输插件,我无法从此URL创建一个打字稿文件对象。
getImage() {
const options: CameraOptions = {
quality: 100,
destinationType: this.camera.DestinationType.FILE_URI,
sourceType: this.camera.PictureSourceType.PHOTOLIBRARY
}
this.camera.getPicture(options).then((imageData) => {
this.imageURI = this.sanitizer.bypassSecurityTrustUrl(imageData)
}, (err) => {
console.log(err)
this.presentToast(err)
})
}
使用此模板
<ion-content padding>
<ion-item>
<p>{{imageFileName}}</p>
<button ion-button color="secondary" (click)="getImage()">Get Image</button>
</ion-item>
<ion-item>
<h4>Image Preview</h4>
<img style="display:block" [src]="imageURI" *ngIf="imageURI" alt="Ionic File" width="300" />
</ion-item>
<ion-item>
<button ion-button (click)="uploadFile()">Upload</button>
</ion-item>
</ion-content>
使用DestinationType.DATA_URL时,我可以显示图像,但不能创建原始文件名所需的打字稿文件对象,以将图像附加到我的Ionic App上传服务上使用的FormData。
我似乎可以找到一种方法,使用来自FILE_URI的原始文件名创建此打字稿文件对象,使用来自cordova相机本机插件的camera.getPicture创建来自DATA_URL的base64编码数据。
将文件上传到我的后端的服务只使用这种方法:
postImage(image: File): Observable<any> {
const formData = new FormData()
.append('file', image, image.name)
}
return this.httpClient.post<any>(this.myUrl,formData)
}
来自组件myPage.ts的getImage和来自uploadservice.ts的postImage都工作正常,但我找不到从camera.getPicture imageData
创建File对象的方法答案 0 :(得分:-1)
如果您不想使用本机插件,则可以始终使用angular。对于这种情况,请使用离子输入文件:
<ion-input type="file"></ion-input>
用角度完成剩下的工作: https://www.academind.com/learn/angular/snippets/angular-image-upload-made-easy/
您可以使用此示例:
https://github.com/diegogplfree/Ionic3-fileupload-non-native
亲切的问候。
答案 1 :(得分:-1)
答案大多来自here,但适用于@ionic-native/camera
插件getPicture
命令。
private urltoFile(url, filename, mimeType) {
return (fetch(url)
.then(function(res){return res.arrayBuffer();})
.then(function(buf){return new File([buf], filename, {type:mimeType});})
);
}
getPicture() {
const options: CameraOptions = {
quality: 50,
destinationType: this.camera.DestinationType.DATA_URL,
encodingType: this.camera.EncodingType.JPEG,
mediaType: this.camera.MediaType.PICTURE,
correctOrientation: true
}
this.camera.getPicture(options).then((imageData) => {
let base64Image = 'data:image/jpeg;base64,' + imageData
this.urltoFile(base64Image, 'filename.jpg', 'image/jpeg')
.then((file) => {
// You now have a file object that you can attach to a form e.g.
this.myForm.get("imageToUpload").setValue(file)
})
}, (err) => {
// Handle error
});
}
要注意的重点是
DATA_URL
data:image/jpeg;base64,
之前加imageData
答案 2 :(得分:-2)
我遇到了同样的问题,并且为我自己做了这件事。但要注意的一件事是我没有首先显示所选的图像。仅上传可见后。
openImage() {
const options: CameraOptions = {
quality: 100,
destinationType: this.camera.DestinationType.FILE_URI,
sourceType: this.camera.PictureSourceType.PHOTOLIBRARY,
correctOrientation: true,
allowEdit: false
};
this.camera.getPicture(options).then(imageData => {
this.ImageFile = imageData;
}, err => {
this.commonProvider.showToast('Error occured while opening the image' + err);
});
}
upload(msg: string) {
this.businessProvider.upload(this.Id, this.ImageFile)
.then(
data => {
let response = JSON.parse(data["response"]);
if (response.code === '1') {
this.commonProvider.showToast('Business successfully ' + msg);
this.navCtrl.pop();
} else {
this.commonProvider.showToast(response.message);
}
this.commonProvider.dismissLoader();
},
error => {
this.commonProvider.showToast(JSON.stringify(error));
this.commonProvider.dismissLoader();
}
);
}
这是我的业务提供商
import { Injectable } from '@angular/core';
import { HttpClient, HttpErrorResponse } from '@angular/common/http';
import { Observable } from 'rxjs/Observable';
import { ErrorObservable } from 'rxjs/observable/ErrorObservable';
import { catchError } from 'rxjs/operators';
import { Constant } from '../app/app.constants';
import { AuthenticationProvider } from '../providers/authentication';
import { FileTransfer, FileUploadOptions, FileTransferObject } from '@ionic-native/file-transfer';
@Injectable()
export class BusinessProvider {
private actionUrl: string;
constructor(
private http: HttpClient,
private auth: AuthenticationProvider,
private constant: Constant,
private transfer: FileTransfer
) {
this.actionUrl = this.constant.getServerWithApiUrl() + 'Business/';
}
upload(Id: number, ImageFile): any {
const fileTransfer: FileTransferObject = this.transfer.create();
const headers = this.auth.getHeader();
let options: FileUploadOptions = {
fileKey: 'ionicfile',
fileName: 'ionicfile.jpg',
chunkedMode: false,
mimeType: "image/jpeg",
headers: { headers }
}
return fileTransfer.upload(ImageFile, this.actionUrl + 'Upload/' + Id, options);
}
}