【发布时间】:2013-07-29 20:55:22
【问题描述】:
编辑(2019 年 10 月):
6 年后,jQuery File Upload 显然仍然让人们发疯。如果您在此处的答案中找不到一点安慰,请尝试search of NPM 以获得现代替代方案。我保证,这不值得麻烦。
我在之前的编辑中推荐了 Uploadify,但正如评论者指出的那样,他们似乎不再提供免费版本。无论如何,Uploadify 是 所以 2013 年。
编辑:
这似乎仍在吸引流量,所以我将解释我最终做了什么。我最终按照已接受答案中的教程使插件正常工作。背景
我正在尝试使用 blueimp 的 jQuery File Upload 来允许用户上传文件。 开箱即可完美运行,按照设置说明进行操作。但要在我的网站上实际使用它,我希望能够做几件事:
- 在我的任何现有页面中包含上传者
- 更改上传文件的目录
插件的所有文件都位于根目录下的文件夹中。
我试过了……
- 将演示页面移至根目录并更新必要脚本的路径
- 按照建议 here 更改 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');
});
我对缺乏文档感到惊讶;似乎改变应该是一件简单的事情。如果有人能解释如何做到这一点,我将不胜感激。
【问题讨论】:
-
好的问题格式。很高兴看到组织。
-
在错误行之前的控制台中打印'e'和'data',值是什么?
-
Uploadify 是 MIT 许可证,例如它是完全免费的。但是,来自同一 dev/website 的上传五个是 5-100 美元,具体取决于使用情况
-
两年来,jQuery-File-Upload 文档并没有变得更好。啊。
-
@MartinJH 可能在某个时候进行了uploadify,但目前只有一个-付费uploadiFive 版本。而且也没有演示;这只是一个视频。形式不佳。
标签: jquery file-upload jquery-plugins jquery-file-upload