gpt4 book ai didi

javascript - 点击上传文件后上传图片!按钮未输入更改事件

转载 作者:行者123 更新时间:2023-11-28 20:14:49 24 4
gpt4 key购买 nike

我想在点击上传文件后上传多张图片!按钮未输入更改事件下面是我的 Jquery 代码(我从这里下载该代码 http://net.tutsplus.com/tutorials/javascript-ajax/uploading-files-with-ajax/ )

index.html

<div id="main">
<h1>Upload Your Images</h1>
<form method="post" enctype="multipart/form-data" action="upload.php">
<input type="file" name="images" id="images" multiple />
<button type="submit" id="btn">Upload Files!</button>
</form>
</div>

上传.js

(function () {
var input = document.getElementById("images"),
formdata = false;

function showUploadedItem (source) {
var list = document.getElementById("image-list"),
li = document.createElement("li"),
img = document.createElement("img");
img.src = source;
li.appendChild(img);
list.appendChild(li);
}

if (window.FormData) {
formdata = new FormData();
// document.getElementById("btn").style.display = "none";
}

button.addEventListener("click", function (evt) {
document.getElementById("response").innerHTML = "Uploading . . ."
var i = 0, len = this.files.length, img, reader, file;

for ( ; i < len; i++ ) {
file = this.files[i];

if (!!file.type.match(/image.*/)) {
if ( window.FileReader ) {
reader = new FileReader();
reader.onloadend = function (e) {
showUploadedItem(e.target.result, file.fileName);
};
reader.readAsDataURL(file);
}
if (formdata) {
formdata.append("images[]", file);
}
}
}

if (formdata) {
$.ajax({
url: "upload.php",
type: "POST",
data: formdata,
processData: false,
contentType: false,
success: function (res) {
document.getElementById("response").innerHTML = res;
}
});
}
}, false);
}());

最佳答案

index.html

<div id="main">
<h1>Upload Your Images</h1>
<form method="post" enctype="multipart/form-data" action="upload.php">
<input type="file" name="images" id="images" multiple />
<button type="submit" id="btn">Upload Files!</button>
</form>
</div>

上传.js

(function () {
var input = document.getElementById("images"),
formdata = false;

function showUploadedItem (source) {
var list = document.getElementById("image-list"),
li = document.createElement("li"),
img = document.createElement("img");
img.src = source;
li.appendChild(img);
list.appendChild(li);
}

if (window.FormData) {

formdata = new FormData();
formdata = new FormData();


//document.getElementById("btn").style.display = "none";
}

$("#btn").click(function (evt) {


evt.preventDefault();
document.getElementById("response").innerHTML = "Uploading . . ."
var i = 0, len = input.files.length, img, reader, file;

for ( ; i < len; i++ ) {
file = input.files[i];

if (!!file.type.match(/image.*/)) {
if ( window.FileReader ) {
reader = new FileReader();
reader.onloadend = function (e) {
showUploadedItem(e.target.result, file.fileName);
};
reader.readAsDataURL(file);
}
if (formdata) {
formdata.append("images[]", file);
}
}
}

if (formdata) {
$.ajax({
url: "upload.php",
type: "POST",
data: formdata,
processData: false,
contentType: false,
success: function (res) {
document.getElementById("response").innerHTML = res;
}
});
}
});
}());

关于javascript - 点击上传文件后上传图片!按钮未输入更改事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19347934/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com