javascript - 通过 AJAX 提交表单后使用 Uppy 上传文件
问题描述
我正在使用Uppy文件上传器来上传文件。现在我有一个以 AJAX 形式发送数据的表单,服务器将表单数据存储在 DB 中。除了表单,我想将 Uppy 文件附加到表单中上传的记录中。因此,一旦记录和文件上传,我希望从服务器返回成功消息,
如何触发文件上传和表单提交?
//下面是AJAX请求
jQuery.ajax({
type: "POST",
url:
$("#expense_id").val() == ""
? "/purchases/expenses/store"
: "/purchases/expenses/update",
data: $("#addExpenseForm").serialize(),
success: function(data) {
if (data.val == 1) {
//Success notification..
_Notification(data);
},
error: function(xhr) {
$("#errors").text("");
jQuery.each(xhr.responseJSON.errors, function(key, value) {
$("#errorbox").show();
$("#errors").append("<p>" + value + "</p>");
});
}
}); //AJAX End
//下面是Uppy上传功能
var _StoreExpenseFile = function() {
const XHRUpload = Uppy.XHRUpload;
const Dashboard = Uppy.Dashboard;
var id = "#storefile";
var expense_id = $("#id").val();
var options = {
proudlyDisplayPoweredByUppy: false,
target: id,
inline: true,
replaceTargetContent: true,
showProgressDetails: true,
note: "Only PDF, jpeg, img, files allowed.",
height: 470,
metaFields: [
{ id: "name", name: "Name", placeholder: "file name" },
{
id: "caption",
name: "Caption",
placeholder: "describe what the image is about"
}
],
browserBackButtonClose: true
};
var uppyDashboard = Uppy.Core({
autoProceed: false,
restrictions: {
maxFileSize: 5000000, // 1mb
maxNumberOfFiles: 5,
minNumberOfFiles: 1
},
meta: {
expense_id: expense_id //Pass additional meta data for process.
}
});
uppyDashboard.use(Dashboard, options);
uppyDashboard.use(XHRUpload, {
endpoint: "/files/store/expensefile",
headers: {
"X-CSRF-TOKEN": $('meta[name="csrf-token"]').attr("content")
},
formData: true,
fieldName: "file"
});
};
解决方案
推荐阅读
- javascript - 为什么每次都安装我的嵌套递归模式?
- python - Scrapy如何抓取tiktok中的视频链接
- php - 是否可以将视图中的复选框功能调用到另一个视图?因此,如果选中一个,则语句显示
- spring-boot - 没有 orderBy 的 Spring Boot CRUD findall:顺序可预测吗?
- sql - 在 BigQuery SQL 中查找由逗号分隔的项目组成的两列之间的交集
- python - 如何修复前景蒙版上不准确的边界框?
- jenkins - 传递给管道 B 的文件并在詹金斯中读取和附加管道 A 共享的文件内容
- linux - Conda 在停用另一个环境后忘记了基本环境名称
- javascript - 试图根据以下输出要求将句子拆分成
- kendo-asp.net-mvc - asp kendo multiselect 通过 jquery 手动选择项目