gz0023
August 2, 2017, 7:09pm
1
I’ve searched but I can’t find a definitive answer… Is there a FileChooser like plugin for iOS that opens a file browser so users can upload a document from the phones storage? Preferably one that works for both iOS and Android
<input type="file"> only shows iCloud and Photos
jblake
August 2, 2017, 8:31pm
2
I use the native camera plugin to either take a photo or select one from the device. You could limit it so that they can’t take a picture pretty easily by just deleting the part in the code below that allows them to take a picture. You would need to import both of the native plugins below too:
Action sheet: https://ionicframework.com/docs/native/action-sheet/
Camera: https://ionicframework.com/docs/native/camera/
/** when cordova, present an action sheet to take a pic or import from gallery */
presentActionSheet(editable: boolean) {
let buttons: any = [];
if (this.platform.is('cordova')) {
buttons.push({
text: 'Choose Photo',
handler: () => {
this.getPicture(this.camera.PictureSourceType.PHOTOLIBRARY, editable); // 0 == Library
}
}, {
text: 'Take Photo',
handler: () => {
this.getPicture(this.camera.PictureSourceType.CAMERA, editable); // 1 == Camera
}
})
}
buttons.push(
{
text: 'Cancel',
role: 'cancel'
}
);
let actionSheet = this.actionSheetCtrl.create({
buttons: buttons
});
actionSheet.present();
}
/**
* get picture from gallery or camera
* @Param sourceType:number camera or gallery
*/
getPicture(sourceType: number, editable: boolean) {
this.utils.presentLoading();
this.camera.getPicture({
quality: 40,
destinationType: 1,
sourceType: sourceType,
targetWidth: 1000,
targetHeight: 1333,
allowEdit: editable,
mediaType: 0,
saveToPhotoAlbum: false,
correctOrientation: true //this needs to be true to get a file:/// FILE_URI, otherwise android does not return a file uri. Yep.
}).then((imageData) => {
console.log(`IMAGEDATA: ${Utils.toJson(imageData, true)}`);
//fix for android, remove query string from end of file_uri or crashes android //
imageData = imageData.split('?')[0];
let filename = imageData.replace(/^.*[\\\/]/, '');
let fileData = {
name: filename,
caption: '',
//notes : '',
path: imageData,
//file : file
}
this.data.files.push(fileData);
this.utils.dismissLoading();
}, (err) => {
console.log(`ERROR -> ${JSON.stringify(err)}`);
this.utils.dismissLoading();
});
}
This is what it looks like when I hit “Add Image”
I believe iOS limits to only certain directories no matter the method attempted.
gz0023
August 2, 2017, 8:42pm
4
@jblake I need to upload documents not photos
@rapropos That sucks :(. Do you know if thats the case for native apps as well?
I wouldn’t think there would be a difference between native and cordova apps on this front; I think they’re both limited to what’s provided here .
o8thug
March 12, 2020, 2:23pm
6
you can try this plugin for selecting documents:Document picker