我试图在用户上传文件后显示文件名。文件上传成功,但在 javascript 中它一直显示以下消息:
文件(未定义)上传成功!
当它应该显示文件名时,它一直显示未定义。为什么是这样?目前发生的情况是,当用户上传文件时,文件被上传到“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>