2

There are a lot of questions about file upload with Android but most of them without answer and actually none of them are related with javascript or php. I'm seeing strange behaviour when selecting a file to upload on Android (4.4.4) native browser (HTC One_M8) and what it gives me is this;

C:\fakepath\image:12045

"Fakepath" part doesnt bother me, what bothers me is that I cant get file name out of /input type="file"/ html tag. I'm sending files with $.ajax and it works on Chrome, FF, Safari (desktop & iPhone), It works also on my M8 with Chrome, but not with native browser.

This is what I use to get selected files;

var filedata = document.getElementById("userFile");
formdata = false;
if (window.FormData) {
    formdata = new FormData();
}
var i = 0, len = filedata.files.length, img, reader, file;

for (; i < len; i++) {
    file = filedata.files[i];

    if (window.FileReader) {
        reader = new FileReader();
        reader.onloadend = function(e) {
           // showUploadedItem(e.target.result, file.fileName);
        };
        reader.readAsDataURL(file);
    }
    if (formdata) {
        formdata.append("userFile[]", file);
    }
}

And this is how I send them to handle.php

$.ajax({
        url: 'handle.php',
        type: 'POST',
        xhr: function() {
            var myXhr = $.ajaxSettings.xhr();
            if(myXhr.upload){
            }
            return myXhr;
        },
        data: formdata,
        dataType:'json',
        cache: false,
        contentType: false,
        processData: false,
        beforeSend: function(xhr, opt) {
            $('#control-console').append($('input[type=file]').val());
            $('input[type=file]').val("");
        },
        success: function() {
        },
        complete: function(podatki) {
            $('#control-console').append(podatki.responseJSON.name);
            console.log(podatki)
            $.each(podatki.responseJSON.name, function(i, val) {
                console.log(val);
                insertFrame(val);
            });
            processing = false;

        }
    });

I haven't found any documentation about this so I don't really know if this is a bug in Android native browser or do I have to use different approach.

Has anyone faced the same problem and maybe found a solution ?

Blue
  • 261
  • 1
  • 3
  • 14
  • Did you get any solution for this? I am getting the same issue in android for pdf file. The file name is a fake file name and the file type is empty. – Setu Kumar Basak Apr 08 '18 at 14:52
  • Actually I did, if you use jQuery file upload script (github), you will not face this problem. The above code I never really got to work for all phones and tablets so best be just leave it and use jQ script. – Blue Apr 13 '18 at 05:12

2 Answers2

0

I have researched a lot and i have made a solution for android only. When you are in browser and iPhone, you can use your solution as it is working perfectly. But, android have a security issue. So, i had to make a solution for android.

You can follow my solution here.

Setu Kumar Basak
  • 11,460
  • 9
  • 53
  • 85
-3

What I have researched in last few hours is when file being uploaded is within the gallery for the android phone, due to security checks above problem happens. No file name, extension, size will be readable.

To workaround this: 1- Click on browse button, you will be taken to gallery of your device. 2- On right corner of gallery click Options image ("...") and Enable "Internal Storage". 3- Make sure the file you upload is in Internal Storage. 4- Access "Internal Storage" option while uploading. 5- File should show the correct name.

Kamran
  • 591
  • 1
  • 5
  • 9