【发布时间】:2015-07-28 20:48:46
【问题描述】:
我使用了 jQuery-File-Upload 插件 (https://github.com/blueimp/jQuery-File-Upload),但它没有帮助。 这是我的html:
<!-- The fileinput-button span is used to style the file input field as button -->
<span class="btn btn-success fileinput-button">
<i class="glyphicon glyphicon-plus"></i>
<span>Add files...</span>
<!-- The file input field used as target for the file upload widget -->
<input id="fileupload" type="file" name="files[]" multiple>
<input type="hidden" class="hidden-token" name="_token" value="{!! csrf_token() !!}">
</span>
<br>
<br>
<!-- The global progress bar -->
<div id="progress" class="progress">
<div class="progress-bar progress-bar-success"></div>
</div>
<!-- The container for the uploaded files -->
<div id="files" class="files"></div>
还有我的脚本:
/*jslint unparam: true, regexp: true */
/*global window, $ */
$(function () {
'use strict';
// Change this to the location of your server-side upload handler:
var url = window.location.hostname === 'blueimp.github.io' ?
'//jquery-file-upload.appspot.com/' : 'server/php/',
uploadButton = $('<button/>')
.addClass('btn btn-primary')
.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: '/upload/artist/image',
method: 'post',
dataType: 'json',
autoUpload: false,
formData: {_token: $('.hidden-token').val()},
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,
success: function(){
alert('ok');
},
error: function(){
alert('error');
},
}).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($('<span class="text-danger"/>').text(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 .progress-bar').css(
'width',
progress + '%'
);
}).prop('disabled', !$.support.fileInput)
.parent().addClass($.support.fileInput ? undefined : 'disabled');
});
</script>
我的路由.php:
Route::post('/upload/artist/image', function() {
return view('upload.artist-image');
});
最后在artist-image.php中我使用了这个简单的代码:
return response()->json(['name' => 'Abigail', 'state' => 'CA']);
但在我的 ajax 中,即使在控制台和网络(chrome 检查元素)中也没有任何响应,并且我的上传总是在这个插件中返回失败文本。
【问题讨论】:
-
你应该努力解决你的问题,即使在尝试之后你也找不到任何东西,那么只有你应该连同你的代码一起提出问题,这样如果你错过了什么,这里的人可以帮助你你的代码。
-
好的。对不起。我纠正了它。 @KhanShahrukh