Plupload not working in webview (Android/iOS mobile)
Hi, whoever comes across this I hope this helps you. Recently I had a problem with plupload where it was not working in a webview. A webview opens when you click on a link in an app and they open the link not in a browser like chrome/safari but in the app.
Versions used: Plupload 2.3.6/Moxie 1.5.7 + DatingScript
The problem was because webview doesn't work with <input type="file" /> when the accept parameter has extensions like "gif,png,jpg". It either needs to be excluded, or have the correct mime types like image/*.
Here is what my plupload javascript with the issue (truncated from php for brevity):
var uploader = new plupload.Uploader({
filters : {
mime_types: [
{title : "Files", extensions : "gif,png,jpg"}
]
}
}
Here is how I fixed the issue, I removed the mime_types filter (This makes the file input exclude the accept parameter) and added my own _extensions filter to validate the file type.
var uploader = new plupload.Uploader({
filters : {
_extensions: "gif,png,jpg"
}
}
plupload.addFileFilter('_extensions', function(maxSize, file, cb) {
var fileType = file.type;
//Make sure file type is valid
if (!fileType || !fileType.includes('/')) {
cb(false);
return;
}
//Get extension of file
var type = fileType.split('/')[1];
//Get all valid extensions as array
var exts = 'gif,png,jpg'.split(',');
//If valid extensions don't include this file type, throw an error
if (!exts.includes(type)) {
this.trigger('Error', {
code: 'EXT_ERROR',
message: plupload.translate('Not a valid file type: ') + 'gif,png,jpg',
file: file
});
cb(false);
} else {
cb(true);
}
});
I'm not sure how to go about this a better way since the default mime_types filter in plupload doesn't take "image/*". If the maintainer sees this, can you please let me know if this is an acceptable solution, and if there is a patch that can be made for this?