Implementation method of obtaining absolute path of uploaded file by js

  • 2021-07-07 06:21:59
  • OfStack

In html

  <input type="file" id="importFile" />
  <input type="button" onclick="upload()"/>

function upload() {
  var filename = document.getElementById("importFile").value;
  //  At this time filename No  importFile  The value in the box 

As in the above code, after selecting a file with the file upload dialog box, if the file "D:\\ temp\ file\ test. txt" is selected, alert (filename) is "C:\\ fakepath\ test. txt". Why? I want to get the original file path and file name. What should I do? Thank you first.

<input type="file" name="blogrollimage" onchange="setBlogrollImageName(this)" txlFillIn="isNotNull" title=" Link picture ">

<script type="text/javascript">
function setBlogrollImageName(basicfile){

Add onchange= "setBlogrollImageName (this)" when input, and then add this js to the page!

Thank you for your reply to doop 1120.

I did as you said, but the result was the same. If you select "D:\\ temp\ file\ test. txt", document. getElementById ("blogrollimagename"). value=basicfile. value;

The resulting value is still document. getElementById ("blogrollimagename"). value=C:\\ fakepath\ test. txt instead of D:\\ temp\ file\ test.txt

D:\\ temp\ file\ is due to a problem with the security settings of the ie browser

To resolve this problem:

Open the ie browser- > internet Option-- > Safe- > Custom level- > Others- > Enable Display File Directory Path when uploading files to the server

Re-run after confirmation

Related articles: