【问题标题】:trouble with downloading video file from azure blob storage using video file url使用视频文件 url 从 azure blob 存储下载视频文件时遇到问题
【发布时间】:2021-05-23 21:15:47
【问题描述】:

我正在尝试使用文件保护程序 npm 模块从 azure blob 存储下载视频文件。你可以看到我的代码here 我做了什么。我想在单击下载按钮时开始下载 blob 视频文件,但文件保护程序首先在内部下载文件,然后在下载后显示另存为窗口,此过程适用于小文件,但如果文件大小过大,对用户来说体验不佳.请建议使用视频 url 从 blob 下载视频文件。

import ReactDOM from "react-dom";
import { saveAs } from "file-saver";

import "./styles.css";

const onDownload = async () => {
  console.log("download start");
  let blob = "media-url"
      saveAs(blob, "video.mp4");
};

function App() {
  return (
    <div className="App">
      <button onClick={onDownload}>Download</button>
    </div>
  );
}

const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);

【问题讨论】:

  • 我的回答对你有用吗?
  • 视频已下载但无法播放,已损坏
  • 让我检查一下,请稍等。
  • 好的,用你自己的 blob 文件 url 检查它是否有效。

标签: javascript reactjs azure-blob-storage


【解决方案1】:

更新

vijay kasar 的评论非常正确,我会更新我的答案。

下载过程通过onDownloadProgress方法进行,下载完成后回调使用响应进行数据处理。 file-saver 中的 saveAs 函数看起来像一个内部下载操作。 It is better to support the callback method。有兴趣的也可以查看官方文档。因为不知道file-saver,所以推荐使用axios。

axios({
    url: 'MediaUrl',
    responseType:'blob',
    onDownloadProgress(progressEvent) {
        progress = Math.round((progressEvent.loaded / progressEvent.total) * 100);

        setPercentage(progress);

        documentStyles.setProperty('--progress', `${progress}%`);
    }
}).then(response => {
        setProgress('finished');
        const downloadUrl = window.URL.createObjectURL(new Blob([response.data]));
        const link = document.createElement('a');
        link.href = downloadUrl;
        link.setAttribute('download', 'video.mp4'); //any other extension
        document.body.appendChild(link);
        link.click();
        link.remove();
});

建议使用axios下载,使用onDownloadProgress增加下载用户体验。

相关博客:
How to Make a Download Progress Indicator in React

您可以根据博客中的示例代码download my modified test code参考。

测试效果图(.gif)和代码:

const download = () => {
    const documentStyles = document.documentElement.style;
    let progress = 0;

    setProgress('in-progress');

    axios({
        url: 'MediaUrl',
        onDownloadProgress(progressEvent) {
            progress = Math.round((progressEvent.loaded / progressEvent.total) * 100);

            setPercentage(progress);

            documentStyles.setProperty('--progress', `${progress}%`);
        }
    }).then(response => {
        setProgress('finished');
        const url = window.URL.createObjectURL(new Blob([response.data]));
        const link = document.createElement('a');
        link.href = url;
        link.setAttribute('download', 'video.mp4'); //or any other extension
        document.body.appendChild(link);
        link.click();
    });
};

【讨论】:

  • 在 axios 请求中添加响应类型(responseType:“blob”)后,此答案对我有用。
  • @vijaykasar 我已经更新了我的答案,mylatest answer解释了你问题的原因,因为我对react或javascript不是很熟悉,所以我只能给你一个大概的想法。
  • @vijaykasar 如果我的解决方案对您有启发或帮助,您能否将我的答案标记为accepted,谢谢~
猜你喜欢
  • 1970-01-01
  • 2018-05-29
  • 2011-01-23
  • 2015-05-20
  • 2023-03-22
  • 1970-01-01
  • 2023-04-05
  • 2021-12-02
  • 2011-11-29
相关资源
最近更新 更多