我有以下问题:我想用 JQuery 读取一个 txt 文件并将其放在一个 div 中,但我还没有找到这样做并解决我的问题。我有这个代码:
<script type="text/javascript">
$(getImages);
function getImages() {
$.getJSON("/home/ListImagesProducts", showImages);
}
function showImages(data) {
$("#directoryImages").append("<ul id = 'productImages'>");
for (i = 0; i < data.length; i++) {
var directories = data[i];
$("#directoryImages ul").append("<li><img src='../Content/images/Products/" + directories.ImageName + "' /><add value='" + directories.DescriptionName + "'</li>");
}
//$("#directoryImages").append("</ ul>");
}
</script>
从 < add ...> 我想包含该文件,但它不起作用。在我的控制器中,包含此函数值的方法是这样的:
public ActionResult ListImagesProducts() {
List < string > tempImages = new List < string > ();
List < string > tempDescription = new List < string > ();
Directory directory;
//Marca o diretório a ser listado
DirectoryInfo diretorio = new DirectoryInfo(@"C:\Users\Lucas\Desktop\Lucas B\Tutoriais\Programação\Programas de Teste\ThreeJay\ThreeJay\Content\images\Products");
//Executa função GetFile(Lista os arquivos desejados de acordo com o parametro)
FileInfo[] Images = diretorio.GetFiles("*.jpg");
FileInfo[] Description = diretorio.GetFiles("*.txt");
List < Directory > model = new List < Directory > ();
foreach(FileInfo fileinfo in Images) {
tempImages.Add(fileinfo.Name);
}
foreach(FileInfo fileinfo in Description) {
tempDescription.Add(fileinfo.Name);
}
for (int i = 0; i < tempImages.Count; i++) {
directory = new Directory();
directory.ImageName = tempImages.ElementAt(i);
directory.DescriptionName = tempDescription.ElementAt(i);
model.Insert(i, directory);
}
var ficheiro = model;
return Json(ficheiro, JsonRequestBehavior.AllowGet);
}
但我想通过 JQuery 读取文本文件。谁能帮我?