美文网首页
前端根据 url 读取 srt、vtt、txt 等文件内容。

前端根据 url 读取 srt、vtt、txt 等文件内容。

作者: magic_pill | 来源:发表于2022-01-14 20:13 被阅读0次

前端根据 url 读取 srt、vtt、txt 等文件内容。

/**
 * 根据文件url获取文件内容
 */
export const getContentFromFileUrl = (
  url: string, 
  callback: (val: string) => void
) => {
  const xhr = new XMLHttpRequest();
  xhr.open('get', url, true);
  xhr.responseType = 'blob';
  xhr.onload = () => {
    if (xhr.status === 200) {
      const reader = new FileReader();
      reader.readAsText(xhr.response);
      reader.onload = () => {
        callback && callback(reader.result as string);
      };
    }
  };
  xhr.send();
};

相关文章

网友评论

      本文标题:前端根据 url 读取 srt、vtt、txt 等文件内容。

      本文链接:https://www.haomeiwen.com/subject/nxkzcrtx.html