用于从or对象URL.createObjectURL
生成blob:
-URI :File
Blob
基本演示:http: //jsfiddle.net/HGXDT/
<input type="file" id="file"><img id="preview">
window.URL = window.URL || window.webkitURL || window.mozURL;
document.getElementById('file').onchange = function() {
var url = URL.createObjectURL(this.files[0]);
console.log(url);
document.getElementById('preview').src = url;
};
检查脚本是否受同源策略影响的代码(答案:没有)。(实际上,页面本身不受影响,因为它创建了blob:
-URI,但其他页面无法blob:
在画布上绘制URI并使用它):http:
//jsfiddle.net/HGXDT/1/
<input type="file" id="file">
<img id="preview">
<canvas id="canvas"></canvas>
window.URL = window.URL || window.webkitURL || window.mozURL;
document.getElementById('file').onchange = function() {
var url = URL.createObjectURL(this.files[0]);
console.log(url);
var img = document.getElementById('preview');
canvasSOPTest(img, url);
};
// See console. SOP error has to show up
canvasSOPTest(new Image(), 'http://stackoverflow.com/favicon.ico?'+new Date());
function canvasSOPTest(img, url) {
// Same Origin Policy check
img.onload = function() {
var canvas = document.getElementById('canvas');
var ctx = canvas.getContext('2d');
console.log('Painting image...');
ctx.drawImage(img, 0, 0);
console.log('Attempting to get image data');
try {
ctx.getImageData(0, 0, canvas.width, canvas.height);
console.log('Success! No errors');
} catch (e) {
console.log(e);
}
};
img.src = url;
}