使用 jQuery 访问本地文件
我相信这个问题已经以几种不同的形式被问到,但我读过很多不同的答复。
起初,我有一个主要使用 jQuery 编写的 Web 应用程序,该应用程序将利用 servlet 从 JavaScript 无法访问的各个位置检索信息(即来自服务器的提要、图像等)。然而现在,我被告知要取消 servlet 和应用程序配置类,以便我的这个项目仅包含 HTML、CSS 和 JavaScript/jQuery。我需要从计算机上的本地文件中检索图像,而不是从服务器上获取图像。我知道允许这样做似乎是一个糟糕的设计,但这就是我被要求做的。无论如何,我真正需要做的是计算目录中图像文件的数量,然后也许编译文件名本身的数组。当使用 servlet 时,我可以在 Java 中很好地完成此操作,但如果没有它们,我不确定如何甚至是否可以完成此操作。
我基本上尝试使用 jQuery Cycle 插件像幻灯片一样循环浏览这些图像。我使用基于当前图像数量的循环将这些图像注入(或 $("#div").append())到 div 中。
那么,有没有办法使用 JavaScript、HTML、jQuery 插件等来做到这一点?我现在想避免使用 PHP 和 Java...
I believe that this question has been asked in a few different forms, but I've read quite a few different responses.
At first, I had a web-application written with mostly jQuery that would make use of servlets to retrieve information from various locations JavaScript could not access (ie. Feeds, images from a server, etc.). Now, however, I've been told to do away with the servlets and application configuration classes so that this project of mine contains only HTML, CSS, and JavaScript/jQuery. Rather than pulling the images off of the server, I need to retrieve them from a local file on the computer. I know that allowing this might seem like terrible design, but it's what I've been asked to do. At any rate, what I really need to do is count the number of image files in a directory and then perhaps compile an array of the filenames themselves. I could do this fine in Java when using the servlets, but without them, I'm not sure how or even if this can be done.
I'm basically trying to use the jQuery Cycle plug-in to cycle through these images like a slideshow. I inject (or $("#div").append()) these images into the div by using a loop based on the number of images present.
So, is there a way I can do this with using JavaScript, HTML, jQuery plug-in, etc? I'd like to avoid using PHP and Java at this point...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你不能仅仅使用 JavaScript 来读取目录;然而,似乎有一种方法可以使用 http://www.irt 来“利用”浏览器的功能.org/articles/js014/。它可能不太漂亮,但该演示对我来说可以在最新的 Chrome 和 IE7-9 中运行。我确信如果您想改进某些技术,可以更新以使用更清晰的代码。
编辑:
您可以使用的另一种技术可以在Javascript读取文件夹中的文件中找到
它看起来绝对是一个更清洁的解决方案。我建议提取正文内容以注入隐藏的 div 或使用可以读取的 iframe 的路径。
You can't just read a directory with JavaScript; however, there appears to be a way to "exploit" how browsers function using http://www.irt.org/articles/js014/. It may not be pretty, but the demo works in the latest Chrome and IE7-9 for me. I'm sure some of the techniques could be updated to use cleaner code if you'd like to improve upon it.
EDIT:
Another technique you could use can be found in Javascript read files in folder
It definitely looks to be a cleaner solution. What I'd recommend is extracting the body contents to inject into a hidden div or using the path for an iframe that you can read from.