get the filename of a fileupload in a document thr

2019-01-09 03:06发布

var fu1 = document.getElementById("FileUpload1");

how can i get the filename of fileupload control with id FileUpload1

7条回答
相关推荐>>
2楼-- · 2019-01-09 03:31

RaYell, You don't need to parse the value returned. document.getElementById("FileUpload1").value returns only the file name with extension. This was useful for me because I wanted to copy the name of the file to be uploaded to an input box called 'title'. In my application, the uploaded file is renamed to the index generated by the backend database and the title is stored in the database.

查看更多
SAY GOODBYE
3楼-- · 2019-01-09 03:44

Using code like this in a form I can capture the original source upload filename, copy it to a second simple input field. This is so user can provide an alternate upload filename in submit request since the file upload filename is immutable.

    <input type="file" id="imgup1" name="imagefile">
      onchange="document.getElementsByName('imgfn1')[0].value = document.getElementById('imgup1').value;">
    <input type="text" name="imgfn1" value="">
查看更多
ら.Afraid
4楼-- · 2019-01-09 03:49

Try the value property, like this:

var fu1 = document.getElementById("FileUpload1");
alert("You selected " + fu1.value);

NOTE: It looks like FileUpload1 is an ASP.Net server-side FileUpload control.
If so, you should get its ID using the ClientID property, like this:

var fu1 = document.getElementById("<%= FileUpload1.ClientID %>");
查看更多
何必那么认真
5楼-- · 2019-01-09 03:49

Try document.getElementById("FileUpload1").value this value should have a path for a file to be uploaded, just strip all dirs from that value and you will have file name.

查看更多
劳资没心,怎么记你
6楼-- · 2019-01-09 03:51

To get only uploaded file Name use this,

fake_path=document.getElementById('FileUpload1').value
alert(fake_path.split("\\").pop())

FileUpload1 value contains fake path, that you probably don't want, to avoid that use split and pop last element from your file.

查看更多
smile是对你的礼貌
7楼-- · 2019-01-09 03:54

In google chrome element.value return the name + the path, but a fake path. Thus, for my case I used the name attribute on the file like below :

function getFileData(myFile){
   var file = myFile.files[0];  
   var filename = file.name;
}

this is the call from the page :

<input id="ph1" name="photo" type="file" class="jq_req" onchange="getFileData(this);"/>
查看更多
登录 后发表回答