编辑(2019 年 10 月):
6 年后,jQuery File Upload 显然仍然让人们发疯。如果您在这里的答案中找不到一点安慰,请尝试在NPM 中搜索现代替代方案。我保证,这不值得麻烦。
我在之前的编辑中推荐了 Uploadify,但正如评论者指出的那样,他们似乎不再提供免费版本。无论如何, Uploadify 都是2013年的。
编辑:
这似乎仍在吸引流量,所以我将解释我最终做了什么。我最终按照已接受答案中的教程使插件正常工作。背景
我正在尝试使用 blueimp 的jQuery File Upload来允许用户上传文件。开箱即用,按照设置说明完美运行。但是要在我的网站上实际使用它,我希望能够做几件事:
- 在我现有的任何页面上包含上传器
- 更改上传文件的目录
该插件的所有文件都位于根目录下的文件夹中。
我试过了...
- 将演示页面移动到根目录并更新必要脚本的路径
- 按照此处的建议更改 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');
});
我对缺乏文档感到惊讶;似乎改变应该是一件简单的事情。如果有人能解释如何做到这一点,我将不胜感激。