将base64图像从图库中选择发送到Ionic3中的服务器
Send base64 image chooses from gallery to server in Ionic3
我正在使用相机和图像选择器插件来添加使用相机和画廊添加照片的功能。但是当我从图库中选择图像并尝试发送到服务器时,作为 base64 图像我无法在服务器端获取它。它发送一个空字符串。
add.dog.ts.
//Open image picker multiple images
openImagePicker(){
let options = {
maximumImagesCount: 3,
}
this.photos = new Array<string>();
this.imagePicker.getPictures(options)
.then((results) => {
this.reduceImages(results).then(() => {
for (let index = 0; index < results.length; index++) {
this.photos.push(results[index]);
this.base64.encodeFile(results[index]).then((base64File: string) => {
this.pic = base64File;
this.authService.uploadPhotosServer(this.pic).then((result) => {
this.responseData = result;
}, (err) => {
console.log(err);
});
}
//console.log("Image Lists", this.photos);
});
}, (err) => { console.log(err) });
}
所以我在这里发送 this.pic = base64File
到我发送 post 请求到服务器的服务。
uploadPhotosServer(photos){ //console.log(photos);
return new Promise((resolve, reject) => {
let headers = new Headers();
let datafile = photos;
this.http.post(uploadDogPhotosGalleryAuthEndPoint,datafile, {headers: headers})
.subscribe(res => {
resolve(res.json());
}, (err) => {
resolve(err.json());
});
});
}
服务器端我正在尝试访问如下数据:
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
echo "<pre>";print_r($_REQUEST);die;
}
但它总是显示为空。如果我检查控制台,它会显示请求负载,如:
data:image/*;charset=utf-8;base64,/9j/4AAQSkZJRg
可能是什么问题?
谢谢
试试这个:
$postData = file_get_contents('php://input');
var_dump($postData )
我正在使用相机和图像选择器插件来添加使用相机和画廊添加照片的功能。但是当我从图库中选择图像并尝试发送到服务器时,作为 base64 图像我无法在服务器端获取它。它发送一个空字符串。
add.dog.ts.
//Open image picker multiple images
openImagePicker(){
let options = {
maximumImagesCount: 3,
}
this.photos = new Array<string>();
this.imagePicker.getPictures(options)
.then((results) => {
this.reduceImages(results).then(() => {
for (let index = 0; index < results.length; index++) {
this.photos.push(results[index]);
this.base64.encodeFile(results[index]).then((base64File: string) => {
this.pic = base64File;
this.authService.uploadPhotosServer(this.pic).then((result) => {
this.responseData = result;
}, (err) => {
console.log(err);
});
}
//console.log("Image Lists", this.photos);
});
}, (err) => { console.log(err) });
}
所以我在这里发送 this.pic = base64File
到我发送 post 请求到服务器的服务。
uploadPhotosServer(photos){ //console.log(photos);
return new Promise((resolve, reject) => {
let headers = new Headers();
let datafile = photos;
this.http.post(uploadDogPhotosGalleryAuthEndPoint,datafile, {headers: headers})
.subscribe(res => {
resolve(res.json());
}, (err) => {
resolve(err.json());
});
});
}
服务器端我正在尝试访问如下数据:
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
echo "<pre>";print_r($_REQUEST);die;
}
但它总是显示为空。如果我检查控制台,它会显示请求负载,如:
data:image/*;charset=utf-8;base64,/9j/4AAQSkZJRg
可能是什么问题?
谢谢
试试这个:
$postData = file_get_contents('php://input');
var_dump($postData )