Android Code Snippet Upload files with WebView

Discussion in 'Code Snippets' started by Erel, Oct 25, 2018.

  1. Erel

    Erel Administrator Staff Member Licensed User

    Code requires Android 5+

    1. Set a custom WebViewChromeClient. It is implemented with inline Java code.

    2. The ShowFile_Chooser event is raised when the user clicks on a "browse" button.

    3. You need to get the file URI with FileProvider and call SendResult.
    Example based on ContentChooser.
    Code:
    Sub ShowFile_Chooser (FilePathCallback As Object, FileChooserParams As Object)
       cc.Initialize(
    "CC")
       cc.Show(
    "*/*""Choose File")
       
    Wait For CC_Result (Success As Boolean, Dir As String, FileName As String)
       
    Dim jo As JavaObject = Me
       
    If Success Then
           
    Log(FileName)
           
    File.Copy(Dir, FileName, Starter.Provider.SharedFolder, "TempFile")
           jo.RunMethod(
    "SendResult"Array(Starter.Provider.GetFileUri("TempFile"), FilePathCallback))
       
    Else
           jo.RunMethod(
    "SendResult"Array(Null, FilePathCallback))
       
    End If
    End Sub
    Project is attached. Don't miss the manifest editor entries that are required for FileProvider.
     

    Attached Files:

    ronell, Beja, ocalle and 10 others like this.
  2. Sasuke Sama

    Sasuke Sama Active Member Licensed User

    Thanx works great but
    when i use this code the WebChromeClient1_ProgressChanged sub will not work anymore
     
  3. Erel

    Erel Administrator Staff Member Licensed User

    It will not work with WebViewExtras as it replaces the WebChromeClient.
     
  4. Sasuke Sama

    Sasuke Sama Active Member Licensed User

    Yes i figured that out is there any other way i can use both? i only need the progress bar from webviewextras so i wonder if thers other ways to use it
     
  5. Erel

    Erel Administrator Staff Member Licensed User

    You cannot use both. You can see how I've implemented the custom WebChromeClient and add more features to it.
     
  6. Sasuke Sama

    Sasuke Sama Active Member Licensed User

    OK thanx its googling time : D
     
  7. theunis

    theunis Member Licensed User

    hi there, when I run the example the https://b4x.com:51041/websocket_with_fileupload/index.html doesnt display - any other url is fine and displays - I just need to know if it is a setting I need to change before I try an adapt this to see if it will work for my project.

    many thanks
    T
     
  8. Erel

    Erel Administrator Staff Member Licensed User

    Your device probably doesn't recognize the certificate.
     
  9. rosippc64a

    rosippc64a Active Member Licensed User

    Hi Erel,
    maybe file download is also possible?
     
  10. DonManfred

    DonManfred Expert Licensed User

    you should use the overrideurl event and download the file using okhttputils2.
    Search the forum. There are examples.
     
  11. rosippc64a

    rosippc64a Active Member Licensed User

    Hi DonManfred,
    that was only a rhetorical question :) I know and can use the okhttputils2, but my download link goes to a download php what needs an authentication what can made only a browser (webview e.g.) and the login data is stored in php session variables what unknown by okhttputils2.
     
  12. JohnC

    JohnC Well-Known Member Licensed User

    I tired using this example (but to a custom URL), and I get a "The page at "[custom url]... File Type is not allowed" error when I click on an image to upload.

    But if I navigate to this same exact page using my regular chrome browser and try to upload the same exact file, it will work fine.

    Any ideas?
     
  13. Erel

    Erel Administrator Staff Member Licensed User

    Does it work with the url from the example?
     
  14. JohnC

    JohnC Well-Known Member Licensed User

    Yes, it does work, but I do not have access to the webpage's source to see what it is doing.

    For example, the webpage I am accessing doesn't have a "submit" button - it will upload the image as soon as it is selected.

    But were is this "file type is not allowed" message being generated from? Is it from the webpage at the selected URL or is it being generated locally from the app?
     
  15. Erel

    Erel Administrator Staff Member Licensed User

    From the web page.
     
  16. JohnC

    JohnC Well-Known Member Licensed User

    But then why does the same webpage accept the same exact file from the chrome browser on my phone, but rejects that same exact file using this webview project?
     
  17. Erel

    Erel Administrator Staff Member Licensed User

    Impossible to say without learning the exact parameters that are required by this page.
     
  18. tuhatinhvn

    tuhatinhvn Active Member Licensed User

    can we upload multi files if webview support it?
    i saw current is only single file ??
    Code:
    File.Copy(Dir, FileName, Starter.Provider.SharedFolder, "TempFile")
           jo.RunMethod(
    "SendResult"Array(Starter.Provider.GetFileUri("TempFile"), FilePathCallback))
     
  19. Erel

    Erel Administrator Staff Member Licensed User

    The example can only handle a single file.
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice