How to resolve the C:\fakepath?

后端 未结 13 1722
梦如初夏
梦如初夏 2020-11-21 13:34

This is my upload button.



        
相关标签:
13条回答
  • 2020-11-21 13:47

    Why don't you just use the target.files?

    (I'm using React JS on this example)

    const onChange = (event) => {
      const value = event.target.value;
    
      // this will return C:\fakepath\somefile.ext
      console.log(value);
    
      const files = event.target.files;
    
      //this will return an ARRAY of File object
      console.log(files);
    }
    
    return (
     <input type="file" onChange={onChange} />
    )
    

    The File object I'm talking above looks like this:

    {
      fullName: "C:\Users\myname\Downloads\somefile.ext"
      lastModified: 1593086858659
      lastModifiedDate: (the date)
      name: "somefile.ext"
      size: 10235546
      type: ""
      webkitRelativePath: ""
    }
    

    So then you can just get the fullName if you wanna get the path.

    0 讨论(0)
  • 2020-11-21 13:49

    Some browsers have a security feature that prevents JavaScript from knowing your file's local full path. It makes sense - as a client, you don't want the server to know your local machine's filesystem. It would be nice if all browsers did this.

    0 讨论(0)
  • 2020-11-21 13:52

    If you really need to send the full path of the uploded file, then you'd probably have to use something like a signed java applet as there isn't any way to get this information if the browser doesn't send it.

    0 讨论(0)
  • 2020-11-21 13:52

    You would be able to get at least temporary created copy of the file path on your machine. The only condition here is your input element should be within a form What you have to do else is putting in the form an attribute enctype, e.g.:

    <form id="formid" enctype="multipart/form-data" method="post" action="{{url('/add_a_note' )}}">...</form>
    

    you can find the path string at the bottom. It opens stream to file and then deletes it.

    0 讨论(0)
  • 2020-11-21 13:53

    I came accross the same problem. In IE8 it could be worked-around by creating a hidden input after the file input control. The fill this with the value of it's previous sibling. In IE9 this has been fixed aswell.

    My reason in wanting to get to know the full path was to create an javascript image preview before uploading. Now I have to upload the file to create a preview of the selected image.

    0 讨论(0)
  • 2020-11-21 13:55

    I use the object FileReader on the input onchange event for your input file type! This example uses the readAsDataURL function and for that reason you should have an tag. The FileReader object also has readAsBinaryString to get the binary data, which can later be used to create the same file on your server

    Example:

    var input = document.getElementById("inputFile");
    var fReader = new FileReader();
    fReader.readAsDataURL(input.files[0]);
    fReader.onloadend = function(event){
        var img = document.getElementById("yourImgTag");
        img.src = event.target.result;
    }
    
    0 讨论(0)
提交回复
热议问题