简体   繁体   English

在上传之前获取文件名

[英]Getting the name of a file before it is uploaded

I have a form where I am allowing users to upload files. 我有一个允许用户上传文件的表格。 Well, I am using a regular button that uses javascript to trigger the file uploader, to let users upload their photos. 好吧,我正在使用一个常规按钮,该按钮使用javascript触发文件上传器,以允许用户上传其照片。 But in doing this, I can no longer show users that they have a file selected (the text that is usually next to the "Select File" button). 但是这样做时,我无法再向用户显示他们已选择了文件(通常在“选择文件”按钮旁边的文本)。 I was wondering if there was a way to grab that value, say with javascript and display it for the user. 我想知道是否有一种方法可以获取该值,例如使用javascript并将其显示给用户。

JSFIDDLE: http://jsfiddle.net/YXgPf/1/ JSFIDDLE: http : //jsfiddle.net/YXgPf/1/

This is what I use to prompt the file upload button: 这是我用来提示文件上传按钮的方式:

 function getFile(){
  document.getElementById("upfile").click();
 }
 function sub(obj){
var file = obj.value;
var fileName = file.split("\\");
document.getElementById("photo-button").innerHTML = fileName[fileName.length-1];
document.myForm.submit();
event.preventDefault();
  }

Something like this will work: 这样的事情会起作用:

$("input[type=file]").on("change", function (e) {
  var file = e.originalEvent.target.files[0];
  file.name // This will be the file name.
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM