【发布时间】:2018-08-22 19:23:36
【问题描述】:
我正在尝试使用 Android 的 ionic native。问题是,在控制台中显示 [object Object] Uploaded Successfully,但我的服务器上没有上传任何内容。
我在浏览器中检查了网络选项卡,它甚至没有调用上传 URL。
下面是我的home.html 代码:
<ion-content padding>
<ion-item>
<p>{{imageURI}}</p>
<button ion-button color="secondary" (click)="getImage()">Get Image</button>
</ion-item>
<ion-item>
<h4>Image Preview</h4>
<img src="{{imageFileName}}" *ngIf="imageFileName" alt="Ionic File" width="300" />
</ion-item>
<ion-item>
<button ion-button (click)="uploadFile()">Upload</button>
</ion-item>
</ion-content>
home.ts 代码:
import { Component } from '@angular/core';
import { NavController, LoadingController, ToastController } from 'ionic-angular';
import { FileTransfer, FileUploadOptions, FileTransferObject } from '@ionic-native/file-transfer';
import { Camera, CameraOptions } from '@ionic-native/camera';
@Component({
selector: 'page-home',
templateUrl: 'home.html'
})
export class HomePage {
imageURI:any;
imageFileName:any;
constructor(public navCtrl: NavController,
private transfer: FileTransfer,
private camera: Camera,
public loadingCtrl: LoadingController,
public toastCtrl: ToastController) {}
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 = imageData;
}, (err) => {
console.log(err);
this.presentToast(err);
});
}
uploadFile() {
let loader = this.loadingCtrl.create({
content: "Uploading..."
});
loader.present();
const fileTransfer: FileTransferObject = this.transfer.create();
let options: FileUploadOptions = {
fileKey: 'ionicfile',
fileName: 'ionicfile',
chunkedMode: false,
mimeType: "image/jpeg",
headers: {}
}
fileTransfer.upload(this.imageURI, 'http://example.com/upload2.php', options)
.then((data) => {
console.log(data+" Uploaded Successfully");
// this.imageFileName = "http://192.168.0.7:8080/static/images/ionicfile.jpg"
loader.dismiss();
this.presentToast("Image uploaded successfully");
}, (err) => {
console.log(err);
loader.dismiss();
this.presentToast(err);
});
}
presentToast(msg) {
let toast = this.toastCtrl.create({
message: msg,
duration: 6000,
position: 'bottom'
});
toast.onDidDismiss(() => {
console.log('Dismissed toast');
});
toast.present();
}
}
【问题讨论】:
标签: angular ionic-framework ionic2 ionic3