javascript - 使用 jQuery File Upload 上传文件时 JSON 输入错误意外结束
问题描述
我正在使用jQuery File Upload Plugin将文档/附件上传到基于 asp.net 核心的服务器。
上传附件时出现以下错误Error SyntaxError: Unexpected end of JSON input
在 jQuery File Upload 的演示(基本 UI)项目之后,我什么也没做。
这是我的 JavaScript 代码
$(function () {
'use strict';
// Change this to the location of your server-side upload handler:
var url = '/api/Document',
uploadButton = $('<button/>')
.addClass('btn btn-primary')
.prop('disabled', false)
.text('Progress..')
.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|doc|docx|pdf|txt)$/i,
maxFileSize: 999000,
// 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)).append('<hr/><br>');
}
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 + '%'
);
}).on('fileuploaddone', function (e, data) {
$.each(data.result.files, function (index, file) {
if (file.url) {
var link = $('<a>')
.attr('target', '_blank')
.prop('href', file.url);
$(data.context.children()[index])
.wrap(link);
} else if (file.error) {
var error = $('<span class="text-danger"/>').text(file.error);
$(data.context.children()[index])
.append('<br>')
.append(error);
}
});
}).on('fileuploadfail', function (e, data) {
$.each(data.files, function (index) {
var error = $('<span class="text-danger"/>').text('File upload failed.');
$(data.context.children()[index])
.append('<br>')
.append(error);
});
}).prop('disabled', !$.support.fileInput)
.parent().addClass($.support.fileInput ? undefined : 'disabled');
});
并且在上传按钮click
事件触发时调用此控制器操作。
行动是
[HttpPost]
public async Task Post([FromForm]AddCommand command)
{
command.IsContactDoc = true;
command.CreatedBy = Guid.Parse(_userManager.GetUserId(HttpContext.User));
await _mediator.Send(command);
return;
}
更进一步..在调试时我也得到了命令参数为空。
请有人解释我在哪里做错了..
解决方案
您需要将输入的名称与控制器参数相对应。参考asp.net core 中的上传文件。
<input id="fileupload" type="file" name="files" multiple>
你的行动:
[HttpPost]
public async Task Post([FromForm] List<IFormFile> files)
推荐阅读
- python-3.x - pyxl 中有没有办法保留现有的宏?
- javascript - 当javascript中有未定义的值时无法解析json
- performance - 使用 elasticsearch 优化服务器操作:解决低磁盘水印
- r - 如何为每个变量创建一个具有相同分母的条形图?
- python - 使用正则表达式在文本python中使用缩写的第一个字母匹配相邻的单词
- copy - 如何从 ADF 复制数据活动中获取给定列的最大值
- reactjs - 如何在反应中使用 videojs-airplay 插件?
- c++ - 使用 MFC 以编程方式更改文件扩展名关联?
- wpf - 使用相对路径时媒体时间线源无法正确加载 - WPF
- java - 无法让程序打印到命令行或报告文件