我正在开发一个应用程序的离子3,需要上传图像到服务器使用一个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(‘照片’))
( 2)有人能告诉我将令牌作为params发送吗?下面的代码不起作用:
let options: FileUploadOptions = {
fileKey: 'photo',
fileName: image.substr(image.lastIndexOf('/')+1),
chunkedMode: true,
mimeType: "image/jpeg",
headers: this.header,
params: {
'token': 'sffsdhnzchvh'
}
}谢谢
发布于 2018-02-09 10:57:50
我安装了“cordova-plugin-照相机”:"^4.0.2“和"cordova-plugin-file":"^6.0.1”
函数调用此处this.selectImage(this.camera.PictureSourceType.CAMERA);->
selectImage(selection: any) {
var options: any;
options = {
quality: 75,
destinationType: this.camera.DestinationType.DATA_URL,
sourceType: selection,
allowEdit: true,
encodingType: this.camera.EncodingType.JPEG,
saveToPhotoAlbum: false
};
this.camera.getPicture(options).then((imgUrl) => {
if (options.destinationType == this.camera.DestinationType.FILE_URI) {
console.log(imgUrl,'if');
var sourceDirectory = imgUrl.substring(0, imgUrl.lastIndexOf('/') + 1);
var sourceFileName = imgUrl.substring(imgUrl.lastIndexOf('/') + 1, imgUrl.length);
sourceFileName = sourceFileName.split('?').shift();
this.File.copyFile(sourceDirectory, sourceFileName, cordova.file.externalApplicationStorageDirectory, sourceFileName).then((result: any) => {
this.imageNewPath = result.nativeURL;
// do api call here
}, (err) => {
console.log(JSON.stringify(err));
})
}
else {
console.log(imgUrl,'else');
this.imageNewPath = "data:image/jpeg;base64," + imgUrl;
//do here
}
}, (err) => {
console.log("Error in choosing image :" + JSON.stringify(err));
});
}https://stackoverflow.com/questions/47491623
复制相似问题