我用WordPress制作Ionic3。
我的问题是文件上传。
我更改了服务器(bitnami).htaccess,还修改了主题中的功能。
ionic3和native @ file-transfer
我跟进this code。
版本 “ @ ionic-native / transfer”:“ ^ 3.13.0” “ @ ionic-native / file-transfer”:“ ^ 4.1.0” “ @ ionic-native / file”:“ ^ 4.1.0” “ cordova-android”:“ 6.3.0” “离子角”:“ 3.9.2” 科尔多瓦版本:9.0.0 离子版本:3.8.1
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/png",
headers: {
"Authorization" : "Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwczpcL1wvZG9ja29hLmNvbSIsImlhdCI6MTU1OTEzMzQyMiwibmJmIjoxNTU5MTMzNDIyLCJleHAiOjE1NTk3MzgyMjIsImRhdGEiOnsidXNlciI6eyJpZCI6IjE3In19fQ.OSkEiCArmWIdrN-ZxCPBQsihoKuciNhB5dP9WYiXRwY",
"Content-Disposition": "attachment; filename=\'nowtest.png\'",
"Content-Type": "image/png"
}
}
fileTransfer.upload(this.imageURI,'https://dockoa.com/wp-json/wp/v2/media/', options)
.then((data) => {
console.log(data+" Uploaded Successfully");
this.imageFileName = "file:///storage/emulated/0/Android/data/io.ionic.starter/cache/1555841579665.png"
loader.dismiss();
this.presentToast("Image uploaded successfully");
}, (err) => {
console.log(err);
alert(JSON.stringify(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();
}
}
其余代码是
body:"{"code":"rest_upload_sideload_error","message":"Sorry, this file type is not permitted for security reasons.","data":{"status":500}}"
code:1