1

我有一个包含文件输入、上传按钮和取消按钮的表单:

更新:

    <form action='imageupload.php' method='post' enctype='multipart/form-data' target='upload_target' onsubmit='stopImageUpload(this);' class='imageuploadform' >
    <p class='imagef1_upload_form' align='center'><label>Image File: <input name='fileImage' type='file' class='fileImage' /></label>
    <input type='submit' name='submitImageBtn' class='sbtnimage' value='Upload' /></label>
    </p>
    <p class='imagef1_cancel' align='center'>
    <label><input type='button' name='imageCancel' class='imageCancel' value='Cancel' /></label>
    </p> 
<p><span class='list></span></p>
    <iframe class='upload_target' name='upload_target' src='#' style='width:0;height:0;border:0px;solid;#fff;'></iframe>
    </form>

现在我想要的是,如果图像已完成上传,在此表单的底部,我想要已上传文件的名称。这可能吗?比如我上传Images/file1.png,上传后文件名file1.png存放在底部,上传user/file2.jpg文件名file2.jpg存放在file1.png下面等等。

我已经上传了所有用 php 和 javascript 整理的文件。以下是上传完成后的javascript代码:

       $(document).ready(function(){
    $('.fileImage').bind('change', handleFileSelect, false);
});

function handleFileSelect(evt) {
    var files = evt.target.files;
    localStorage["fname"] = files[0].name; //save the name for future use
}
function stopImageUpload(success){
      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;   
}
4

1 回答 1

0

JavaScript 方式

如果您的目标是现代浏览器(不再是 IE6789),那么File API是您获取文件名的正确选择!它易于使用,并且代码中没有混乱。

查看我制作的演示:http: //jsfiddle.net/xUzcS
这是你想要的吗?

但是如果你想支持那个旧的IE6,那么对不起,你必须走AJAX的方式,或者使用VBscript。(不建议)

添加

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

document.getElementById('files').addEventListener('change', handleFileSelect, false);​

然后:

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

这可能对你有用,因为我不知道整个 html。

于 2012-04-16T02:17:15.473 回答