How to choose between `window.URL.createObjectURL()` and `window.webkitURL.createObjectURL()` based on browser

后端 未结 3 1481
失恋的感觉
失恋的感觉 2020-12-30 05:13

From the Firefox developer website, I know that Firefox uses

objectURL = window.URL.createObjectURL(file);

to get url of file type, but in

3条回答
  •  不知归路
    2020-12-30 06:04

    You could define a wrapper function:

    function createObjectURL ( file ) {
        if ( window.webkitURL ) {
            return window.webkitURL.createObjectURL( file );
        } else if ( window.URL && window.URL.createObjectURL ) {
            return window.URL.createObjectURL( file );
        } else {
            return null;
        }
    }
    

    And then:

    // works cross-browser
    var url = createObjectURL( file );
    

提交回复
热议问题