URL.createObjectURL()

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since January 2020.

實驗性質: 這是一個實驗中的功能
此功能在某些瀏覽器尚在開發中,請參考兼容表格以得到不同瀏覽器用的前輟。

摘要

靜態方法 URL.createObjectURL() 用於建立一個帶有 URL 的 DOMString 以代表參數中所傳入的物件. 該 URL 的生命週期與創造它的 window 中的 document一致. 這個新的物件 URL 代表了所指定的 File 物件 或是 Blob 物件。

備註: 此功能可在 Web Worker 中使用。

語法

objectURL = URL.createObjectURL(blob);

參數

blob

一個用以建立物件 URL 的 File 物件 或是 Blob 物件.

範例

參見 Using object URLs to display images.(藉由物件 URL 來顯示圖像)

注意事項

每次呼叫 createObjectURL() 都會產生一個新的 URL, 不論是否曾以同一物件產生過. 當你不再需要它們的時候必須對每一個都呼叫 URL.revokeObjectURL() 來釋放它們. 瀏覽器會在 document 被 unload 時自動釋放它們; 然而, 為了最佳化效能與記憶體用量, 當有安全的時機請務必手動釋放它們.

規範文件

Specification
File API
# dfn-createObjectURL

瀏覽器相容性

Report problems with this compatibility data on GitHub
desktopmobileserver
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
Deno
Node.js
createObjectURL() static method

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support
See implementation notes.

另見