在iframe中提交android webview

时间:2013-05-24 09:28:08

标签: iframe webview

我有一个webview显示一些网页,该网页包含一个iframe,让用户选择图片并上传。我的问题是当我点击按钮并选择照片时,在onActivityResult方法之后,它刷新整个主页而不是iframe页面。如何仅刷新iframe页面?

主页

<iframe scrolling="no" src="/service/ccrp.do?method=img"></iframe>
<img id="uploadImg" name="uploadImg" src="" style="display: none;"/>
<input type="hidden" id="imgUrl" name="imgUrl" />

iframe页面(/service/ccrp.do?method=img):

function sub() {
        var path = $("#file").val();
        var fs = path.split("\\");
        var fileName = fs[fs.length - 1];
        $("#file_form")[0].submit();
    }
<form id="file_form" onsubmit="return false;"
            ENCTYPE="multipart/form-data" method="post"
            action="/module/bbs/mobile.do?method=upload">
<input id="file" name="file" type="file" onchange="sub()">
</form>

WebviewActivity

@Override
protected void onStart() {
    // TODO Auto-generated method stub
    super.onStart();
    initWebview();
}

private void initWebview(){
    WebSettings settings = webview1.getSettings();
    settings.setAllowFileAccess(true);
    settings.setJavaScriptEnabled(true);
    webview1.setWebViewClient(new WebViewClient() {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
    }
    webview1.setWebChromeClient(new WebChromeClient() {
        // For Android 4+
        public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture){
             openFileChooser( uploadMsg );
        }
        // For Android 3.0+
        public void openFileChooser( ValueCallback<Uri> uploadMsg, String acceptType ) 
        {  
         openFileChooser( uploadMsg );
        }

        // For Android < 3.0
        public void openFileChooser( ValueCallback<Uri> uploadMsg ) 
        {
            WebviewActivity.this.mUploadMessage = uploadMsg;  
            Intent i = new Intent(Intent.ACTION_GET_CONTENT);  
            i.addCategory(Intent.CATEGORY_OPENABLE);  
            i.setType("image/*");  
            WebviewActivity.this.startActivityForResult( Intent.createChooser( i, "File Chooser" ), FILECHOOSER_RESULTCODE );  
        }
    });
    webview1.loadUrl(url);
}

@Override
protected void onActivityResult(int requestCode, int resultCode,
        Intent intent) {

    switch (requestCode) {

    // Choose a file from the file picker.
    case FILECHOOSER_RESULTCODE:
        if (null == mUploadMessage)
            break;
        Uri result = intent == null || resultCode != RESULT_OK ? null
                : intent.getData();
        Log.v("CEEG_RESULT", "onActivityResult:" + result.getQuery());
        mUploadMessage.onReceiveValue(result);
        mUploadMessage = null;
        break;
    }
}

1 个答案:

答案 0 :(得分:0)

解决。我把initWebview方法错误地放在onStart而不是onCreate上。所以在点击html链接后,onStart再次被触发,然后重新加载主页网址,而不是提交iframe表单。