使用jQuery获取给定目录中的文件名 [英] get filenames in a given directory using jQuery

查看:92
本文介绍了使用jQuery获取给定目录中的文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(我发现了一些类似的问题,但没有一个解决了我的问题,所以我认为这不是重复的问题)

(I found some similar questions but none of them solved my problem so this is not a duplicated question I think)

我想使用jQuery在我的一个文件夹中检索文件名.我尝试了以下方法,但仍然无法获取每个文件名.

I want to retrieve filenames in one of my folder using jQuery. I tried the following method but I still can't get each filename.

$.get(".", function(data) {
    $("#divID").append(data);
});

但是我注意到'data'的类型是字符串,并且它的末尾包含文件名,如下所示:

But I noticed that the type of 'data' is string and it contains filenames at the end of it like this:

<script>addRow("filename.csv","filename.csv",0,238618,"233 kB",1512119177,"12/1/17, 5:06:17 PM");</script>

所以无论如何,我可以从数据"中检索文件名吗? (不使用正则表达式)

So is there anyway I can retrieve the filenames from 'data'? (not by using regex)

推荐答案

您无法在客户端计算机上读取文件.您可以通过修改标志来出于开发目的访问它们.在这里查看我的答案这个.之后,请继续执行以下代码.

You cannot read files in a client's machine. You may access them for development purpose by modifying a flag. Look at my answer here regarding this. After this proceed with the below code.

两者都可以.

使用 PURE JAVASCRIPT :

var filenames=[], foldernames=[];
var url = "file:///Users/Default/Downloads";
var req = new XMLHttpRequest();
req.open("GET",url,true);
req.onreadystatechange=function(){
    if(req.readyState === 4)
    {
        document.write(req.responseText);
        getNames();
    }
};
req.send();

function getNames()
{
    var files = document.querySelectorAll("a.icon.file");
    var folders = document.querySelectorAll("a.icon.dir");
    files.forEach(function(item){filenames.push(item.textContent)})
    folders.forEach(function(item){foldernames.push(item.textContent.slice(0,-1))})
    console.log(filenames);
    console.log(foldernames);
}

使用 JQUERY :

var filenames=[], foldernames=[];

$.get("file:///Users/Default/Downloads",function(response){
    document.write(response);
    getNames();
});

function getNames()
{
    var files = document.querySelectorAll("a.icon.file");
    var folders = document.querySelectorAll("a.icon.dir");
    files.forEach(function(item){filenames.push(item.textContent)})
    folders.forEach(function(item){foldernames.push(item.textContent.slice(0,-1))})
    console.log(filenames);
    console.log(foldernames);
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

这篇关于使用jQuery获取给定目录中的文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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