FakePath issue in Chrome browser

前端 未结 2 978
没有蜡笔的小新
没有蜡笔的小新 2020-12-20 06:38

I am making a browser based audio player. So for making a playlist from the local directory I am using :


         


        
2条回答
  •  猫巷女王i
    2020-12-20 07:00

    This is a security feature, by design. You should not be able to read the original file path of a file input into a browser form. File input is for reading file contents only, not metadata like path on the user's file system.

    The good news is that you don't need the original file path. You can use FileReader's readAsDataURL to convert the file contents into a base64-encoded data URL and use that as the audio src. To read from #myUploadInput and output through #myAudioElement (also available as a working fiddle):

    var reader = new FileReader();
    
    reader.onload = function (event) {
        document.getElementById("myAudioElement").src = event.target.result;
    };
    
    reader.readAsDataURL(document.getElementById("myUploadInput").files[0]);
    

提交回复
热议问题