How to upload files from webview with php and javascript

1

I will proceed to explain everything as detailed as possible.

My system is PHP and I used the Materialize framework. There are 2 forms that require uploading files.

Form 1- Requires uploading a photo (JPG, JPEG, PNG, GIF).

Form 2- Requires uploading a file or photo (PDF, JPG, JPEG, PNG, GIF, DOC, XLS, DOCX, PPT, XLSX).

This is my code:

 <div class="row">
<div class="input-field col s12 m12 l12"> <!-- Tamano del input-field Upload-File-->
<div class="file-field input-field">
 <div class="btn blue">
 <input type="hidden" name="MAX_FILE_SIZE" value="2000000" />
 <span>Archivo</span>
 <input type="file" name="imagen" id="imagen">
 </div>
 <div class="file-path-wrapper">
 <input class="file-path validate" type="text" placeholder="PDF, JPG, JPEG, PNG, GIF, DOC, XLS, DOCX, PPT, XLSX">
 </div>
 </div>
 </div>
</div>

Regarding android, I have a webview:

WebView webView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        webView = (WebView) findViewById(R.id.wb);
        webView.setWebChromeClient(new WebChromeClient());
        webView.setWebViewClient(new WebViewClient());
        webView.getSettings().setJavaScriptEnabled(true);
        webView.loadUrl("http://mi.pagina.com");
    }

PROBLEM: When I press to upload the file, nothing happens.

I was reading a lot, and it seems that you have to add a lot of code, but I think it varies in the android version.

For the moment add this the Androidmanifest.xml:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
<uses-permission android:name="android.permission.VIBRATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

And I tried several codes that appear in this forum, but none of them work for me.

Forums: link link

I would appreciate your help.

EDITED:

I tried this code:

link

  • The app starts and everything works perfectly, so I assume that the code works perfectly, but it does not start the FILE, so I think some code is missing but javascript on the server
asked by Strelok 06.04.2017 в 13:56
source

1 answer

1

Actually no action is taken in this case, only if the action comes from a redirect , as a proof, you can use this application that contains a log and you can see the actions that are performed when interact with the content loaded within WebView :

link

What is needed is to create a interface , making the declaration on the page:

<input type="button" value="Say hello" onClick="showAndroidToast('Hello Android!')" />

<script type="text/javascript">
    function showAndroidToast(toast) {
        Android.showToast(toast);
    }
</script>

and the call in WebView , so that the WebView detects the action performed by JavaScript :

WebView webView = (WebView) findViewById(R.id.webview);
webView.addJavascriptInterface(new WebAppInterface(this), "android");

In the documentation you can see information about the Javascript interface

    
answered by 06.04.2017 в 18:40