【发布时间】:2020-01-27 14:19:32
【问题描述】:
我有一个数据库和 webview。我希望 webview 能够在本地文件夹中显示文本和图像。
数据库是从下面的JSON获取的:
"list_soal": [
{
"qid": "33840",
"question": "<p><img src=\"https://tryout.pendidikan.id/upload/16.JPG\" />Lafal yang melengkapi ayat tersebut adalah ….</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"
]
}
]
},
对于img scr,我改成本地文件夹中的图片路径。
代码:
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());
我遇到了一个问题,即本地文件夹中的图像无法在 webview 上显示,如下图所示:
如何处理?
注意:
图片已成功下载并保存在本地文件夹(C:\Users\\AppData\Local\Packages\\LocalState\library\gambar\503)
questionText 是 webview 的名称
【问题讨论】:
标签: c# json database webview uwp