为了让您的上传者进入有效的可重复表单,您需要做的是检查每个表单并将 ID 替换为有意义的唯一 ID,并允许每个实例单独运行。
我将代码分为两个步骤。第一步是将无效的 HTML 转换为有效的 HTML:
function runner(index) {
var form = document.getElementById('upload_form');
if (!form) return false;
form.id = 'upload_form-' + index;
var children = document.querySelectorAll('#upload_form-' + index + ' *');
for (i = 0; i < children.length; i++) {
if (children[i].id) {
children[i].id = children[i].id + '-' + index;
}
}
return true;
}
var index = 0;
while (runner(index)) {
index++;
}
这会遍历您页面中 ID 为 upload_form 的所有表单,并在他们的 ids 和他们孩子的 ids 之后附加一个漂亮的小 index,确保它们变得独一无二。
这是一个小测试:
function runner(index) {
var form = document.getElementById('upload_form');
if (!form) return false;
form.id = 'upload_form-' + index;
var children = document.querySelectorAll('#upload_form-' + index + ' *');
for (i = 0; i < children.length; i++) {
if (children[i].id) {
children[i].id = children[i].id + '-' + index;
}
}
return true;
}
var index = 0;
while (runner(index)) {
index++;
}
<form id="upload_form" enctype="multipart/form-data" method="post">
<div class="file has-name is-fullwidth is-info">
<label class="file-label">
<input class="file-input" type="file" name="file1" id="file1" data-uploadValue="{{ item[0] }}" onchange="uploadFile(this)"><br>
<span class="file-cta">
<span class="file-icon">
<i class="fa fa-upload"></i>
</span>
<span class="file-label">
Choose a file…
</span>
</span>
<span class="file-name">
<div style="color:red;" id="status"></div>
Supported file types: .png, .jpg, .jpeg and .gif
</span>
</label>
<div style="display:none">
<p id="loaded_n_total"></p>
<progress id="progressBar" class="progress" value="0" max="100" style="width:300px;"></progress></div>
</div>
</form>
<form id="upload_form" enctype="multipart/form-data" method="post">
<div class="file has-name is-fullwidth is-info">
<label class="file-label">
<input class="file-input" type="file" name="file1" id="file1" data-uploadValue="{{ item[0] }}" onchange="uploadFile(this)"><br>
<span class="file-cta">
<span class="file-icon">
<i class="fa fa-upload"></i>
</span>
<span class="file-label">
Choose a file…
</span>
</span>
<span class="file-name">
<div style="color:red;" id="status"></div>
Supported file types: .png, .jpg, .jpeg and .gif
</span>
</label>
<div style="display:none">
<p id="loaded_n_total"></p>
<progress id="progressBar" class="progress" value="0" max="100" style="width:300px;"></progress></div>
</div>
</form>
<form id="upload_form" enctype="multipart/form-data" method="post">
<div class="file has-name is-fullwidth is-info">
<label class="file-label">
<input class="file-input" type="file" name="file1" id="file1" data-uploadValue="{{ item[0] }}" onchange="uploadFile(this)"><br>
<span class="file-cta">
<span class="file-icon">
<i class="fa fa-upload"></i>
</span>
<span class="file-label">
Choose a file…
</span>
</span>
<span class="file-name">
<div style="color:red;" id="status"></div>
Supported file types: .png, .jpg, .jpeg and .gif
</span>
</label>
<div style="display:none">
<p id="loaded_n_total"></p>
<progress id="progressBar" class="progress" value="0" max="100" style="width:300px;"></progress></div>
</div>
</form>
<form id="upload_form" enctype="multipart/form-data" method="post">
<div class="file has-name is-fullwidth is-info">
<label class="file-label">
<input class="file-input" type="file" name="file1" id="file1" data-uploadValue="{{ item[0] }}" onchange="uploadFile(this)"><br>
<span class="file-cta">
<span class="file-icon">
<i class="fa fa-upload"></i>
</span>
<span class="file-label">
Choose a file…
</span>
</span>
<span class="file-name">
<div style="color:red;" id="status"></div>
Supported file types: .png, .jpg, .jpeg and .gif
</span>
</label>
<div style="display:none">
<p id="loaded_n_total"></p>
<progress id="progressBar" class="progress" value="0" max="100" style="width:300px;"></progress></div>
</div>
</form>
<form id="upload_form" enctype="multipart/form-data" method="post">
<div class="file has-name is-fullwidth is-info">
<label class="file-label">
<input class="file-input" type="file" name="file1" id="file1" data-uploadValue="{{ item[0] }}" onchange="uploadFile(this)"><br>
<span class="file-cta">
<span class="file-icon">
<i class="fa fa-upload"></i>
</span>
<span class="file-label">
Choose a file…
</span>
</span>
<span class="file-name">
<div style="color:red;" id="status"></div>
Supported file types: .png, .jpg, .jpeg and .gif
</span>
</label>
<div style="display:none">
<p id="loaded_n_total"></p>
<progress id="progressBar" class="progress" value="0" max="100" style="width:300px;"></progress></div>
</div>
</form>
<form id="upload_form" enctype="multipart/form-data" method="post">
<div class="file has-name is-fullwidth is-info">
<label class="file-label">
<input class="file-input" type="file" name="file1" id="file1" data-uploadValue="{{ item[0] }}" onchange="uploadFile(this)"><br>
<span class="file-cta">
<span class="file-icon">
<i class="fa fa-upload"></i>
</span>
<span class="file-label">
Choose a file…
</span>
</span>
<span class="file-name">
<div style="color:red;" id="status"></div>
Supported file types: .png, .jpg, .jpeg and .gif
</span>
</label>
<div style="display:none">
<p id="loaded_n_total"></p>
<progress id="progressBar" class="progress" value="0" max="100" style="width:300px;"></progress></div>
</div>
</form>
<form id="upload_form" enctype="multipart/form-data" method="post">
<div class="file has-name is-fullwidth is-info">
<label class="file-label">
<input class="file-input" type="file" name="file1" id="file1" data-uploadValue="{{ item[0] }}" onchange="uploadFile(this)"><br>
<span class="file-cta">
<span class="file-icon">
<i class="fa fa-upload"></i>
</span>
<span class="file-label">
Choose a file…
</span>
</span>
<span class="file-name">
<div style="color:red;" id="status"></div>
Supported file types: .png, .jpg, .jpeg and .gif
</span>
</label>
<div style="display:none">
<p id="loaded_n_total"></p>
<progress id="progressBar" class="progress" value="0" max="100" style="width:300px;"></progress></div>
</div>
</form>
<form id="upload_form" enctype="multipart/form-data" method="post">
<div class="file has-name is-fullwidth is-info">
<label class="file-label">
<input class="file-input" type="file" name="file1" id="file1" data-uploadValue="{{ item[0] }}" onchange="uploadFile(this)"><br>
<span class="file-cta">
<span class="file-icon">
<i class="fa fa-upload"></i>
</span>
<span class="file-label">
Choose a file…
</span>
</span>
<span class="file-name">
<div style="color:red;" id="status"></div>
Supported file types: .png, .jpg, .jpeg and .gif
</span>
</label>
<div style="display:none">
<p id="loaded_n_total"></p>
<progress id="progressBar" class="progress" value="0" max="100" style="width:300px;"></progress></div>
</div>
</form>
<form id="upload_form" enctype="multipart/form-data" method="post">
<div class="file has-name is-fullwidth is-info">
<label class="file-label">
<input class="file-input" type="file" name="file1" id="file1" data-uploadValue="{{ item[0] }}" onchange="uploadFile(this)"><br>
<span class="file-cta">
<span class="file-icon">
<i class="fa fa-upload"></i>
</span>
<span class="file-label">
Choose a file…
</span>
</span>
<span class="file-name">
<div style="color:red;" id="status"></div>
Supported file types: .png, .jpg, .jpeg and .gif
</span>
</label>
<div style="display:none">
<p id="loaded_n_total"></p>
<progress id="progressBar" class="progress" value="0" max="100" style="width:300px;"></progress></div>
</div>
</form>
<form id="upload_form" enctype="multipart/form-data" method="post">
<div class="file has-name is-fullwidth is-info">
<label class="file-label">
<input class="file-input" type="file" name="file1" id="file1" data-uploadValue="{{ item[0] }}" onchange="uploadFile(this)"><br>
<span class="file-cta">
<span class="file-icon">
<i class="fa fa-upload"></i>
</span>
<span class="file-label">
Choose a file…
</span>
</span>
<span class="file-name">
<div style="color:red;" id="status"></div>
Supported file types: .png, .jpg, .jpeg and .gif
</span>
</label>
<div style="display:none">
<p id="loaded_n_total"></p>
<progress id="progressBar" class="progress" value="0" max="100" style="width:300px;"></progress></div>
</div>
</form>
<form id="upload_form" enctype="multipart/form-data" method="post">
<div class="file has-name is-fullwidth is-info">
<label class="file-label">
<input class="file-input" type="file" name="file1" id="file1" data-uploadValue="{{ item[0] }}" onchange="uploadFile(this)"><br>
<span class="file-cta">
<span class="file-icon">
<i class="fa fa-upload"></i>
</span>
<span class="file-label">
Choose a file…
</span>
</span>
<span class="file-name">
<div style="color:red;" id="status"></div>
Supported file types: .png, .jpg, .jpeg and .gif
</span>
</label>
<div style="display:none">
<p id="loaded_n_total"></p>
<progress id="progressBar" class="progress" value="0" max="100" style="width:300px;"></progress></div>
</div>
</form>
<form id="upload_form" enctype="multipart/form-data" method="post">
<div class="file has-name is-fullwidth is-info">
<label class="file-label">
<input class="file-input" type="file" name="file1" id="file1" data-uploadValue="{{ item[0] }}" onchange="uploadFile(this)"><br>
<span class="file-cta">
<span class="file-icon">
<i class="fa fa-upload"></i>
</span>
<span class="file-label">
Choose a file…
</span>
</span>
<span class="file-name">
<div style="color:red;" id="status"></div>
Supported file types: .png, .jpg, .jpeg and .gif
</span>
</label>
<div style="display:none">
<p id="loaded_n_total"></p>
<progress id="progressBar" class="progress" value="0" max="100" style="width:300px;"></progress></div>
</div>
</form>
<form id="upload_form" enctype="multipart/form-data" method="post">
<div class="file has-name is-fullwidth is-info">
<label class="file-label">
<input class="file-input" type="file" name="file1" id="file1" data-uploadValue="{{ item[0] }}" onchange="uploadFile(this)"><br>
<span class="file-cta">
<span class="file-icon">
<i class="fa fa-upload"></i>
</span>
<span class="file-label">
Choose a file…
</span>
</span>
<span class="file-name">
<div style="color:red;" id="status"></div>
Supported file types: .png, .jpg, .jpeg and .gif
</span>
</label>
<div style="display:none">
<p id="loaded_n_total"></p>
<progress id="progressBar" class="progress" value="0" max="100" style="width:300px;"></progress></div>
</div>
</form>
<form id="upload_form" enctype="multipart/form-data" method="post">
<div class="file has-name is-fullwidth is-info">
<label class="file-label">
<input class="file-input" type="file" name="file1" id="file1" data-uploadValue="{{ item[0] }}" onchange="uploadFile(this)"><br>
<span class="file-cta">
<span class="file-icon">
<i class="fa fa-upload"></i>
</span>
<span class="file-label">
Choose a file…
</span>
</span>
<span class="file-name">
<div style="color:red;" id="status"></div>
Supported file types: .png, .jpg, .jpeg and .gif
</span>
</label>
<div style="display:none">
<p id="loaded_n_total"></p>
<progress id="progressBar" class="progress" value="0" max="100" style="width:300px;"></progress></div>
</div>
</form>
<form id="upload_form" enctype="multipart/form-data" method="post">
<div class="file has-name is-fullwidth is-info">
<label class="file-label">
<input class="file-input" type="file" name="file1" id="file1" data-uploadValue="{{ item[0] }}" onchange="uploadFile(this)"><br>
<span class="file-cta">
<span class="file-icon">
<i class="fa fa-upload"></i>
</span>
<span class="file-label">
Choose a file…
</span>
</span>
<span class="file-name">
<div style="color:red;" id="status"></div>
Supported file types: .png, .jpg, .jpeg and .gif
</span>
</label>
<div style="display:none">
<p id="loaded_n_total"></p>
<progress id="progressBar" class="progress" value="0" max="100" style="width:300px;"></progress></div>
</div>
</form>
<form id="upload_form" enctype="multipart/form-data" method="post">
<div class="file has-name is-fullwidth is-info">
<label class="file-label">
<input class="file-input" type="file" name="file1" id="file1" data-uploadValue="{{ item[0] }}" onchange="uploadFile(this)"><br>
<span class="file-cta">
<span class="file-icon">
<i class="fa fa-upload"></i>
</span>
<span class="file-label">
Choose a file…
</span>
</span>
<span class="file-name">
<div style="color:red;" id="status"></div>
Supported file types: .png, .jpg, .jpeg and .gif
</span>
</label>
<div style="display:none">
<p id="loaded_n_total"></p>
<progress id="progressBar" class="progress" value="0" max="100" style="width:300px;"></progress></div>
</div>
</form>
<form id="upload_form" enctype="multipart/form-data" method="post">
<div class="file has-name is-fullwidth is-info">
<label class="file-label">
<input class="file-input" type="file" name="file1" id="file1" data-uploadValue="{{ item[0] }}" onchange="uploadFile(this)"><br>
<span class="file-cta">
<span class="file-icon">
<i class="fa fa-upload"></i>
</span>
<span class="file-label">
Choose a file…
</span>
</span>
<span class="file-name">
<div style="color:red;" id="status"></div>
Supported file types: .png, .jpg, .jpeg and .gif
</span>
</label>
<div style="display:none">
<p id="loaded_n_total"></p>
<progress id="progressBar" class="progress" value="0" max="100" style="width:300px;"></progress></div>
</div>
</form>
<form id="upload_form" enctype="multipart/form-data" method="post">
<div class="file has-name is-fullwidth is-info">
<label class="file-label">
<input class="file-input" type="file" name="file1" id="file1" data-uploadValue="{{ item[0] }}" onchange="uploadFile(this)"><br>
<span class="file-cta">
<span class="file-icon">
<i class="fa fa-upload"></i>
</span>
<span class="file-label">
Choose a file…
</span>
</span>
<span class="file-name">
<div style="color:red;" id="status"></div>
Supported file types: .png, .jpg, .jpeg and .gif
</span>
</label>
<div style="display:none">
<p id="loaded_n_total"></p>
<progress id="progressBar" class="progress" value="0" max="100" style="width:300px;"></progress></div>
</div>
</form>
运行它并检查表单,您会注意到它们获得了索引,他们的孩子也拥有id 属性。
第二步是确保您当前的代码查找、获取和使用父表单的id,以便正确选择其中元素的ids。为此,我首先获取正在使用的输入的父表单索引,然后使用闭包将此 index 传递给每个后续函数调用,因此 _() 始终选择正确的元素。
function _(el, index) {
return document.getElementById(el + '-' + index);
}
function uploadFile(element) {
var formId = element.closest('form').id,
index = formId.split('-')[formId.split('-').length - 1],
file = _("file1", index).files[0];
alert(file.name + " | " + file.size + " | " + file.type);
var formdata = new FormData();
formdata.append("file1", file);
var ajax = new XMLHttpRequest();
var uploadValue = element.getAttribute("data-uploadValue");
ajax.upload.addEventListener("progress",
(function(n) { progressHandler(event, n) })(index),
false);
ajax.addEventListener("load",
(function(n) { completeHandler(event, n) })(index),
false);
ajax.addEventListener("error",
(function(n) { errorHandler(event, n) })(index),
false);
ajax.addEventListener("abort",
(function(n) { abortHandler(event, n) })(index),
false);
ajax.open("POST", "/upload/" + uploadValue); //
ajax.send(formdata);
}
function progressHandler(event, index) {
_("loaded_n_total", index).innerHTML = "Uploaded " + event.loaded + " bytes of " + event.total;
var percent = event.total ? event.loaded * 100 / event.total : 0;
_("progressBar", index).value = Math.round(percent);
_("status", index).innerHTML = Math.round(percent) + "% uploaded... please wait";
}
function completeHandler(event, index) {
_("status", index).innerHTML = event.target.responseText;
_("progressBar", index).value = 0; //wil clear progress bar after successful upload
}
function errorHandler(event, index) {
_("status", index).innerHTML = "Upload Failed";
}
function abortHandler(event, index) {
_("status", index).innerHTML = "Upload Aborted";
}
旁注:我冒昧改变了
var percent = (event.loaded / event.total) * 100;
...进入:
var percent = event.total ? event.loaded * 100 / event.total : 0;
...,因为(可能是由于 SO 上不允许 POST),event.total 是 0 使 percent NaN,在下一行生成错误。如果您在实际示例中没有遇到此问题,请确保将此行改回适合您的行。
据我测试,它似乎可以工作,唯一的错误是 SO 不允许 POST 请求,一旦文件被选择并附加到表单。
如果您遇到任何麻烦,请告诉我,我会尽力弄清楚发生了什么。