Created
May 4, 2017 02:02
-
-
Save AlexanderOMara/459f9938d79a8d9739c1aa1553ee677b to your computer and use it in GitHub Desktop.
Firefox IDBMutableFile Download Test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8" /> | |
<title>Firefox IDBMutableFile Download Test</title> | |
<style> | |
a { | |
text-decoration: none; | |
} | |
</style> | |
</head> | |
<body> | |
<h1>Firefox IDBMutableFile Download Test</h1> | |
<p>Click the different links to test the behavior. Those created with just the <code>File</code> constructor will behave as expected. Those created from an <code>IDBMutableFile</code> will fail, except those which use the URL during the success handler.</p> | |
<p>Most simply fail silently, but set by JavaScript <code>location</code> will throw the following error in the console.</p> | |
<blockquote><code>FileHandleInactiveError: A request was placed against a file handle which is currently not active, or which is finished.</code></blockquote> | |
<p>In some versions of Firefox newer than 53 it appears to be possible to right-click and open the link in new tabs and get the file.</p> | |
<ul id="list"></ul> | |
<script> | |
(function() { | |
'use strict'; | |
var listEl = document.getElementById('list'); | |
function addListEntry(title, href, download, target) { | |
var li = document.createElement('li'); | |
var a = document.createElement('a'); | |
a.textContent = title; | |
if (typeof href === 'function') { | |
a.href = '#'; | |
a.addEventListener('click', href); | |
} | |
else { | |
a.href = href; | |
} | |
if (download !== null && download !== undefined) { | |
a.setAttribute('download', download); | |
} | |
if (target !== null && target !== undefined) { | |
a.setAttribute('target', target); | |
} | |
li.appendChild(a); | |
listEl.appendChild(li); | |
} | |
function examples(prefix, file) { | |
var fileURL = URL.createObjectURL(file); | |
addListEntry(prefix + ': link', fileURL); | |
addListEntry(prefix + ': target _blank', fileURL, null, '_blank'); | |
addListEntry(prefix + ': download', fileURL, ''); | |
addListEntry(prefix + ': download named', fileURL, 'test-named.txt'); | |
addListEntry(prefix + ': javascript location', function(e) { | |
e.preventDefault(); | |
window.location = fileURL; | |
}); | |
addListEntry(prefix + ': javascript open', function(e) { | |
e.preventDefault(); | |
window.open(fileURL); | |
}); | |
} | |
examples('File', new File(['testing123'], 'test.txt')); | |
var IDBReq = indexedDB.open('testdb', 1); | |
IDBReq.onsuccess = function() { | |
var DB = this.result; | |
var buildHandle = DB.createMutableFile('test.txt', 'text/plain'); | |
buildHandle.onsuccess = function() { | |
var myFileHandle = this.result; | |
var myFile = myFileHandle.open('readwrite'); | |
var writing = myFile.append('testing123'); | |
writing.onsuccess = function() { | |
var theFile = myFileHandle.getFile(); | |
theFile.onsuccess = function() { | |
var file = this.result; | |
examples('IDBMutableFile', file); | |
addListEntry('IDBMutableFile: javascript location on success', function(e) { | |
e.preventDefault(); | |
var theFile = myFileHandle.getFile(); | |
theFile.onsuccess = function() { | |
var file = this.result; | |
var fileURL = URL.createObjectURL(file); | |
window.location = fileURL; | |
}; | |
}); | |
examples('IDBMutableFile->File', new File([file], 'test.txt')); | |
addListEntry('IDBMutableFile->File: javascript location on success', function(e) { | |
e.preventDefault(); | |
var theFile = myFileHandle.getFile(); | |
theFile.onsuccess = function() { | |
var file = this.result; | |
var file2 = new File([file], 'test.txt'); | |
var fileURL = URL.createObjectURL(file2); | |
window.location = fileURL; | |
}; | |
}); | |
}; | |
}; | |
}; | |
}; | |
})(); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment