从gallary选择图片上传时,会出错。以下是我的home.ts
和<?php
header('Access-Control-Allow-Origin: *');
$target_path = "uploads/";
$target_path = $target_path . basename( $_FILES['file']['name']);
if (move_uploaded_file($_FILES['file']['tmp_name'], $target_path)) {
echo "Upload and move success";
} else {
echo $target_path;
echo "There was an error uploading the file, please try again!";
}
?>
代码
错误是在php端还是离子?
php代码
import { Component } from '@angular/core';
import { NavController,NavParams } from 'ionic-angular';
import { Camera, CameraOptions } from '@ionic-native/camera';
import { FileTransfer, FileUploadOptions, FileTransferObject } from '@ionic-native/file-transfer';
//trying to upload file from below
import { File } from '@ionic-native/file';
import { FileChooser } from '@ionic-native/file-chooser';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
//import { UserProfileService } from '../../services/login.service';
//import { ConfirmidentityPage } from '../confirmidentity/confirmidentity';
//import { ProgressDialog } from '../../utility/progress-dialog';
@Component({
selector: 'page-home',
templateUrl: 'home.html'
})
export class HomePage {
constructor(public navCtrl: NavController, private builder: FormBuilder, private transfer: FileTransfer,private camera: Camera, private file: File,private fileChooser: FileChooser) {
}
upload()
{
let options = {
quality: 100
};
this.camera.getPicture(options).then((imageData) => {
// imageData is either a base64 encoded string or a file URI
// If it's base64:
const fileTransfer: FileTransferObject = this.transfer.create();
let options1: FileUploadOptions = {
fileKey: 'file',
fileName: 'name.jpg',
headers: {}
}
fileTransfer.upload(imageData, 'http://sco7.com/del/uploadtest/upload-device.php', options1)
.then((data) => {
// success
alert("success");
}, (err) => {
// error
alert("error"+JSON.stringify(err));
});
});
}
openGallery() {
var options = {
sourceType: this.camera.PictureSourceType.PHOTOLIBRARY,
destinationType: this.camera.DestinationType.FILE_URI
};
this.camera.getPicture(options).then((imageData) => {
// imageData is either a base64 encoded string or a file URI
// If it's base64:
const fileTransfer: FileTransferObject = this.transfer.create();
let options1: FileUploadOptions = {
fileKey: 'image_upload_file',
fileName: 'name.jpg',
headers: {},
params: {"app_key":"Testappkey"},
chunkedMode : false
}
fileTransfer.upload(imageData, 'http://sco7.com/del/uploadtest/upload-device.php', options1)
.then((data) => {
// success
alert("success"+JSON.stringify(data));
}, (err) => {
// error
alert("error"+JSON.stringify(err));
});
});
}
Home.ts代码
upload()
从
home.html button
调用时,函数openGallery()
有效,单击的图像上传到服务器,但函数success{"bytesSent":5433,"response Code":200,"response":"uploads/ There was an error uploading the file, please try again!","objectId":""}
不起作用。
当我选择图片并上传时,它会说 -
[row.cell.val][row.cell.bg;att=w:shd#w:fill]
请帮忙。 提前感谢您的时间。
答案 0 :(得分:0)
根据错误判断,您将无法正确定位$ target_path-基本名称($ _FILES ['file'] ['name'])中缺少文件名。最有可能的是,它只是没有达到PHP。 我会改变:
let options = {
quality: 100,
destinationType: this.camera.DestinationType.DATA_URL,
encodingType: this.camera.EncodingType.JPEG,
mediaType: this.camera.MediaType.PICTURE
};
let options1: FileUploadOptions = {
fileKey: 'file',
fileName: 'name.jpg',
headers: {},
chunkedMode: false,
httpMethod: 'post',
mimeType: "image/jpeg"
}
fileTransfer.upload('data:image/jpeg;base64,' + imageData, 'http://sco7.com/del/uploadtest/upload-device.php', options1)