最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - FakePath issue in Chrome browser - Stack Overflow

programmeradmin6浏览0评论

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

<input type="file" id="getFile" />

Then I am using a button to confirm the playlist.On clicking the button I am calling a javascript function to change the src of the audio tag to play the new audio file selected in the playlist. I want the exact path of the file from the input file to run in the HTML5 audio player but it starts taking the path as C://Fakepath/filename.mp3. Can someone help me with this.

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

<input type="file" id="getFile" />

Then I am using a button to confirm the playlist.On clicking the button I am calling a javascript function to change the src of the audio tag to play the new audio file selected in the playlist. I want the exact path of the file from the input file to run in the HTML5 audio player but it starts taking the path as C://Fakepath/filename.mp3. Can someone help me with this.

Share Improve this question edited Aug 15, 2013 at 19:19 putvande 15.2k3 gold badges36 silver badges51 bronze badges asked Aug 15, 2013 at 13:48 user1580096user1580096 4871 gold badge7 silver badges17 bronze badges 5
  • 1 Bad news – Chris Forrence Commented Aug 15, 2013 at 13:50
  • 1 A website has no business knowing my directory layout, thankyouverymuch. – millimoose Commented Aug 15, 2013 at 13:51
  • So will I be able to play the selected file in the browser somehow or through any other alternative as this is a local application. – user1580096 Commented Aug 15, 2013 at 13:52
  • 1 @user1580096 Sure, use FileReader's toDataURL to convert the file contents into a base64-encoded data URL and use that as the audio src. – apsillers Commented Aug 15, 2013 at 13:54
  • 1 @apsillers, post answers in the answer box, please :-) – Brigand Commented Aug 15, 2013 at 13:56
Add a ment  | 

2 Answers 2

Reset to default 16

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]);

if the user is 'building' / creating the playlist based on files they have locally you could do a 'browse' field (s) where they select the local audio files, then take the contents of the field (that Should include the paths to those images), build an array of the count/id, filename.mp3, and path... then, based on what is 'chosen' to play, just reassemble the full local path and play that file.

that would be an approach I would take anyway to see if it would work. the necessary piece here is getting the user to disclose the paths to the audio files... but Im still not 100% sure it would work given the security feature that the earlier menter posted a link to.

if this were included in an application the user approved for local installation you could just refer to it using the 'application directory' and copy the file to that 'safe location' but since its web based it just really opens up a whole can of worms in terms of a potentially unapproved / authorized web function knowing your local directory structure. good luck, let me know if you find a solution.

发布评论

评论列表(0)

  1. 暂无评论