gpt4 book ai didi

javascript - 文件名一直显示未定义

转载 作者:行者123 更新时间:2023-11-28 21:04:39 28 4
gpt4 key购买 nike

我尝试在用户上传文件后显示文件的名称。文件上传成功,但在 javascript 中它不断显示以下消息:

The file (undefined) was uploaded successfully!

当它应该显示文件名时,它却一直显示未定义。为什么是这样?目前发生的情况是,当用户上传文件时,文件被上传到“ImageFiles”文件夹中,当上传完成时,下面的 JavaScript 函数会显示消息以说明已成功上传的文件的名称。

下面是上传后显示文件名和消息的代码:

function stopImageUpload(success){

function handleFileSelect(evt) {
var files = evt.target.files;
localStorage["fname"] = files[0].name; //save the name for future use
}

$('.fileImage').bind('change', handleFileSelect, false);


var result = '';
if (success == 1){
result = '<span class="msg">The file ('+localStorage["fname"]+') was uploaded successfully!</span><br/><br/>';
localStorage["fname"] = undefined; //remove the temporary variable

}
else {
result = '<span class="emsg">There was an error during file upload!</span><br/><br/>';
}

return true;
}

以下是表格:

  <form action='imageupload.php' method='post' enctype='multipart/form-data' target='upload_target' onsubmit='stopImageUpload(this);' class='imageuploadform' >
<p>Image File: <input name='fileImage' type='file' class='fileImage' />
<input type='submit' name='submitImageBtn' class='sbtnimage' value='Upload' />
</p>
</form>

最佳答案

试试这个:

var fileName;
function stopImageUpload(success){

function handleFileSelect(evt) {
var files = evt.target.files;
fileName = files[0].name; //save the name for future use
}

$('.fileImage').bind('change', handleFileSelect, false);


var result = '';
if (success == 1){
result = '<span class="msg">The file ('+fileName+') was uploaded successfully!</span><br/><br/>';
fileName = ""; //remove the temporary variable

}
else {
result = '<span class="emsg">There was an error during file upload!</span><br/><br/>';
}

return true;
}

关于javascript - 文件名一直显示未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10174406/

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