Javascript - 检索文件夹中文件的名称 [英] Javascript - Retrieve names of files in a folder

查看:107
本文介绍了Javascript - 检索文件夹中文件的名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个要求,我需要从客户端的文件夹中检索所有文件名。

I have a requirement where I need to retrieve all the filenames from a folder in client side.

因此我试图检索文件中的文件名文件夹使用Jquery参考此答案

Hence I am trying to retrieve the names of the files in a folder using Jquery referring to this answer.

我的代码是如下:

    <script>
        var fileExt = ".xml";

        $(document).ready(
        function(){
            $.ajax({
            //This will retrieve the contents of the folder if the folder is configured as 'browsable'
            url: 'xml/',
            success: function (data) {
               $("#fileNames").html('<ul>');
               //List all xml file names in the page
               $(data).find('a:contains(" + fileExt + ")').each(function () {
                   var filename = this.href.replace(window.location, "").replace("http:///", "");
                   $("#fileNames").append( '<li>'+filename+'</li>');
               });
               $("#fileNames").append('</ul>');
             }     
            });
        });

    </script>

HTML代码如下:

<div id="fileNames"></div>

但是当我在chrome和firefox中运行代码时出现以下错误:

But I get the following error when I run the code in chrome and firefox:


chrome:XMLHttpRequest无法加载file:/// E:/ Test / xml /。收到
无效回复。因此不允许原点'null'访问。

chrome: XMLHttpRequest cannot load file:///E:/Test/xml/. Received an invalid response. Origin 'null' is therefore not allowed access.

Firefox:ReferenceError:$未定义

Firefox: ReferenceError: $ is not defined

我尝试了很多搜索,但错误没有解决。

I have tried googling a lot but the error is not resolved.

非常感谢您的帮助。

推荐答案

<html>
<body>
    <div id='fileNames'></div>
</body>
<script src="js/jquery.js"></script>

<script type="text/javascript">
    $(document).ready(function () 
    {
        $.get(".", function(data) 
        {
            $("#fileNames").append(data);
        });
    })
</script>

这将打印所有文件在网页上的文件夹中。

this will print all the files in a folder on webpage.

这篇关于Javascript - 检索文件夹中文件的名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆