How to save the window.URL.createObjectURL() result for future use?

后端 未结 1 1925
长情又很酷
长情又很酷 2021-01-12 13:56

I\'m making an application in HTML5 where you choose a video file, and then the application plays it with the HTML5 video tag and the window.URL.createObj

相关标签:
1条回答
  • 2021-01-12 14:32

    I haven't used createObjectURL(), but if I understand correctly, it's essentially a temporary reference to a file or an in-memory object. If you want to save the actual video, it won't be useful, because the video itself will no longer be referenced by this pointer the next time the user visits the application.

    I think you might be able to do this with a data: URL instead, as that URL actually includes the full data from the file. This example demonstrates using a FileReader to generate a data URL. I think you should be able to do this:

    var reader = new FileReader();  
    reader.onload = function(e) { 
        var myDataUrl = e.target.result;
        // do something with the URL in the DOM,
        // then save it to local storage
    };  
    reader.readAsDataURL(file);
    

    Update: If you want to go up to 1GB, as you note in your comment, you'd probably be better served by the FileSystem API. This would require you to get the local file, save a copy of the file to persistent filesystem storage, and then use createObjectURL() to get a URL for the file copy. You still have a problem with disk space - you just added 1GB of duplicative file content to the user's filesystem - but I don't think it's possible to keep a persistent reference to a file outside of the browser sandbox otherwise.

    0 讨论(0)
提交回复
热议问题