簡體   English   中英

xamarin android webview 文件上傳下載不起作用。 請我對 c# 感到滿意

[英]xamarin android webview file upload download does not work . please i am comfortable with c#

我為我的網站創建了一個移動應用程序。webview 不處理上傳和下載。所以我按照鏈接使用 Xamarin Android 應用程序中的 webview 上傳文件並包含此鏈接中的代碼,但不處理上傳和下載。 請指導。 storeactivity.cs 代碼在這里。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Android.App;
using Android.Content;
using Android.Net;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Webkit;
using Android.Widget;

namespace smartapp
{
    [Activity(Label = "StoreActivity")]
    public class StoreActivity : Activity
    {
        static ProgressBar progressBar;
        WebView webview;


        IValueCallback mUploadMessage;
        private static int FILECHOOSER_RESULTCODE = 1;
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            SetContentView(Resource.Layout.StoresView);

            webview = FindViewById<WebView>(Resource.Id.StoreView);
            // show progress bar
            progressBar = FindViewById<ProgressBar>(Resource.Id.progressBar);
            // chrome client to upload files from webview
            var chrome = new FileChooserWebChromeClient((uploadMsg, acceptType, capture) => {
                mUploadMessage = uploadMsg;
                var i = new Intent(Intent.ActionGetContent);
                i.AddCategory(Intent.CategoryOpenable);
                i.SetType("image/*");
                StartActivityForResult(Intent.CreateChooser(i, "File Chooser"), FILECHOOSER_RESULTCODE);
            });

            webview.LoadUrl("https://smartbook.pk/Stores/index");

            webview.SetWebViewClient(new WebViewListener());
            // download files from webview
            webview.SetDownloadListener(new MyDownloadListerner(this));
            webview.SetWebChromeClient(chrome);
                    webview.Settings.JavaScriptEnabled = true;


        }
        //
        class MyDownloadListerner : Java.Lang.Object, IDownloadListener
        {
            Context cont;
            public MyDownloadListerner(Context context)
            {
                cont = context;
            }
            public void OnDownloadStart(string url, string userAgent, string contentDisposition, string mimetype, long contentLength)
            {
                Android.Net.Uri uri = Android.Net.Uri.Parse(url);
                Intent intent = new Intent(Intent.ActionView, uri);
                cont.StartActivity(intent);
            }
        }
        //

        //
        protected override void OnActivityResult(int requestCode, Result resultCode, Intent intent)
        {
            if (requestCode == FILECHOOSER_RESULTCODE)
            {
                if (null == mUploadMessage)
                    return;
                Java.Lang.Object result = intent == null || resultCode != Result.Ok
                    ? null
                    : intent.Data;
                mUploadMessage.OnReceiveValue(result);
                mUploadMessage = null;
            }
        }
        // file chooser code 
        partial class FileChooserWebChromeClient : WebChromeClient
        {
            Action<IValueCallback, Java.Lang.String, Java.Lang.String> callback;

            public FileChooserWebChromeClient(Action<IValueCallback, Java.Lang.String, Java.Lang.String> callback)
            {
                this.callback = callback;
            }

            //For Android 4.1
            [Java.Interop.Export]
            public void openFileChooser(IValueCallback uploadMsg, Java.Lang.String acceptType, Java.Lang.String capture)
            {
                callback(uploadMsg, acceptType, capture);
            }
        }

        // webview listener code here
        public class WebViewListener : WebViewClient
        {
            public override bool ShouldOverrideUrlLoading(WebView view, IWebResourceRequest request)
            {
                view.LoadUrl(request.Url.ToString());
                return true;
            }
            public override void OnPageStarted(WebView view, string url, Android.Graphics.Bitmap favicon)
            {

                progressBar.Progress = view.Progress;
            }
            public override void OnLoadResource(WebView view, string url)
            {

                progressBar.Progress = view.Progress;
            }
            public override void OnPageFinished(WebView view, string url)
            {

                progressBar.Progress = 0;
            }
        }
        public override bool OnKeyDown(Android.Views.Keycode keyCode, Android.Views.KeyEvent e)
        {
            if (keyCode == Keycode.Back && webview.CanGoBack())
            {
                webview.GoBack();
                return true;
            }
            return base.OnKeyDown(keyCode, e);
        }


    }

}

這是文件上傳的工作代碼希望這對你有幫助

public class MyWb : Activity
{
int count = 1;

IValueCallback mUploadMessage;
private static int FILECHOOSER_RESULTCODE = 1;

protected override void OnCreate (Bundle bundle)
{
    base.OnCreate (bundle);
    // Set our view from the "main" layout resource
    SetContentView (Resource.Layout.Main);
    // Get our button from the layout resource,
    // and attach an event to it
    Button button = FindViewById<Button> (Resource.Id.myButton);
    button.Click += delegate {
        button.Text = string.Format ("{0} clicks!", count++);
    };

    var chrome = new FileChooserWebChromeClient ((uploadMsg, acceptType, capture) => {
        mUploadMessage = uploadMsg;
        var i = new Intent (Intent.ActionGetContent);
        i.AddCategory (Intent.CategoryOpenable);
        i.SetType ("image/*");
        StartActivityForResult (Intent.CreateChooser (i, "File Chooser"), FILECHOOSER_RESULTCODE);  
    });

    var webview = this.FindViewById<WebView> (Resource.Id.webView1);
    webview.SetWebViewClient (new WebViewClient ());
    webview.SetWebChromeClient (chrome);
    webview.Settings.JavaScriptEnabled = true;
    webview.LoadUrl ("http://www.script-tutorials.com/demos/199/index.html");
}

protected override void OnActivityResult (int requestCode, Result resultCode, Intent intent)
{
    if (requestCode == FILECHOOSER_RESULTCODE) {
        if (null == mUploadMessage)
            return;
        Java.Lang.Object result = intent == null || resultCode != Result.Ok
            ? null
            : intent.Data;
        mUploadMessage.OnReceiveValue (result);
        mUploadMessage = null;
    }
 }
 }

partial class FileChooserWebChromeClient : WebChromeClient
{
Action<IValueCallback, Java.Lang.String, Java.Lang.String> callback;

public FileChooserWebChromeClient (Action<IValueCallback, Java.Lang.String, Java.Lang.String> callback)
{
    this.callback = callback;
}

//For Android 4.1
[Java.Interop.Export]
public void openFileChooser (IValueCallback uploadMsg, Java.Lang.String acceptType, Java.Lang.String capture)
{
    callback (uploadMsg, acceptType, capture);
}
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM