2013-02-13 343 views
1

我有一个api调用,发送给我一个文件流。我得到流并将其作为uInt8Array放出。使用uInt8Array响应 - 文件下载 - Javascript

林这个HTML5例如:

http://www.html5rocks.com/en/tutorials/file/xhr2/

我知道文件的名称和类型是什么,我怎么把这个阵列中的数据变成了“文件”,并把那个给用户下载?

xhr = new XMLHttpRequest() 
xhr.open "GET", "#{window.API_URL}/transfer/123/2134/#{program_id}", true 
xhr.responseType = "arraybuffer" 
xhr.onloadstart = (e) -> 
    # downloading file notify on footer 
xhr.onprogress = (e) -> 
    # show progress and size of file. 
    console.log "#{Math.round(e.loaded/e.total)*100}% completed" 
xhr.onload = (e) -> 
    uInt8Array = new Uint8Array(@response) 

xhr.send() 

回答

0

试试这个办法(工作在IE10 +,FF,铬):

var blob = new Blob([uInt8Array], {type: contentType }); 

,并参考answer