admin管理员组文章数量:1425780
I'm using the HTML5 File API to assemble multipart form data for submission by XHR to a web service. I have the whole thing working in FF, which has a nice convenient getAsBinary() method included in their implementation of the file API. This was a pretty sweet deal. It basically went:
var const; // constructor
const += headers;
const += field_data;
for(var i = 0; i < files.length; i++)
{
const += files[i].getAsBinary();
}
sendData(const);
Worked like a charm.
To get it working in Chrome, though, I have to make a FileReader object, which handles a bit differently. I essentially have to go:
var const; // constructor
const += headers;
const += field_data;
var reader = new FileReader();
for(var i = 0; i < files.length; i++)
{
reader.onload = (function(file)
{
const += file.target.result; // const is not in scope in this anonymous function!
}
reader.readAsBinaryString(files[i]);
}
sendData(const);
Which doesn't work, for two main reasons. Firstly, the read happens asynchronously, so by the time it gets to the sendData() function, the file data isn't written to the const variable. Secondly, the const variable is out of scope inside the reader.onload handler. However I re-jig the code, I seem to e across one of these obstacles, and I'm struggling to e up with a graceful way of handling it.
Any suggestions?
I'm using the HTML5 File API to assemble multipart form data for submission by XHR to a web service. I have the whole thing working in FF, which has a nice convenient getAsBinary() method included in their implementation of the file API. This was a pretty sweet deal. It basically went:
var const; // constructor
const += headers;
const += field_data;
for(var i = 0; i < files.length; i++)
{
const += files[i].getAsBinary();
}
sendData(const);
Worked like a charm.
To get it working in Chrome, though, I have to make a FileReader object, which handles a bit differently. I essentially have to go:
var const; // constructor
const += headers;
const += field_data;
var reader = new FileReader();
for(var i = 0; i < files.length; i++)
{
reader.onload = (function(file)
{
const += file.target.result; // const is not in scope in this anonymous function!
}
reader.readAsBinaryString(files[i]);
}
sendData(const);
Which doesn't work, for two main reasons. Firstly, the read happens asynchronously, so by the time it gets to the sendData() function, the file data isn't written to the const variable. Secondly, the const variable is out of scope inside the reader.onload handler. However I re-jig the code, I seem to e across one of these obstacles, and I'm struggling to e up with a graceful way of handling it.
Any suggestions?
Share Improve this question asked Mar 19, 2011 at 14:38 R HillR Hill 1,8042 gold badges23 silver badges36 bronze badges 1- Actually "const" definitely is in scope, but scope is not the problem. – Pointy Commented Mar 19, 2011 at 14:42
2 Answers
Reset to default 3What you're going to have to do is have the reader "load" handlers all check to see whether they're the last one to run. When that happens, then that handler can call "sendData()".
var const; // constructor
const += headers;
const += field_data;
var reader;
var finished = 0;
for(var i = 0; i < files.length; i++)
{
reader = new FileReader();
reader.onload = function(file)
{
const += file.target.result;
if (++finished === files.length)
sendData(const);
};
reader.readAsBinaryString(files[i]);
}
(I don't fully understand the details of how that accumulated "const" thing will correctly turn into a multipart MIME blob, but I presume that you do :-) Also, and this is probably important: I think you probably need to make a new "FileReader" instance for each file. I coded this that way (actually I just edited it) but that may be incorrect, as I'm not that familiar with the API and its semantics.
Have you considered xhr.send(FormData)
? See my response here: upload to php $_FILE from chrome extension
You can append files to a FormData object and send that via xhr. The browser constructs the multi-part request for you. I believe this is available in FF4 and has been in Chrome for some time.
本文标签: htmlJavascriptHTML5 file API reading sequential files into multipart form dataStack Overflow
版权声明:本文标题:html - JavascriptHTML5 file API reading sequential files into multipart form data - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745400292a2657004.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论