javascript - 未捕获的类型错误 : Failed to execute 'createObjectURL' on 'URL' : No function was found that matched the signature provided

标签 javascript indexeddb html5-appcache

这是下载文件并持久化到数据库的js代码:

 <script type="text/javascript"> 
      (function () {        
 
 window.indexedDB = window.indexedDB || window.webkitIndexedDB ||
                    window.mozIndexedDB || window.OIndexedDB || 
                    window.msIndexedDB;
                      
 var IDBTransaction = window.IDBTransaction || 
                      window.webkitIDBTransaction || 
                      window.OIDBTransaction || 
                      window.msIDBTransaction;
                        
 var dbVersion = 1.0; 
 var indexedDB = window.indexedDB;
 var dlStatusText = document.getElementById("fetchstatus");

 // Create/open database
 var request = indexedDB.open("Syafunda_Videos", dbVersion),
    db,
    createObjectStore = function (dataBase) {
        dataBase.createObjectStore("Videos",{ keyPath: "id", autoIncrement:true });
    },    

    getVideoFile = function () {
       var xhr = new XMLHttpRequest(),
       blob;
       // Get the Video file from the server.
       xhr.open("GET", "<?php echo $name ?>", true);     
       xhr.responseType = "blob";
       xhr.addEventListener("load", function () {
          if (xhr.status === 200) {
              blob = xhr.response;
              addVideoInDb(blob);
              dlStatusText.innerHTML = "DOWNLOAD COMPLETE: Video file downloaded.";
          }
          else {
              dlStatusText.innerHTML = "ERROR: Unable to download video.";
          }
        }, false);
        xhr.send();
    },

    addVideoInDb = function (blob) {
       var transaction = db.transaction(["Videos"], "readwrite");
       var add = transaction.objectStore("Videos").put(blob);
       //console.log(objectStore.autoIncrement);
  
    };


  request.onerror = function (event) {
      console.log("Error creating/accessing IndexedDB database");
  };

  request.onsuccess = function (event) {
      console.log("Success creating/accessing IndexedDB database");
      db = request.result;

      db.onerror = function (event) {
          console.log("Error creating/accessing IndexedDB database");
      };
       
      getVideoFile();
      
  }
   
  // For future use. Currently only in latest Firefox versions
  request.onupgradeneeded = function (event) {
      createObjectStore(event.target.result);
  };
    
})();</script>

我正在尝试从 indexedDB 中检索文件。我一直在控制台中收到该错误:这是用于从数据库中检索文件的 js 代码,这是我收到错误的地方:

Uncaught TypeError: Failed to execute 'createObjectURL' on 'URL': No function was found that matched the signature provided. at IDBRequest.transaction.objectStore.get.onsuccess)

我哪里错了?这是我的 JS 代码片段。一些指示会很棒:

<script type="text/javascript"> 

      (function () {
    // IndexedDB
    window.indexedDB = window.indexedDB || window.webkitIndexedDB || 
                       window.mozIndexedDB || window.OIndexedDB || 
                       window.msIndexedDB,
    IDBTransaction = window.IDBTransaction || 
                     window.webkitIDBTransaction ||
                     window.OIDBTransaction || window.msIDBTransaction,
    dbVersion = 1.0;
 
    var indexedDB = window.indexedDB;
 
    // Create/open database
    var request = indexedDB.open("Syafunda_Videos");
     
    request.onerror = function (event) {
        // Failed to Open the indexedDB database
    };
 
    request.onsuccess = function (event) {
        db = request.result;
         
        // Open a transaction to the database
        var transaction = db.transaction(["Videos"], "readwrite");
 
        //Retrieve the video file
        transaction.objectStore("Videos").get("2").onsuccess = 
        function (event) {        
          
        var videoFile = event.target.result;
        var URL = window.URL || window.webkitURL;
        var videoURL = URL.createObjectURL(videoFile);
      
       
        // Set video src to ObjectURL        
        var videoElement = document.getElementById("Video");
        videoElement.setAttribute("src", videoURL);
 
       var mimeDisplayElement = document.getElementById("vidMimeDisplay");
           mimeDisplayElement.innerHTML = videoFile.type;
        };
    }
})();

</script>

最佳答案

在获取视频时,我将 get("2") 更改为 get(2) ,就像这样

//Retrieve the video file
        transaction.objectStore("Videos").get(2).onsuccess = 
        function (event) {  //code...}     

关于javascript - 未捕获的类型错误 : Failed to execute 'createObjectURL' on 'URL' : No function was found that matched the signature provided,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53865028/

相关文章:

javascript - 在 IndexedDB 和 AngularJS 上查询 MomentJS 日期 - 日期与字符串

indexeddb - 打开高版本数据库时的DOMException

javascript - 当元素由脚本动态生成并通过类选择器访问时,如何在 Jquery 中的单击事件上调用函数?

.net - .NET 的 Markdown ? SO 如何即时渲染它?

javascript - 如何为网页创建配色方案生成器

html - 永久删除 HTML5 Appcache list

javascript - 防止appcache缓存当前页面

javascript - 如何从网站获取javascript动态内容

javascript - IndexedDB IDBObjectStore.add 错误 : "a generated key could not be inserted into the value"

html5-appcache - HTML5 缓存 list : Fallback for external resources