构建失败 'FormData'
Failed to construct 'FormData'
当我在我的拖放区上传文件时,它不起作用。通常它工作得很好,但自从 1 个月以来我有这个 JS 错误:
Uncaught TypeError: Failed to construct 'FormData': parameter 1 is not of type 'HTMLFormElement'.
这是代码,当我使用 FormData 时:
var form_data = new FormData("#my-awesome-dropzone");
Dopzone 代码
Dropzone.options.myAwesomeDropzone = {
maxFilesize: 5,
maxFiles: 1,
addRemoveLinks: true,
dictResponseError: 'Server not Configured',
acceptedFiles: ".pdf",
init:function(){
var self = this;
// config
self.options.addRemoveLinks = true;
self.options.dictRemoveFile = "Delete";
//New file added
self.on("addedfile", function (file) {
console.log('new file added ', file);
if(!confirm("Do you want to upload the file?")){
this.removeFile(file);
return false;
}
});
// Send file starts
self.on("sending", function (file, xhr, formData) {
console.log('upload started', file);
$('.meter').show();
var form_data = new FormData("#my-awesome-dropzone");
$.ajax({
url: '/settings/uploadFile',
data: 'file=' + file.name ,
type: 'POST',
processData: false,
contentType: false,
success: function(response) {
}
});
});
// File upload Progress
self.on("totaluploadprogress", function (progress) {
console.log("progress ", progress);
$('.roller').width(progress + '%');
});
self.on("queuecomplete", function (progress) {
$('.meter').delay(999).slideUp(999);
});
// On removing file
self.on("removedfile", function (file) {
console.log(file);
});
}
HTML 代码
<form enctype="multipart/form-data" action="/settings/uploadFile" method="post" class="dropzone"
id="my-awesome-dropzone">
</form>
编辑 01-08-2019:好的,刚刚测试过,它适用于 Microsoft Edge 44.17763.1.0 但不适用于 Google Chrome 或Firefox,有什么解释吗?
您正在将 字符串 传递给 FormData
。正如错误所说,它需要一个表单元素,而不是一个字符串。所以:
var form_data = new FormData(document.getElementById("my-awesome-dropzone"));
实例:
var data = new FormData(document.getElementById("my-awesome-dropzone"));
console.log("Created FormData, " + [...data.keys()].length + " keys in data");
<form enctype="multipart/form-data" action="/settings/uploadFile" method="post" class="dropzone" id="my-awesome-dropzone">
<input type="text" name="x" value="kittens">
<input type="text" name="y" value="puppies">
</form>
当我在我的拖放区上传文件时,它不起作用。通常它工作得很好,但自从 1 个月以来我有这个 JS 错误:
Uncaught TypeError: Failed to construct 'FormData': parameter 1 is not of type 'HTMLFormElement'.
这是代码,当我使用 FormData 时:
var form_data = new FormData("#my-awesome-dropzone");
Dopzone 代码
Dropzone.options.myAwesomeDropzone = {
maxFilesize: 5,
maxFiles: 1,
addRemoveLinks: true,
dictResponseError: 'Server not Configured',
acceptedFiles: ".pdf",
init:function(){
var self = this;
// config
self.options.addRemoveLinks = true;
self.options.dictRemoveFile = "Delete";
//New file added
self.on("addedfile", function (file) {
console.log('new file added ', file);
if(!confirm("Do you want to upload the file?")){
this.removeFile(file);
return false;
}
});
// Send file starts
self.on("sending", function (file, xhr, formData) {
console.log('upload started', file);
$('.meter').show();
var form_data = new FormData("#my-awesome-dropzone");
$.ajax({
url: '/settings/uploadFile',
data: 'file=' + file.name ,
type: 'POST',
processData: false,
contentType: false,
success: function(response) {
}
});
});
// File upload Progress
self.on("totaluploadprogress", function (progress) {
console.log("progress ", progress);
$('.roller').width(progress + '%');
});
self.on("queuecomplete", function (progress) {
$('.meter').delay(999).slideUp(999);
});
// On removing file
self.on("removedfile", function (file) {
console.log(file);
});
}
HTML 代码
<form enctype="multipart/form-data" action="/settings/uploadFile" method="post" class="dropzone"
id="my-awesome-dropzone">
</form>
编辑 01-08-2019:好的,刚刚测试过,它适用于 Microsoft Edge 44.17763.1.0 但不适用于 Google Chrome 或Firefox,有什么解释吗?
您正在将 字符串 传递给 FormData
。正如错误所说,它需要一个表单元素,而不是一个字符串。所以:
var form_data = new FormData(document.getElementById("my-awesome-dropzone"));
实例:
var data = new FormData(document.getElementById("my-awesome-dropzone"));
console.log("Created FormData, " + [...data.keys()].length + " keys in data");
<form enctype="multipart/form-data" action="/settings/uploadFile" method="post" class="dropzone" id="my-awesome-dropzone">
<input type="text" name="x" value="kittens">
<input type="text" name="y" value="puppies">
</form>