html5ã®ãã¡ã¤ã«APIã«ã¤ãã¦èª¿ã¹ããhttp://www.w3.org/TR/FileAPI/ ãèªã¿ãªãããã¡ã¢ããã£ã±ãæ¬å®¶ããã¥ã¡ã³ãã¯é ¼ãã«ãªãã®ãã
FileAPIã¯ãªã«ã
- ãã¼ã«ã«ãã¡ã¤ã«ã«ã¢ã¯ã»ã¹ã§ããã
- ã¢ã¯ã»ã¹ã§ãããã¼ã«ã«ãã¡ã¤ã«ã¯input ã§æå®ãããç©ã ãã*1
使ãæ¹
input ã« files ã¿ã¤ããæå®ããã¨FileAPIã®å¯¾è±¡ã«ãªãã
<input type=files >
filesã¿ã¤ãã®èªã¿è¾¼ã¿æ¹
file = document.forms[0].elements[0].files[0]
ãã®ããã«ã<input type=files/>ã¸JSçµç±ã§ã¢ã¯ã»ã¹ã§ããã
input type=filesã®é ç®ã¯ãè¤æ°ãã¡ã¤ã«ã®é åã«ãªã£ã¦ãããFileListãªãã¸ã§ã¯ãå¼ã¶
FileList ãªãã¸ã§ã¯ã
é常ã®é åã¿ãããªãªãã¸ã§ã¯ããã¦å®ç¾©ããã¦ãããã¤ã¾ãé åã ã¨æã£ã¦ããã°è¯ãã
files = document.forms[0].elements[0].files file.length files[0] files.item(0)
FileãFileListã®ä¸ã«å ¥ã£ã¦ããªãã¸ã§ã¯ã
input type=filesã§ã¦ã¼ã¶ã¼ãæå®ãããã¡ã¤ã«ã®ãªãã¸ã§ã¯ããå
¥ã£ã¦ããã
File/FileListã¯ãIteratorãã¿ã¼ã³ã®é¢ä¿ã«ããã®
Fileãªãã¸ã§ã¯ãã§ã¨ããç©ã
- ãã¡ã¤ã«å
- ãµã¤ãº
- æçµæ´æ°æ¥
files = document.forms[0].elements[0].files file = files[0] file.name // => "test.xls" files.lastmodifideDate // => "" files.size // => 1234567
ãã ããå 容èªã¿è¾¼ã¿ã¯åºæ¥ãªãã
ãªããï¼
ãinput type=fileã§ããã©ã¦ã¶ããã¡ã¤ã«ãèªã¿è¾¼ãã§ããé ãã¸ã£ã³
ã ã£ããã©ãããã®ï¼
ããã¼ã«ã«ãã¡ã¤ã«ããéåæã§èªã¿è¾¼ã¿ã¾ãã
File.read ã¡ã½ããã£ã¦ç¡ããã©ï¼
ãªããã
File㯠FileReader()ãã¤ãã£ã¦éåæã«èªã¿ãã¿ã¾ãã File 㯠file://ã®URLã示ããªãã¸ã§ã¯ããªãã ã
éåæã§èªã¿è¾¼ãã£ã¦ï¼
XHRã¿ããã«èªã¿è¾¼ãã°è¯ããã ãã
new XmlHttp()ã®ãããã«ãnew FileReader()ãã
ä½åº¦ãè¨ããã©ãéåæã ãããï¼ï¼
reader = new FileReader() file = document.forms[0].elements[0].files[0] reader.readAsText(file, 'UTF-8')#ãã®ãã¨ã¯Eventå¦çã«ä»»ãããï¼
éåæãªãã ãããEventå¦çãæ¸ãã¨ããªãã¨ããããç¡ãã
reader = new FileReader() reader.onload = function(evt){} //èªã¿è¾¼ã¿å®äºãããå¼ã°ããã reader.onprogress = function(evt){} //ãã¡ã¤ã«èªã¿è¾¼ã¿ä¸ã«å¼ã°ããã reader.onerror = function(ev){} //ã¨ã©ã¼æã«å¼ã°ããã reader.readAsText(file, 'UTF-8')#Eventå¦çã«ä»»ãããï¼
ã¤ãã³ãã«ã¯ã©ããªç¨®é¡ãããã®ï¼
Filereaderãæã£ã¦ãã¤ãã³ãã¯ãããªã®ããã
ãã³ãã©å | ã¤ãã³ã | ã¿ã¤ãã³ã° |
---|---|---|
onloadstart | loadstart | ãã¼ã¿èªã¿è¾¼ã¿éå§æç¹ |
onprogress | progress | ãã¼ã¿ãèªã¿è¾¼ãã ã¿ã¤ãã³ã°ã§ |
onabort | abort | å¼·å¶åæ¢(abort()é¢æ°)ã§æ¢ããæç¹ |
onerror | error | ãªã¯ã¨ã¹ããã¨ã©ã¼ã ã£ãï¼404ã¨ãï¼ |
onload | load | ãªã¯ã¨ã¹ãæåæ |
onloadend | loadend | ãªã¯ã¨ã¹ãå®äºæï¼ã¨ã©ã¼ã§ãæåã§ãé¢ä¿ãªããéä¿¡ãå®äºããæç¹ã§å¼ã°ããï¼ |
ã¤ãã³ãã¯ä»¥ä¸ã§ãã
ãã ãã è¨ããã«ããã¡ã¤ã«èªãããè¯ããã ã
åãã¯ãã¡ã¤ã«ãèªã¿è¾¼ã¿ããã ããªãã ã
reader = new FileReader() file = document.forms[0].elements[0].files[0] reader.readAsText(file, 'UTF-8')#ãã®ãã¨ã¯Eventå¦çã«ä»»ãããï¼ ///ãã°ããå¾ ã¤ã reader.readyState //ãããï¼ã«ãªãã°èªã¿è¾¼ã¿ããã while(reader.readyState!=2){ } //ãã¡ã¤ã«ã®ä¸èº«ãè¦ã reader.result
readyStateã¨ãè¦è¦ããããã
ããXHRã¿ãããªãããªãã ãã
ã¤ã¾ã
æ¯è¼ãã¦ã¿ãã
ãã¼ã¿ | ãªãã¸ã§ã¯ã | |
Ajax | URLï¼ã¢ãã¬ã¹ã | Xmlhttp |
FileAPI | Fileãªãã¸ã§ã¯ã | FileReader |
対ã§è¦ããã¨ã¤ã¡ã¼ã¸ããããã¿ããã
ä½ã§éåæã«ãªã£ã¦ãã®ãã
ãã¡ã¤ã«èªã¿è¾¼ã¿ç¨åº¦ã§ãã»ãã®JSã®åä½ãã¹ããããããã¨ãã£ãããªããã ãããã«ãã¹ã¬ããã§åãããã«ãã¡ã¤ã«èªã¿è¾¼ã¿ãå¥ã¹ã¬ããã«ãã¦ããHTMLãèªã¿è¾¼ãã§DOMãæ§ç¯ããã¨ãã«IMGãCSSãéåæã«èªã¿è¾¼ãã§ããï¼ããã¨åããã¨ããã¼ã«ã«ãã¡ã¤ã«ã§ããã£ã¦ãã
ã£ã¦ãã¨ã¿ãã
ã·ã³ãã«ã«ãè¦ãããã
次ã®åçã¨ãã¦è¦ãããã¨ã«ããã
ãã¼ã«ã«ãã¡ã¤ã«ã¯file://ã¹ãã¼ããhttp://ã¹ãã¼ãã¨åãæ±ããåãã
絶対ã«åæã§ããªãã®ï¼
ãã¡ããåºæ¥ãã
XMLHTTPã«åæéä¿¡ãç¨æããã¦ããããã«ããã¡ã¤ã«ã«ãåæãç¨æããã¦ãããããã FileReaderSync
å ¨é¨éåæã§è¯ãã¸ã£ã³ï¼
ãããWebWorkersã§ããã°ã©ããã¹ã¬ããä½ãããããã¹ã¬ããåæå¦çã¯ãã£ãæ¹ãè¯ãã
é¨åçã«ãã¡ã¤ã«èªè¾¼ã§ããªãã®ï¼
HTTPã¯ãã¡ã¤ã«ã®é¨ååå¾ãåºæ¥ããFileAPIã¯åºæ¥ãªãã®ï¼
ãã¡ããåºæ¥ããFile#sliceãããã
ã¾ã¨ã
HTTPã¨ããªãæ±ãã®FILEã¨ãã¦å®ç¾©ããã¦ããã
åæã»éåæãé¨åèªã¿è¾¼ã¿ãããã®ã§ãããã£ã±HTTPã¨åãã ãããFileã ããã¨ãã£ã¦ããææ³ã¯ä½ãå¤ãããªãããªãã¸ã§ã¯ããå¤ããã ãã
ã¨ããã§ãèªã¿è¾¼ãã ãã¡ã¤ã«ãã©ã使ãã®ï¼
ç»åãã¤ã³ã©ã¤ã³ã«åãè¾¼ãè¨è¿°ãè¦ãã¦ãã¾ããï¼
<img src="data:123456yjgbvdâ/>
ãã¡ã¤ã«APIã§èªã¿è¾¼ãã ãã¡ã¤ã«å
容ã¯ã
<img src=âblob:123456yjgbvdâ/>
ã¨æ¸ãè¾¼ã¿ã使ããã¨ãåºæ¥ãã
data: ã 㨠base64ã¨ã³ã³ã¼ããå¿ è¦ã ããããã©ããããã©ãblogãªããã¤ããªãã®ã¾ã¾ã§æ¥½ã¡ãã
ãããblob: ãããã³ã«ã¨å¼ã³ã¾ãã
è¬éã¯è¯ãããã使ãæ¹ã
説æå¤ãã¦ããã¾ãããããããã¾ãã
var file = document.getElementById('file').files[0]; if(file){ blobURLref = window.URL.createObjectURL(file); myimg.src = blobURLref; }
ï¼ï¼window.URL.createObjectURL() ã«ãã¡ã¤ã«ã渡ã㨠blob:xxxxxã®URLã帰ã£ã¦ãã¾ãã
ï¼ï¼blob:ã®URLãimg.srcã«åãè¾¼ãã°çµããã
ç°¡åã§ããã
追è¨ï¼window.URLã«ã¤ã㦠2012-08-06
chrome/safariã® Webkitã§ã¯ãwindow.URLããã³ãã¼ãã¬ãã£ãã¯ã¹ä»ãã§ãªãã¨ã¢ã¯ã»ã¹åºæ¥ãªãå ´åãããã¾ãã
window.webkitURL
ãªã©ã¨ãã³ãã¼ãã¬ãã£ãã¯ã¹ webkitãã¤ãã¦ãã ãããªã
è¿½è¨ blob URLçæã¯ãã£ã¨ç°¡åãªæ¹æ³ãããã¾ããã
var file = document.getElementById('file').files[0]; var reader = new FileReader(); reader.readAsDataURL(file) //æ«ãå¾ ã¤ while(reader.readyState!=2){ } img.src = reader.result ;
追è¨ã®è¿½è¨ãAndroid4.0ã§ã¯ readAsDataURLããã¾ãåä½ããªãã£ããwindow.URL.createObjectURL(file)ã使ãã®ãç¡é£
追è¨ãBLOBã«ã¤ãã¦ï¼2012/08/06
æå³çã«Blobãå¤ãã¦ããã©ããã£ã±ãæ¸ããæ¹ãããã¨ãããã®ã§è¿½è¨
FileAPI㯠file://ã¿ãããªããã ã¨è¨ãã¾ããããããã®å ã«ããã®ãhttpããã¼ã«ã«ãã¡ã¤ã«ã«ããã®ãfileãã§ã¯HTMLä¸ãã¨ãã°Canvasãªã©ã«å ¥ã£ã¦ããã¼ã¿ã¯ä½ã«ãªãã®ãï¼ãããblobã§ãã canvsãªã©ã®ã¡ã¢ãªã«è¼ã£ã¦ããã¼ã¿ã¯BLOBã§ããèªã¿è¾¼ãã ãã¼ã¿ãHTMLä¸ã®imgã¿ã°ãªã©ã«åå¨ããã®ã§Blobã§ãã
ãããã³ã« | æ¸å¼ | |
HTTP(s) | http:// | |
File | file:// | |
Blob | blob:// |
ã¿ãããªããã§ãã
blobã¯ãèªã¿è¾¼ãã ãã¼ã¿ãã®ãã®ã表ãã
File㯠Blobã®æ¡å¼µã§å®ç¾©ããã¦ãããªããï¼ãã¡ã¤ã«ãèªã¿è¾¼ãã ãããã¤ããªãåºã¦ããããã«ä»ããªããªãã
åºã¦ãããã¤ããªã¯ãã®ã¾ã¾Canvasã«å ¥ãããªããImageã«ããããªããVideoã§åçãããªã©ã«ãªãã
*1:ã¤ãã³ããããåãããã©ãã¾ãã¯input ããæããã¨ããã