找不到jqueryFileTree连接器脚本
我正在尝试创建一个网页,用来显示文件树。我选择使用这个网站上的jqueryFileTree:http://www.abeautifulsite.net/blog/2008/03/jquery-file-tree/
最终,我会把这个文件树连接到一个服务器上。不过现在,我只是想在我的Windows电脑上,使用Node.js的本地服务器让它工作。
因为这个jquery插件需要一个连接脚本(用来获取目录信息),所以我选择使用他们提供的python连接脚本,jqueryFileTree.py。
我遇到的问题是,本地服务器告诉我找不到这个python脚本(错误404)。但是,我知道我提供的文件路径是正确的,因为当我在浏览器中输入这个网址时,我可以下载到这个脚本。这种情况让我感到困惑,因为它告诉我一件事,但实际却是另一回事。
下面是我所有相关的代码和信息。
目录结构:
|-public (localhost)
|-connectors
|-jqueryFileTree.py
创建jqueryFileTree对象的代码:
$(window).load(
function()
{
$('#fileTree').fileTree({
root: '/',
script: 'connectors/jqueryFileTree.py',
expandSpeed: 500,
collapseSpeed: 500,
multiFolder: true
},
function(file)
{
alert(file);
});
});
这是我的错误信息:
Failed to load resource: the server responded with a status of 404 (Not Found): http://localhost:3700/connectors/jqueryFileTree.py
下面是jqueryFileTree和连接脚本的代码。我并不是自己写的这些代码,而是从我之前提到的网站下载插件时自带的。
插件代码:
// jQuery File Tree Plugin
//
// Version 1.01
//
// Cory S.N. LaViska
// A Beautiful Site (http://abeautifulsite.net/)
// 24 March 2008
//
// Visit http://abeautifulsite.net/notebook.php?article=58 for more information
//
// Usage: $('.fileTreeDemo').fileTree( options, callback )
//
// Options: root - root folder to display; default = /
// script - location of the serverside AJAX file to use; default = jqueryFileTree.php
// folderEvent - event to trigger expand/collapse; default = click
// expandSpeed - default = 500 (ms); use -1 for no animation
// collapseSpeed - default = 500 (ms); use -1 for no animation
// expandEasing - easing function to use on expand (optional)
// collapseEasing - easing function to use on collapse (optional)
// multiFolder - whether or not to limit the browser to one subfolder at a time
// loadMessage - Message to display while initial tree loads (can be HTML)
//
// History:
//
// 1.01 - updated to work with foreign characters in directory/file names (12 April 2008)
// 1.00 - released (24 March 2008)
//
// TERMS OF USE
//
// This plugin is dual-licensed under the GNU General Public License and the MIT License and
// is copyright 2008 A Beautiful Site, LLC.
//
if(jQuery) (function($){
$.extend($.fn, {
fileTree: function(o, h) {
// Defaults
if( !o ) var o = {};
if( o.root == undefined ) o.root = '/';
if( o.script == undefined ) o.script = 'jqueryFileTree.py';
if( o.folderEvent == undefined ) o.folderEvent = 'click';
if( o.expandSpeed == undefined ) o.expandSpeed= 500;
if( o.collapseSpeed == undefined ) o.collapseSpeed= 500;
if( o.expandEasing == undefined ) o.expandEasing = null;
if( o.collapseEasing == undefined ) o.collapseEasing = null;
if( o.multiFolder == undefined ) o.multiFolder = true;
if( o.loadMessage == undefined ) o.loadMessage = 'Loading...';
$(this).each( function() {
function showTree(c, t) {
$(c).addClass('wait');
$(".jqueryFileTree.start").remove();
$.post(o.script, { dir: t }, function(data) {
$(c).find('.start').html('');
$(c).removeClass('wait').append(data);
if( o.root == t ) $(c).find('UL:hidden').show(); else $(c).find('UL:hidden').slideDown({ duration: o.expandSpeed, easing: o.expandEasing });
bindTree(c);
});
}
function bindTree(t) {
$(t).find('LI A').bind(o.folderEvent, function() {
if( $(this).parent().hasClass('directory') ) {
if( $(this).parent().hasClass('collapsed') ) {
// Expand
if( !o.multiFolder ) {
$(this).parent().parent().find('UL').slideUp({ duration: o.collapseSpeed, easing: o.collapseEasing });
$(this).parent().parent().find('LI.directory').removeClass('expanded').addClass('collapsed');
}
$(this).parent().find('UL').remove(); // cleanup
showTree( $(this).parent(), escape($(this).attr('rel').match( /.*\// )) );
$(this).parent().removeClass('collapsed').addClass('expanded');
} else {
// Collapse
$(this).parent().find('UL').slideUp({ duration: o.collapseSpeed, easing: o.collapseEasing });
$(this).parent().removeClass('expanded').addClass('collapsed');
}
} else {
h($(this).attr('rel'));
}
return false;
});
// Prevent A from triggering the # on non-click events
if( o.folderEvent.toLowerCase != 'click' ) $(t).find('LI A').bind('click', function() { return false; });
}
// Loading message
$(this).html('<ul class="jqueryFileTree start"><li class="wait">' + o.loadMessage + '<li></ul>');
// Get the initial file list
showTree( $(this), escape(o.root) );
});
}
});
})(jQuery);
连接脚本代码:
#
# jQuery File Tree
# Python/Django connector script
# By Martin Skou
#
import os
import urllib
def dirlist(request):
r=['<ul class="jqueryFileTree" style="display: none;">']
try:
r=['<ul class="jqueryFileTree" style="display: none;">']
d=urllib.unquote(request.POST.get('dir','c:\\temp'))
for f in os.listdir(d):
ff=os.path.join(d,f)
if os.path.isdir(ff):
r.append('<li class="directory collapsed"><a href="#" rel="%s/">%s</a></li>' % (ff,f))
else:
e=os.path.splitext(f)[1][1:] # get .ext and remove dot
r.append('<li class="file ext_%s"><a href="#" rel="%s">%s</a></li>' % (e,ff,f))
r.append('</ul>')
except Exception,e:
r.append('Could not load directory: %s' % str(e))
r.append('</ul>')
return HttpResponse(''.join(r))
我可能提供了比需要的更多信息。我想尽量详细,以免后面还要回复和补充更多信息。也就是说,如果你需要更多信息,请随时问我。
我非常感谢任何帮助!
3 个回答
我觉得你的问题出在这里:
script: 'connectors/jqueryFileTree.py',
你的JavaScript代码是在用户的浏览器上运行的,所以它看不到服务器上的Python文件。
相反,你应该使用Django视图可以访问的URL。可以查看Django的URL路由机制,了解如何找到与你的视图(“dirlist”)相关的URL。点击这里查看。
请原谅我没有仔细检查你的脚本,也因为回复得晚。不过,我遇到过同样的问题,并且相信我通过以下链接中的方法解决了这个问题:http://pastebin.com/kVkTssdG 和 http://www.abeautifulsite.net/jquery-file-tree/
具体步骤如下:
- 我在项目的 'node_modules' 子文件夹中创建了一个名为 'jQFTree' 的新文件夹。
- 我在 node.js 的 app.js 文件中添加了以下几行代码:
- 在我的表单的 HTML 中添加了:
- 我之前创建的 jQFTree 文件夹里只有一个名为 'index.js' 的文件,里面有以下的 JavaScript 代码:
var jQFTree=require('jQFTree');
还有:
app.post('/FileTree', jQFTree.handlePost);
div(id="FileTree" class="fTre")
以及相应的脚本:
var orig = window.location.origin + '/FileTree';
var servDir='Mats';//or Carps or Rugs
$("#FileTree").fileTree({
root: servDir,
script: orig
},
followed by callback function.
//my Node.js module for jQuery File Tree
var path = require('path');
var fs = require('fs');
var util = require('util');
var stringHeader = "<ul class='jqueryFileTree' style='display: none;'>";
var stringFooter = "</ul>";
// arguments: path, directory name
var formatDirectory =
"<li class='directory collapsed'><a href='#' rel='%s/'>%s</a><li>";
// arguments: extension, path, filename
var formatFile = "<li class='file ext_%s'><a href='#' rel='%s'>%s</a></li>";
var createStatCallback = (function (res, path, fileName, isLast) {
return function (err, stats) {
if (stats.isDirectory()) {
res.write(util.format(formatDirectory, path, fileName));
}
else {
var fileExt = fileName.slice(fileName.lastIndexOf('.') + 1);
res.write(util.format(formatFile, fileExt, path, fileName));
}
if (isLast) {
res.end(stringFooter);
}
}
});
exports.handlePost = function (req, res) {
//for safety; only directory name is available from the browser
//through the req.body.dir variable
//but complete dir path in server is required for dir browsing
// 'text/html'
//console.log('Filetree handles post dir: '+ req.body.dir);
res.writeHead(200, { 'content-type': 'text/plain' });
res.write(stringHeader);
// get a list of files
var seeDir = req.body.dir;
if (seeDir.indexOf('%5C') > -1 || seeDir.indexOf('%2F') > -1) {
//directory path was returned as a subfolder from jQuery FileTree using URL encoding
//decode it first:
seeDir = decodeURIComponent(seeDir);
//console.log('seeDir: '+seeDir+' __dirname:'+__dirname);
if (seeDir.slice(-1) == "/" || seeDir.slice(-1) == "\\") {
//last char either / (MS-windows) or \ (not MS-windows) is dropped
seeDir = seeDir.slice(0, seeDir.length - 1)
if (seeDir.indexOf('/') > -1) {
seeDir = seeDir + '/';
}
else {
seeDir = seeDir + '\\';
}
}
}
if (req.body.dir == 'Mats' || req.body.dir == 'Carps' || req.body.dir == 'Rugs') {
//restricted to three sub dirs in server directory: public/MatsCarpsRugs/
//console.log('.=%s', path.resolve('.'));
//gives path to the above app.js calling this module (=as wanted)
//combine path to requested directory:
seeDir = path.join(path.resolve('.'), 'public', 'MatsCarpsRugs', req.body.dir);
if (seeDir.indexOf('/')>-1){
//web-browser not in ms-windows filesystem
seeDir=seeDir+'/';
}
else{
//web-browser is in ms-windows filesystem
seeDir=seeDir+'\\';
}
//console.log('seeDir now: '+seeDir);
}
fs.readdir(seeDir, function (err, files) {
var fileName = '';
var path = '';
var statCallback;
if (files.length === 0) {
path = util.format('%s%s', seeDir, fileName);
statCallback = createStatCallback(res, path, fileName, true);
fs.stat(path, statCallback);
}
for (var i = 0; i < files.length; i++) {
fileName = files[i];
path = util.format('%s%s', seeDir, fileName);
var isLast = (i === (files.length - 1));
statCallback = createStatCallback(res, path, fileName, isLast);
fs.stat(path, statCallback);
}
});
}
希望这些信息能帮助你或其他人找到解决方案。
试试这个方法:不要用这个路径 'connectors/jqueryFileTree.py',改用你在浏览器中下载脚本时使用的完整网址。