我有一个“新产品”表单,其中包含多个输入(名称,描述等)和一个“添加图像”按钮(只需在您点击时加载预览图像)
一旦我提交“新产品”表单(此时图片未上传),如果MySQL查询正常,则返回最后插入的ID 。然后,我想提交带有最后插入ID 变量的图像上传(用于生成文件的名称,即:123465.jpg)我在第一次提交时获得了“新产品“形式。它有意义吗?
所以,我需要在此代码之外提交图片上传表单:
<script>
$(function () {
$('#fileupload').fileupload({
dataType: 'json',
url: location.protocol + '//' + location.hostname + '/files/index.php',
dataType: 'json',
add: function (e, data) {
var uploadErrors = [];
var acceptFileTypes = /^image\/(jpe?g|png)|application\/(pdf|doc|xls|vnd.openxmlformats-officedocument.spreadsheetml.sheet)$/i;
if(data.originalFiles[0]['type'].length && !acceptFileTypes.test(data.originalFiles[0]['type'])) {
uploadErrors.push("Bad file : " + '\n\n' + data.originalFiles[0]['name'] + '\n\n' + "Accepted : " + '\n' + "jpg, png, pdf, doc, xls, xlsx ou zip.");
}
if(data.originalFiles[0]['size'].length && data.originalFiles[0]['size'] > 5000000) {
uploadErrors.push("Too heavy : " + '\n\n' + data.originalFiles[0]['name']);
}
if(uploadErrors.length > 0) {
alert(uploadErrors.join("\n"));
} else {
$('#preview').html('<img src="' + URL.createObjectURL(data.files[0]) + '"/>');
}
},
done: function (e, data) {
$.each(data.result.files, function (index, file) {
$('<p/>').text(file.name).appendTo(document.body);
});
},
progressall: function (e, data) {
var progress = parseInt(data.loaded / data.total * 100, 10);
console.log(progress);
}
});
});
</script>
有什么想法吗?
答案 0 :(得分:0)
// Initialize the jQuery File Upload widget:
$('.fileupload').fileupload({
url: 'image_upload/server/php/index.php',
maxFileSize: 5000 * 1000,
acceptFileTypes: /(\.|\/)(gif|jpe?g|png)$/i
}).on('fileuploadsubmit', function (e, data) {
alert("submit triggered");
//Code which needs to comes after submit
});