简体   繁体   English

Chrome浏览器中的FakePath问题

[英]FakePath issue in Chrome browser

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. 然后我使用一个按钮来确认播放列表。单击该按钮时,我调用一个javascript函数来更改音频标签的src,以播放在播放列表中选择的新音频文件。 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. 我希望输入文件中的文件的确切路径在HTML5音频播放器中运行,但它开始将路径作为C://Fakepath/filename.mp3。 Can someone help me with this. 有人可以帮我弄这个吗。

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 . 您可以使用FileReaderreadAsDataURL将文件内容转换为base64编码的数据URL并将其用作音频src To read from #myUploadInput and output through #myAudioElement ( also available as a working fiddle ): 要读取#myUploadInput并通过#myAudioElement输出( 也可以用作工作提琴 ):

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. 如果用户根据本地文件在“构建” /创建播放列表,则可以在“浏览”字段中选择本地音频文件,然后获取字段内容(应包含指向以下位置的路径)这些图像),构建一个由count / id,filename.mp3和path组成的数组...然后,根据要播放的内容,重新组合完整的本地路径并播放该文件。

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 commenter posted a link to. 此处的必要步骤是使用户公开音频文件的路径...但是考虑到早期评论者发布的链接的安全功能,我仍然不能100%地确定它会起作用。

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. 如果该文件包含在用户批准用于本地安装的应用程序中,则可以使用“应用程序目录”引用该文件并将其复制到该“安全位置”,但是由于基于Web的它确实打开了一堆蠕虫知道您的本地目录结构的潜在未经批准/授权的网络功能。 good luck, let me know if you find a solution. 祝您好运,如果您找到解决办法,请告诉我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM