简体   繁体   English

在 webview 上显示本地文件夹中的文本和图像

[英]Display Text and Image from local folder on webview

I have a database and webview.我有一个数据库和 webview。 I want webview to be able to display text and images in the local folder.我希望 webview 能够在本地文件夹中显示文本和图像。

Database:数据库: 数据库

The database is obtained from JSON below:该数据库是从下面的JSON中获得的:

"list_soal": [
            {
                "qid": "33840",
                "question": "<p><img src=\"https://tryout.pendidikan.id/upload/16.JPG\" />Lafal&nbsp;yang melengkapi ayat tersebut adalah &hellip;.</p>",
                "has_img": "1",
                "images": [
                    "https://tryout.pendidikan.id/upload/16.JPG"
                ],
                "jawaban": [
                    {
                        "oid": "81912",
                        "q_option": "<p><img src=\"https://tryout.pendidikan.id/upload/1a1.JPG\" /></p>",
                        "score": "0",
                        "has_img": "1",
                        "images": [
                            "https://tryout.pendidikan.id/upload/1a1.JPG"
                        ]
                    },
                    {
                        "oid": "81913",
                        "q_option": "<p><img src=\"https://tryout.pendidikan.id/upload/1b1.JPG\" /></p>",
                        "score": "1",
                        "has_img": "1",
                        "images": [
                            "https://tryout.pendidikan.id/upload/1b1.JPG"
                        ]
                    },
                    {
                        "oid": "81914",
                        "q_option": "<p><img src=\"https://tryout.pendidikan.id/upload/1c1.JPG\" /></p>",
                        "score": "0",
                        "has_img": "1",
                        "images": [
                            "https://tryout.pendidikan.id/upload/1c1.JPG"
                        ]
                    },
                    {
                        "oid": "81915",
                        "q_option": "<p><img src=\"https://tryout.pendidikan.id/upload/1d1.JPG\" /></p>",
                        "score": "0",
                        "has_img": "1",
                        "images": [
                            "https://tryout.pendidikan.id/upload/1d1.JPG"
                        ]
                    }
                ]
            },

For img scr, I change it to the image path in the local folder.对于img scr,我将其更改为本地文件夹中的图像路径。

Code:代码:

StorageFolder installedLocation = ApplicationData.Current.LocalFolder;    
StorageFolder library = await installedLocation.CreateFolderAsync("library", CreationCollisionOption.OpenIfExists);
                                            StorageFolder gambar = await library.CreateFolderAsync("gambar", CreationCollisionOption.OpenIfExists);
                                            StorageFolder imgName = await gambar.CreateFolderAsync(quizID.ToString(), CreationCollisionOption.OpenIfExists);
                                            string imgPath = imgName.Path;
                                            string soal = "";
                                            if(question.Pertanyaan.ToString().Contains("https://tryout.pendidikan.id/upload/"))
                                            {   
                                                soal = Regex.Replace(question.Pertanyaan, "\"https://tryout.pendidikan.id/upload/", "''file:\\" + "\\" + "\\" + imgPath + "\\");
                                                soal = Regex.Replace(soal, ".JPG\"", ".jpg''");
                                            }
                                            string InsertQuestion = @"INSERT INTO DBQuestion (QID,Pertanyaan, QuizID) SELECT '" + question.QID.ToString() + "','" + soal + "','" + quiz.ID + "' WHERE not exists " +
                                            "(select QID and Pertanyaan and QuizID FROM DBQuestion WHERE QID='" + question.QID.ToString() + "' and Pertanyaan='" + soal + "' and QuizID='" + quiz.ID + "')";
                                            var quizQuestion = objConn.Prepare(InsertQuestion);
                                            quizQuestion.Step();
                                        }
    string QuestionPhrase = @"SELECT * FROM DBQuestion WHERE QuizID='" + quizID + "'";
                question = objConn.Prepare(QuestionPhrase);
     question.Step();

                questionText.NavigateToString(question[1].ToString());

I'm having a problem, ie the images in the local folder can't be displayed on the webview, like the image below:我有一个问题,即本地文件夹中的图像无法在webview上显示,如下图: 图片

How to handle it?如何处理?

Note:笔记:

  • The image has been downloaded and saved successfully in the local folder (C:\Users\\AppData\Local\Packages\\LocalState\library\gambar\503)图片已成功下载并保存在本地文件夹(C:\Users\\AppData\Local\Packages\\LocalState\library\gambar\503)

  • questionText is the name of webview questionText是webview的名字

UWP applications are sandboxed applications that are isolated from the external environment and cannot access files directly through the path, even WebView. UWP 应用程序是与外部环境隔离的沙盒应用程序,无法直接通过路径访问文件,即使是 WebView。

You can directly pass the http link of the image to the WebView.可以直接将镜像的http链接传给WebView。 If you want to access it offline, convert the image to a Base64 string and pass the string as the source of the image.如果要离线访问,请将图像转换为 Base64 字符串并将该字符串作为图像源传递。

Here is the convert method:这是转换方法:

public static async Task<string> ImgToBase64(StorageFile file)
{
    var buffer = await FileIO.ReadBufferAsync(file);
    string result = Convert.ToBase64String(GetbytesFromBuffer(buffer));
    result = "data:image/png;base64," + result;
    return result;
}

public static byte[] GetbytesFromBuffer(IBuffer buffer)
{
    DataReader dataReader = DataReader.FromBuffer(buffer);
    byte[] bytes = new byte[buffer.Length];
    dataReader.ReadBytes(bytes);
    return bytes;
}

Best regards.此致。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM