【发布时间】:2018-05-09 13:14:45
【问题描述】:
我正在 ionic 3 中开发一个应用程序,需要使用在 lumen 框架中创建的 api 将图像上传到服务器。 上传图片的请求是: 图片从相机中点击并转换为base64。
let base64Image = 'data:image/jpeg;base64,' + imageData;
然后我使用 FileUpload 上传图片
import { FileTransfer, FileUploadOptions, FileTransferObject } from '@ionic-native/file-transfer';
buildHeadersDeals(){
this.header = new Headers();
this.header.append('Authorization', 'Basic '
+btoa("test:test"));
}
uploadPhoto(image, token) {
this.buildHeadersDeals();
url = 'http://192.168.2.12/api/upload?token="+token;
const fileTransfer: FileTransferObject = this.transfer.create();
let options: FileUploadOptions = {
fileKey: 'photo',
fileName: image.substr(image.lastIndexOf('/')+1),
chunkedMode: true,
mimeType: "image/jpeg",
headers: this.header,
}
return fileTransfer.upload(image, encodeURI(url), options)
.then((data) => {
console.log(data);
return data;
}, (err) => {
console.log(err);
});
}
我的 api 端是:
public function upload(Request $request) {
if ($request->hasFile('photo')) {
$image = $request->file('photo');
$response['image'] = $image;
return response()->json($response,200);
}
}
我有两个问题:
1) 我总是将照片设为 null ($request->file('photo'))
2) 有人可以告诉我将令牌作为参数发送吗,下面的代码不起作用:
let options: FileUploadOptions = {
fileKey: 'photo',
fileName: image.substr(image.lastIndexOf('/')+1),
chunkedMode: true,
mimeType: "image/jpeg",
headers: this.header,
params: {
'token': 'sffsdhnzchvh'
}
}
谢谢
【问题讨论】:
标签: laravel ionic-framework ionic3 lumen