[英]Save uploaded file for later use
I'm building a music player of sorts. 我正在建立各种音乐播放器。 In this program, users can click a button to add a song to their playlist from their filesystem, based on a file input.
在此程序中,用户可以单击按钮,根据文件输入将歌曲从文件系统添加到其播放列表。 It works fine if I just want to use a single song - the user clicks the "add song" button, and I can get the data from that to play a song.
如果我只想使用一首歌曲,效果很好-用户单击“添加歌曲”按钮,然后我就可以从中获取数据来播放歌曲。 The problem comes when the user reloads the page.
当用户重新加载页面时出现问题。 What I want to happen is when the user reloads the page, I can just resume the very same song.
我想发生的是,当用户重新加载页面时,我可以恢复同一首歌曲。 Here's some code.
这是一些代码。
var song = manageAddSong.files[0];
I need that song variable to be persistent. 我需要该歌曲变量才能持久。 Can I use localSotrage for this?
我可以为此使用localSotrage吗?
localStorage.setItem("persistentSong",song);
EDIT: I am also very likely going to use a JSON array to store the song data. 编辑:我也很可能会使用JSON数组来存储歌曲数据。 Can that also be done?
还能做到吗?
Yes, you can use localStorage
. 是的,您可以使用
localStorage
。 However, localStorage
can only store strings, so you'll need to convert the object to a JSON string: 但是,
localStorage
只能存储字符串,因此您需要将对象转换为JSON字符串:
localStorage.setItem("persistentSong", JSON.stringify(song));
And to retrieve it later: 并在以后检索:
var song = JSON.parse(localStorage.getItem("persistentSong"));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.