编辑(2019 年 10 月):
6 年过去了,jQuery 文件上传显然仍然让人抓狂。如果您在此处的答案中找不到任何安慰,请尝试搜索NPM https://www.npmjs.com/search?q=file%20upload一个现代的替代方案。我保证,这不值得这么麻烦。
我在之前的编辑中推荐了 Uploadify,但正如评论者指出的那样,他们似乎不再提供免费版本。上传是so无论如何,2013年。
EDIT:
This still seems to be getting traffic so I'll explain what I ended up doing. I eventually got the plugin working by following the tutorial in the accepted answer.
However, jQuery File Upload is a real hassle and if you're looking for a simpler file upload plugin, I would highly recommend [Uploadify](http://www.uploadify.com). As an answer pointed out, it is only free for non-commercial use.
背景
我正在尝试使用 blueimp 的jQuery 文件上传 https://github.com/blueimp/jQuery-File-Upload允许用户上传文件。开箱即用,完美运行,按照设置说明进行操作。但为了在我的网站上实际使用它,我希望能够做几件事:
- 将上传器包含在我的任何现有页面上
- 更改上传文件的目录
该插件的所有文件都位于根目录下的文件夹中。
我试过了...
- 将演示页面移动到根目录并更新必要脚本的路径
- 按照建议更改 UploadHandler.php 文件中的“upload_dir”和“upload_url”选项here https://github.com/blueimp/jQuery-File-Upload/issues/1190.
- 更改演示 javascript 第二行中的 URL
在所有情况下,都会显示预览,并且进度条运行,但文件无法上传,并且我在控制台中收到此错误:Uncaught TypeError: Cannot read property 'files' of undefined
。我不明白插件的所有部分是如何工作的,这使得调试变得困难。
Code
演示页面中的 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');
});
I'm surprised by the lack of documentation; it seems like it should be a simple thing to change. I would appreciate if someone could explain how to do this.