自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

createObjectURL 這個 API 好用的離譜,我舉幾個場景你們就懂了

開發(fā) 前端
隨著我用 URL.createObjectURL 這個 API 越來越多次,越發(fā)感覺真的是一個很好用的方法,列舉一下我在項(xiàng)目中用到它的場景吧。

隨著我用 URL.createObjectURL 這個 API 越來越多次,越發(fā)感覺真的是一個很好用的方法,列舉一下我在項(xiàng)目中用到它的場景吧!

圖片預(yù)覽

以前我們想要預(yù)覽圖片,只能是上傳圖片到后端后,獲取到url然后賦予給img標(biāo)簽,才能得到回顯預(yù)覽,但是有了URL.createObjectURL就不需要這么麻煩了,直接可以在前端就達(dá)到預(yù)覽的效果~

<body>
  <input type="file" id="fileInput">
  <img id="preview" src="" alt="Preview">
  <script>
    const fileInput = document.getElementById('fileInput');
    fileInput.addEventListener('change', (event) => {
      const file = event.target.files[0];
      const fileUrl = URL.createObjectURL(file);
      const previewElement = document.getElementById('preview');
      previewElement.src = fileUrl;
    });
  </script>
</body>

音視頻流傳輸

舉個例子,我們通過MediaStream 去不斷推流,達(dá)到了視頻顯示的效果,有了URL.createObjectURL我們并不需要真的有一個url賦予video標(biāo)簽,去讓視頻顯示出來,只需要使用URL.createObjectURL去構(gòu)造一個臨時(shí)的url即可~非常方便~

<body>
  <video id="videoElement" autoplay playsinline></video>

  <script>
    const videoElement = document.getElementById('videoElement');

    navigator.mediaDevices.getUserMedia({ video: true, audio: true })
      .then((stream) => {
        videoElement.srcObject = stream;
      })
      .catch((error) => {
        console.error('Error accessing webcam:', error);
      });
  </script>

</body>

結(jié)合 Blob

URL.createObjectURL結(jié)合Blob也可以做很多方便開發(fā)的事情~

WebWorker

我們知道,想要用 WebWorker 的話,是要先創(chuàng)建一個文件,然后在里面寫代碼,然后去與這個文件運(yùn)行的代碼進(jìn)行通信,有了URL.createObjectURL就不需要新建文件了,比如下面這個解決excel耗時(shí)過長的場景,可以看到,我們傳給WebWorker的不是一個真的文件路徑,而是一個臨時(shí)的路徑~

const handleImport = (ev: Event) => {
  const file = (ev.target as HTMLInputElement).files![0];
  const worker = new Worker(
    URL.createObjectURL(
      new Blob([
        `
        importScripts('https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.16.4/xlsx.full.min.js');
        onmessage = function(e) {
          const fileData = e.data;
          const workbook = XLSX.read(fileData, { type: 'array' });
          const sheetName = workbook.SheetNames[0];
          const sheet = workbook.Sheets[sheetName];
          const data = XLSX.utils.sheet_to_json(sheet, { header: 1 });
          postMessage(data);
        };
        `,
      ]),
    ),
  );

  // 使用FileReader讀取文件內(nèi)容
  const reader = new FileReader();

  reader.onload = function (e: any) {
    const data = new Uint8Array(e.target.result);
    worker.postMessage(data);
  };

  // 讀取文件
  reader.readAsArrayBuffer(file);

  // 監(jiān)聽Web Worker返回的消息
  worker.onmessage = function (e) {
    console.log('解析完成', e.data);
    worker.terminate(); // 當(dāng)任務(wù)完成后,終止Web Worker
  };
};

下載文件

同樣也可以應(yīng)用在下載文件上,下載文件其實(shí)就是有一個url賦予到a標(biāo)簽上,然后點(diǎn)擊a標(biāo)簽就能下載了,我們也可以用URL.createObjectURL去生成一個臨時(shí)url

// 創(chuàng)建文件 Blob
const blob = new Blob([/* 文件數(shù)據(jù) */], { type: 'application/pdf' });

// 創(chuàng)建下載鏈接
const downloadUrl = URL.createObjectURL(blob);
const downloadLink = document.createElement('a');
downloadLink.href = downloadUrl;
downloadLink.download = 'document.pdf';
downloadLink.textContent = 'Download PDF';
document.body.appendChild(downloadLink);
責(zé)任編輯:趙寧寧 來源: 前端之神
相關(guān)推薦

2022-07-19 10:02:49

Vueuse文檔

2018-09-10 09:18:30

程序員領(lǐng)導(dǎo)加班

2025-04-17 04:22:00

Log插件日志管理

2020-05-15 12:23:38

人工智能AI

2018-03-05 11:29:17

云計(jì)算云服務(wù)服務(wù)器

2021-09-30 18:22:46

VSCode插件API

2022-07-08 10:09:47

SPLSQL數(shù)據(jù)庫

2013-08-26 09:31:47

技術(shù)面試

2022-07-27 22:59:53

Node.jsNest

2020-04-27 07:28:00

Java反射動態(tài)代理

2021-12-16 08:27:54

Vue3 插件Vue應(yīng)用

2021-10-08 15:55:52

Python模塊開發(fā)

2022-04-28 23:08:40

Windows 10微軟功能

2025-04-27 10:14:57

2014-08-22 13:27:59

物聯(lián)網(wǎng)

2014-11-13 09:21:23

TCP

2020-10-25 08:45:38

IPv6網(wǎng)絡(luò)協(xié)議網(wǎng)絡(luò)

2022-08-18 18:37:44

前端網(wǎng)絡(luò)開發(fā)

2023-12-01 08:39:29

分布式鎖系統(tǒng)

2021-06-08 06:46:48

設(shè)計(jì)模式創(chuàng)建型
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號