我正在为 iOS 7.1 使用 IndexedDBShim polyfill(所以底层是 WebSQL),当我尝试运行它时,我得到:
"InvalidStateError: DOM Exception 11: An attempt was made to use an object that is not, or is no longer, usable."
编码:
//Create and open the database
var request = indexedDB.open( "Videos", 1.1 );
var database;
request.onerror = function (event)
{
console.log( "Unable to create storage for offline videos, an error occurred." );
console.dir( event );
};
//On success we'll grab the database and store or load the videos
request.onsuccess = function (event)
{
//Grab the database
database = request.result;
//Handle database error
database.onerror = function (event)
{
console.log( "Unable to access storage, an error occurred." );
console.dir( event );
};
downloadVideo();
}
//Create the database and object store
request.onupgradeneeded = function (event)
{
//Create the video object store (event.target.result is the database)
event.target.result.createObjectStore( "Videos" );
};
function downloadVideo()
{
var blob;
//Start the request
var videoRequest = new XMLHttpRequest();
//Get the Video file from the server.
videoRequest.open( "GET", "videos/test.mp4", true );
//It's a blob (for storing in database)
videoRequest.responseType = "blob";
//Listen for when it's done downloading the video data
videoRequest.addEventListener(
"load",
function ()
{
//We got it
if ( videoRequest.status === 200 )
{
//Get the data
blob = videoRequest.response;
//Start transaction for videos object store
var transaction = database.transaction( [ "Videos" ], "readwrite" );
//Store the video file
var putRequest = transaction.objectStore( "Videos" ).put( blob, "savedVideo" );
putRequest.onsuccess = function(e)
{
console.log( "succes!" );
console.dir(e);
}
}
//An error occurred
else console.log( null, "Unable to save, as an error occurred." );
},
false
);
//Start the request
videoRequest.send();
}