0

I am trying to do a file upload to a link (share point rest service) which works well in chrome browser, but not in android webview.

I know android webview comes with a lot of restrictions, but I am trying to upload a file using the following approach

https://github.com/mgks/Os-FileUp

it opens up the browser on but it does not show the file name nor it upload any content or call the service, but the same code works just fine in chrome browser (pls note that I have enabled CORS to test it

my html and js code below

i have successfully built the android project and put my html file and run it as an apk. other ajax codes with get and post are working just fine. only file upload is a problem

please help me with this file upload thing, with some workable example. Also in the sample

<!DOCTYPE html>
<html>
<body>

    <h1>Sharepoint File upload</h1>

    <form method="POST" enctype="multipart/form-data" id="fileUploadForm">
        <!--<input type="text" name="extraField" />--><br /><br />       
        <input type="file" id="fileupload" name="files" /><br /><br />
        <input type="submit" value="Submit" id="btnSubmit" />
    </form>

    <h1></h1>
    <span id="result"></span>

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
    <script>
        $(document).ready(function () {

            $("#btnSubmit").click(function (event) {

                //stop submit the form, we will post it manually.
                event.preventDefault();

                // Get form
              //  var form = $('#fileUploadForm')[0];

                // Create an FormData object
                var data = new FormData();
                data.append("File", $('#fileupload')[0].files[0]);
                // If you want to add an extra field for the FormData
                // data.append("File", form);
                console.log('file', $('#fileupload')[0].files[0]);
                // disabled the submit button
                $("#btnSubmit").prop("disabled", true);

                var _url = "https://my.sharepoint.com/teams/<MyProj>/_api/web/getfolderbyserverrelativeurl(\'/teams/<MyProj>/Project Documents/1264\')/files/add(overwrite=true,url=\'TestUpload1.txt\')"
                $.ajax({
                    type: "POST",
                   // enctype: 'multipart/form-data',
                    headers: {     
                        'Accept': 'application/json;odata=verbose',
                        'processData': false,
                        'Authorization': 'Bearer <key-value>',
                        'Content-Type': 'application/json'
                    },
                    url: _url,
                    data: data,
                    processData: false,
                    contentType: true,
                    cache: false,
                    timeout: 600000,                    
                    success: function (data) {

                        $("#result").text(data);
                        console.log("SUCCESS : ", data);
                        $("#btnSubmit").prop("disabled", false);

                    },
                    error: function (e) {

                        $("#result").text(e.responseText);
                        console.log("ERROR : ", e);
                        $("#btnSubmit").prop("disabled", false);

                    }
                });

            });

        });
    </script>
</body>
</html> 

android code the filetype is restricted to image/* but i need to upload pdf, doc and txt files

  • Check this answer https://stackoverflow.com/a/50463988/4377954 – Deˣ Jul 20 '19 at 19:56
  • Possible duplicate of [File Upload is not working in Android WebView](https://stackoverflow.com/questions/50463911/file-upload-is-not-working-in-android-webview) – Deˣ Jul 20 '19 at 19:57
  • the first link does not have accepted answer, second link has an accepted answer, but it refers to the first link ! I have played a lot, still could not make it work. It works like a charm in a chrome browser in laptop ! request users to provide any troubleshooting ideas, which will make it work ! I still keep trying – user1619015 Jul 21 '19 at 04:22

1 Answers1

0

Change image/* to */* for all file types or you can use application/pdf for specifically for PDF files, on this line.

Regarding file upload, everything seems fine and it should work. Are you using the latest Os-FileUp update? The repo has just been recently updated with many bug fixes. OR try Smart WebView, if it works for you.

Ghazi
  • 176
  • 1
  • 11
  • Thanks, this helped my app to choose pdf or text files. I was also able to get success response for fileupload and the file was shown in the server. But the files were corrupted. The text file content came as undefined ! and pdf file did not open. Above html code shows the filename by default behaviour (just bext to choose file button) but in webview it still shows "no file chosen" , secondly, when I read through codes in mainActivity in detail, I do not see any code to handle other than image or video, is it that this code is only written for image or video, and cant be used for pdf or txt ? – user1619015 Jul 22 '19 at 07:35
  • @user1619015 all files are handled by `onShowFileChooser()` by default, I believe customs handlers you found for images and videos are methods for file creation if user chooses camera/camcoder as a method of input. You can try uploading files to your local server first to make sure this sole issue is on applications end. – Ghazi Jul 23 '19 at 03:47
  • using SmartWebView resolved the problem, hence accepting as an answer. But it takes 4-5 minutes to upload a 600 kb pdf file ! need to see how it can be optimized ! – user1619015 Jul 24 '19 at 15:11
  • I'm not so sure why that must be happening, I'll look into it and update if I found something out. – Ghazi Jul 24 '19 at 17:05