编辑(2019年10月):
6年过去了,jQuery文件上传显然仍然让人们抓狂。如果你在这里找不到什么安慰,试试NPM搜索一个现代的替代方案。我保证它不值得麻烦。
在上一次修改中,我推荐使用Uploadify,但正如评论者指出的那样,他们似乎不再提供免费版本了。Uploadify已经太过时了。
编辑:
由于仍有流量过来,所以我会解释我最终是怎么做的。我最终通过遵循接受答案中的教程使插件工作。但是,jQuery文件上传非常麻烦,如果你正在寻找一个更简单的文件上传插件,我强烈推荐 [Uploadify](http://www.uploadify.com)。正如一个回答指出的,它只适用于非商业用途。
背景
我正在尝试使用blueimp的jQuery文件上传来允许用户上传文件。 开箱即用,按照设置说明运行得非常完美。但是为了在我的网站上实际使用它,我想能够做一些事情:
- 在我的任何现有页面中包含上传程序
- 更改上传文件的目录
插件的所有文件都位于根目录下的一个文件夹中。
我试过...
- 将演示页面移动到根目录并更新必要脚本的路径
- 如此处所建议的更改UploadHandler.php文件中的“upload_dir”和“upload_url”选项。
- 更改演示javascript第二行中的URL
在所有情况下,预览显示,进度条运行,但文件未能上传,并出现此控制台错误:Uncaught TypeError: Cannot read property 'files' of undefined
。我不理解插件的所有部分是如何工作的,这使得调试变得困难。
代码
演示页面中的javascript:
$(function () {
'use strict';
// Change this to the location of your server-side upload handler:
var url = 'file_upload/server/php/UploadHandler.php',
uploadButton = $('<button/>')
.addClass('btn')
.prop('disabled', true)
.text('Processing...')
.on('click', function () {
var $this = $(this),
data = $this.data();
$this
.off('click')
.text('Abort')
.on('click', function () {
$this.remove();
data.abort();
});
data.submit().always(function () {
$this.remove();
});
});
$('#fileupload').fileupload({
url: url,
dataType: 'json',
autoUpload: false,
acceptFileTypes: /(\.|\/)(gif|jpe?g|png)$/i,
maxFileSize: 5000000, // 5 MB
// Enable image resizing, except for Android and Opera,
// which actually support image resizing, but fail to
// send Blob objects via XHR requests:
disableImageResize: /Android(?!.*Chrome)|Opera/
.test(window.navigator.userAgent),
previewMaxWidth: 100,
previewMaxHeight: 100,
previewCrop: true
}).on('fileuploadadd', function (e, data) {
data.context = $('<div/>').appendTo('#files');
$.each(data.files, function (index, file) {
var node = $('<p/>')
.append($('<span/>').text(file.name));
if (!index) {
node
.append('<br>')
.append(uploadButton.clone(true).data(data));
}
node.appendTo(data.context);
});
}).on('fileuploadprocessalways', function (e, data) {
var index = data.index,
file = data.files[index],
node = $(data.context.children()[index]);
if (file.preview) {
node
.prepend('<br>')
.prepend(file.preview);
}
if (file.error) {
node
.append('<br>')
.append(file.error);
}
if (index + 1 === data.files.length) {
data.context.find('button')
.text('Upload')
.prop('disabled', !!data.files.error);
}
}).on('fileuploadprogressall', function (e, data) {
var progress = parseInt(data.loaded / data.total * 100, 10);
$('#progress .bar').css(
'width',
progress + '%'
);
}).on('fileuploaddone', function (e, data) {
$.each(data.result.files, function (index, file) {
var link = $('<a>')
.attr('target', '_blank')
.prop('href', file.url);
$(data.context.children()[index])
.wrap(link);
});
}).on('fileuploadfail', function (e, data) {
$.each(data.result.files, function (index, file) {
var error = $('<span/>').text(file.error);
$(data.context.children()[index])
.append('<br>')
.append(error);
});
}).prop('disabled', !$.support.fileInput)
.parent().addClass($.support.fileInput ? undefined : 'disabled');
});
我对缺乏文档感到惊讶,这似乎应该是一个简单的更改。如果有人能够解释如何做到这一点,我将不胜感激。