My favorites | Sign in
Project Home Downloads Wiki Issues Code Search
New issue   Search
for
  Advanced search   Search tips   Subscriptions
Issue 35705: Extend XmlHttpRequest with getAsBinary() and sendAsBinary() methods, like in FF3
59 people starred this issue and may be notified of changes. Back to list
Status:  WontFix
Owner:  ----
Closed:  Feb 2012
Cc:  jia...@chromium.org, ericu@chromium.org, micha...@chromium.org, kinuko@chromium.org

Restricted
  • Only users with Commit permission may comment.


Sign in to add a comment
 
Reported by edoardo....@gmail.com, Feb 13, 2010
It would be great if the XmlHttpRequest object in Chromium could be 
extended with getAsBinary() and sendAsBinary() methods, like in FF3.

This would greatly help the development of extensions that can fetch and 
upload binary file from/to web services (like Google App Engine Blobstore for 
example).


Jun 3, 2010
#1 wilfred....@gmail.com
The chrome implementation is useless without getAsBinary()
Jul 2, 2010
#2 erik...@chromium.org
(No comment was entered for this change.)
Labels: not-extensions
Jul 28, 2010
#3 toni.ruo...@gmail.com
The problem is really irritating. However the correct solution, atleast for send, would be supporting XHR level2 specification. It supports using the send function on a blob object. I'm not really sure if there is a standard API to construct a blob, but I am expecting that will change in the future. Atleast you should be able to get blobs by reading data from a file or the web cam. I also think that google gears has some nonstandard support for creating a blob.
Aug 2, 2010
#4 ora...@gmail.com
The problem is really annoying, a lot form data is transferred in binary mode, the only way to fix problem at this time is to do encode/decode, which is only possible if the form processor is able to do that (which are none, except for some custom prepared pages).
Aug 5, 2010
#5 toni.ruo...@gmail.com
I want to point out that receiving binary is in most cases not that much of a problem. It requires some extra care in defining correct modes and making sure the extra bits in javascript's 16-bit strings are turned into zeros, but in many cases this works, as long as the server sets correct modes. It may be handy to have a function that also lets the user force reading of incoming data as binary, but that is another story.

I have filed a separate bug asking support for sending out binary data in a standards compliant way, as defined by the xmlhttprequest level2 specification. See https://code.google.com/p/chromium/issues/detail?id=51267 I do realize that some users are still expecting Firefox compatible legacy support functions for sending/receiving binary data, so I am not asking Chromium devs to close this bug. I am also not sure, if a separate bug should be filed against receiving binary data in an xmlhttprequest level2 compliant way.

Sep 1, 2010
#6 darin@chromium.org
I'm not sure that we want to implement those non-standard Firefox extensions.  We already have support for XMLHttpRequest.send(Blob).  We just need to provide a way to construct a Blob from a Uint8Array, or alternatively from a String w/ some explicitly specified charset conversion.  This would probably be done by extending BlobBuilder to have a new append method.

Today BlobBuilder.append forces a String to UTF-8:
http://www.w3.org/TR/file-writer-api/#widl-BlobBuilder-append
Status: Untriaged
Cc: jia...@chromium.org er...@chromium.org micha...@chromium.org
Labels: -Area-Undefined Area-WebKit
Sep 1, 2010
#7 felixl...@gmail.com
I came to this bugreport, when I want to implement an ajax-file-upload based on the MDC tutorial: 
https://developer.mozilla.org/en/using_files_from_web_applications

It's really hard to find code when searching for "ajax file upload" because the results refer to flash-based solutions or hidden-form-submits. So the MDC tutorial was best source.

I didn't know that the XMLHttpRequest.send(blob) method can handle this, because it's written nowhere. After deeper searching I found this:
http://comments.gmane.org/gmane.comp.web.chromium.extensions/6568

It does exactly what sendAsBinary() should do (but it's faster because it doesn't extracts the binary code first). Maybe other people get help with this.
Sep 1, 2010
#8 Trente.N...@gmail.com
Yes a more general BlobBuilder.append would do... anything that just lets you build and send a binary object ;)
Sep 1, 2010
#9 toni.ruo...@gmail.com
A BlobBuilder.append for appending arbitrary bytes would be useful indeed. Currently a web application needs to ask the user for a file with all possible bytes, and retrieve it through file uploader interface which returns a file that inherits from blob. Although the situation would already be better, if we could host such file on the web and retrieve it with XmlHttpRequest2 or such. Then, at least we would not need to bother users.
Sep 3, 2010
#10 toni.ruo...@gmail.com
I wrote a small demo application to show how you can send arbitrary binary data as the body of a http POST, from javascript using xhr2.

http://www.cs.helsinki.fi/u/twruottu/testi/bpost.html

The only limitation is that you need to ask your user to download a file with all bit patterns and drag it back to the page. The user only needs to do this once when entering the web page. After this javascript can do multiple http POSTs with different data.

There are some limitations to this approach. First of all, it only works on systems with 8-bit byte size. Also, there is no way to check, if the user really uploaded back the correct file. Communications will fail, if we got a wrong file back. Making sure user is able to download the byte file correctly may be a bit tricky in some cases.
Sep 7, 2010
#11 toni.ruo...@gmail.com
Mr. Uhrhane, could you give us a status update on the current status of blobBuilder design? Have any additional append methods been discussed?

I realize there are multiple alternatives how someone might want to append binary data. First I thought bb.append(integer value of one byte) would be good enough, but supporting other common formats might make sense too. So maybe one or more of bb.append(dataurl), bb.append([integer value of a byte]), or bb.append(string with meaningless upper 8bits) is wanted.

Could we agree on at least one method, so we could get that into the specification, and get going with it?
Sep 7, 2010
#12 toni.ruo...@gmail.com
As  issue 52486  ("Implement an XHR.responseBlob accessor") gets fixed, we could start hosting allbytes.dat at some url, and create a library which generates arbitrary blobs usinng that. It would not be perfect, but atleast it would save users some trouble in the transitional phase.

[52486] https://code.google.com/p/chromium/issues/detail?id=52486
Sep 8, 2010
#15 ericu@chromium.org
Toni, the right place to discuss additions to the BlobBuilder spec is the WebApps Working Group, public-webapps@w3.org.  Please check their archives for background, and feel free to propose the interface that would suit your needs.
Sep 9, 2010
#16 ka...@chromium.org
(No comment was entered for this change.)
Labels: Mstone-X
Sep 22, 2010
#17 bslatkin
The lack of a solution here or any work around significantly impacts what Chrome extensions you can build. Sucks.
Sep 23, 2010
#18 toni.ruo...@gmail.com
Sending binary blobs is already possible. A separate bug for retrieving binary blobs has been filed at https://code.google.com/p/chromium/issues/detail?id=52486

This bug report should probably be closed, and new bugs should be filed for the lack of methods for constructing blobs and taking them apart. However, getting that bug fixed might not be easy as long as the standardization bodies have not figured out the correct API for doing such things.
Oct 7, 2010
#19 toni.ruo...@gmail.com
The message in this thread seems to be that Chrome needs a non-standard way of feeding generated data into a blob. If later, someone wishes to standardize that it is a good thing, but we should not discus the standard while there are people starving. So what we really need to begin with is an append function (in Chrome, perhaps not in any standard) that takes an array of bytes, presented as an array of unsigned integer values. That will do for the lack of sendAsBinary.

With getAsBinary the situation is not that bad as it is only a convenience function. Similarly being able to "see" inside a blob would be useful, but is not required for this use case as it is already possible to read binary data into a 16-bit string and throw away the high bits. The instructions for doing this are given at MDC, but seem to work in Chrome. See https://developer.mozilla.org/En/XMLHttpRequest/Using_XMLHttpRequest#Receiving_binary_data


Oct 7, 2010
#20 toni.ruo...@gmail.com
I have a new bug, where I request support for adding arbitrary bytes into a blob. See https://code.google.com/p/chromium/issues/detail?id=58264 for details.
Oct 10, 2010
#21 aku...@gmail.com
I've been digging through a number of the links here, but have yet to find a simple example of a sendAsBinary workaround using blobs?  Could someone familiar with the best practices for this post a short snipped of what the chrome version of "xhr.sendAsBinary(someData);" should look like?



Nov 20, 2010
#22 downch...@gmail.com
This is working Chrome 9 using .send(Blob) - see  issue #58264  for details on using Blob builder to create a binary blob from a typed array. Issue fixed.
Nov 21, 2010
#23 Trente.N...@gmail.com
Wow, @downchuck, if this is indeed the case, that's at long last a solution!!! Great :-)
Nov 21, 2010
#24 toni.ruo...@gmail.com
This fixes the problem, but not before  bug 58264  ( https://code.google.com/p/chromium/issues/detail?id=58264 ) is fixed. The good news is, that the standard specifications now make this possible. As soon as the spec is implemented by Chrome devs, we'll have a solution.
Nov 24, 2010
#25 downch...@gmail.com
Toni: Please let me know what the issue is. I've commented on your bug report -- I think you misunderstood my suggestion. You can build up your blob to send only a single file -- you do not need to use multipart mime.

Tested against Google Storage, as well as Google Docs.
Nov 24, 2010
#26 powrs...@gmail.com
Maybe I'm just having trouble working with the BlobBuilder, but I presently have a form ( http://www.sitebrandbuilder.com/test/resize/index2.php ) that will take images that the user wants to upload and then uses canvas to resize them all and generates the appropriate multipart mime headers and binary data to send the resized code in Firefox using sendAsBinary. Creating a BlobBuilder, appending said data, and then using .send(Blob) did not work, neither with or without the multipart boundaries.
Nov 25, 2010
#27 toni.ruo...@gmail.com
Here is a working code example that does HTTP POST with all bytes in the reverse order http://www.cs.helsinki.fi/u/twruottu/testi/bpost4.html

To replicate sendAsBinary one would need to get character codes of the string and do binary AND (i.e. &) with 0xff. From there on one could use the technique from my example.
Nov 25, 2010
#28 toni.ruo...@gmail.com
I think it is possible to code sendAsBinary as follows. How ever, I have not tested this chunk of code much. It might not be too efficient, and might have bugs in it.

XMLHttpRequest.prototype.sendAsBinary = function(datastr) {
        var bb = new BlobBuilder();
        var data = new ArrayBuffer(1);
        var ui8a = new Uint8Array(data, 0);
        for (var i in datastr) {
                if (datastr.hasOwnProperty(i)) {
                        var chr = datastr[i];
                        var charcode = chr.charCodeAt(0)
                        var lowbyte = (charcode & 0xff)
                        ui8a[0] = lowbyte;
                        bb.append(data);
                }
        }
        var blob = bb.getBlob();
        this.send(blob);
}

Nov 25, 2010
#29 toni.ruo...@gmail.com
I was not able to find a webpage describing how getAsBinary() would work with XHR in Firefox. Are you sure there is such a function for XHR? I found some references to other APIs having such functions, but not XHR.
Nov 25, 2010
#30 iSe...@gmail.com
The bug submitter most likely confused it with the original Mozilla File API (not the HTML5 File API)'s getAsBinary() method on files.
Nov 25, 2010
#31 toni.ruo...@gmail.com
In that case I think this bug could finally be closed. :-)
Nov 25, 2010
#32 toni.ruo...@gmail.com
I wrote about the method on my blog to get some more feedback
http://objectprog.blogspot.com/2010/11/sendasbinary-for-google-chrome.html
Nov 26, 2010
#33 toni.ruo...@gmail.com
It turns out Blogger does not support code in comments. I have moved development of the function to http://javascript0.org/wiki/Portable_sendAsBinary
Nov 26, 2010
#34 toni.ruo...@gmail.com
I did some cleanups together with Konrad Markus. Here is the result...

XMLHttpRequest.prototype.sendAsBinary = function(datastr) {
        var bb = new BlobBuilder();
        var data = new ArrayBuffer(datastr.length);
        var ui8a = new Uint8Array(data, 0);
        for (var i=0; i<datastr.length; i++) {
                ui8a[i] = (datastr.charCodeAt(i) & 0xff);
        }
        bb.append(data);
        var blob = bb.getBlob();
        this.send(blob);
}
Nov 26, 2010
#35 downch...@gmail.com
Uint8 will clamp the value automatically. You may be right though, about using a bit mask. For Moz compatibility: I haven't checked on how setRequestHeader('Content-Type') is handled in Moz. Consider the following code for discussion (not use). Your code looks good/safe.

XMLHttpRequest.prototype.sendAsBinary = function(datastr,contentType) {
        var bb = new BlobBuilder();
        var len = datastr.length;
        var data = new Uint8Array(len);
        for (var i=0; i<len; i++) {
                data[i] = datastr.charCodeAt(i);
        }
        bb.append(data.buffer);
        this.send(bb.getBlob(contentType));
}
Dec 13, 2010
#36 FyHertz
Dudes this hack doesn't work for me, as both ArrayBuffer and Uint8Array objects don't exist on Chrome :( I'm running Chromium 8.0.552.215 on Ubuntu 10.04. 
Any ideas ?
Dec 13, 2010
#37 downch...@gmail.com
It's in M9
Jan 31, 2011
#38 toni.ruo...@gmail.com
The latest XHR spec supports sending typed arrays directly. The following version seems to work in Chrome 10.0.648.6 dev

XMLHttpRequest.prototype.sendAsBinary = function(datastr) {
        var data = new ArrayBuffer(datastr.length);
        var ui8a = new Uint8Array(data, 0);
        for (var i=0; i<datastr.length; i++) {
                ui8a[i] = (datastr.charCodeAt(i) & 0xff);
        }
        this.send(data);
}

Jan 31, 2011
#39 toni.ruo...@gmail.com
It also seems you can create the typed array directly, without creating a buffer and a view separately. By doing this simplification we get...

XMLHttpRequest.prototype.sendAsBinary = function(datastr) {
        var ui8a = new Uint8Array(datastr.length);
        for (var i = 0; i < datastr.length; i++) {
                ui8a[i] = (datastr.charCodeAt(i) & 0xff);
        }
        this.send(ui8a.buffer);
}
Jan 31, 2011
#40 toni.ruo...@gmail.com
You can also give Uint8Array data initial values in an array. This way we can get rid of the explicit looping structures.

XMLHttpRequest.prototype.sendAsBinary = function(datastr) {
    function byteValue(x) {
        return x.charCodeAt(0) & 0xff;
    }
    var ords = Array.prototype.map.call(datastr, byteValue);
    var ui8a = new Uint8Array(ords);
    this.send(ui8a.buffer);
}

Mar 29, 2011
#41 maruel@chromium.org
(No comment was entered for this change.)
Labels: -Type-Bug Type-Feature
Mar 29, 2011
#42 toni.ruo...@gmail.com
Can we remove getAsBinary from the bug title and description? Mozilla does not provide such function for XHR, and it is confusing me over and over again.
May 13, 2011
#43 kar...@google.com
(No comment was entered for this change.)
Status: Available
Jun 1, 2011
#44 anders.e...@gmail.com
The implementations of sendAsBinary here work quite well, but it crashes Chrome (11.0.696.71) for larger files (around 30 MB). Perhaps I'm doing it wrong, but what I do is:
1. Create a FileReader, and call readAsBinaryString
2. In FileReader.onload, i construct a request string with a boundary and the binary data.
3. This is sent to the userland implementation of sendAsBinary, which then crashes, probably because the string is too long.

Are there any plans for providing a built in implementation of this? I can live with the fact that it might not be a sendAsBinary on the XHR object, since that is Mozillas own way of doing it.
Jun 1, 2011
#45 toni.ruo...@gmail.com
You need to get the file object from <input type="file"> [1]. File objects are a sub types of blob [2]. You should be able to call send on a blob [3].

Please report the crash separately. It would be good, if you could create a minimal code example that could be used to reproduce the crash. It would be good, if the crash could be reproduced without using a separate file, but sending the code and a file is probably ok.

[1] http://www.w3.org/TR/FileAPI/#dfn-filelist
[2] http://www.w3.org/TR/FileAPI/#dfn-file
[3] http://www.w3.org/TR/XMLHttpRequest2/#the-send-method

Nov 9, 2011
#46 brod.jus...@gmail.com
The comment 40 update breaks Chrome 14 on Ubuntu, the Array.prototype.map.call(..) fails. The previous version in comment 39 works fine.
Nov 14, 2011
#47 toni.ruo...@gmail.com
Sounds pretty bad. Are all maps broken?
Try opening the console (ctrl + i, click "Console") and run...
Array.prototype.map.call([1,2,3],function(x){return 2 * x;});
The result should be [2, 4, 6]
Dec 27, 2011
#48 kinuko@chromium.org
(No comment was entered for this change.)
Cc: kinuko@chromium.org
Feb 2, 2012
#49 downch...@gmail.com
Can we mark this bug as FIXED? I'm having no issues working with binary XHR in M16.
Feb 2, 2012
#50 downch...@gmail.com
Can we mark this bug as FIXED? I'm having no issues working with binary XHR in M16.
Feb 2, 2012
#51 toni.ruo...@gmail.com
The feature request was for a Firefox compatible sendAsBinary method. If the dev team decides XHR2 is enough, then this bug should be marked "invalid" or "won't fix" or similar.
Feb 2, 2012
#52 ericbidelman@chromium.org
All the pieces are in place to "sendAsBinary()", but using standards-based constructs.  Marking this as FIXED. If someone feels otherwise, please reopen 
Status: WontFix
Feb 6, 2012
#53 gufo...@gmail.com
i have used 

XMLHttpRequest.prototype.sendAsBinary = function(datastr) {
    function byteValue(x) {
        return x.charCodeAt(0) & 0xff;
    }
    var ords = Array.prototype.map.call(datastr, byteValue);
    var ui8a = new Uint8Array(ords);
    this.send(ui8a.buffer);
}

for js method (not replace FF method or other funcion) rename sendAsBinary and use new method
XMLHttpRequest.prototype.sendBin 
success on (FF crome other ?)
Oct 13, 2012
#54 bugdro...@chromium.org
This issue has been closed for some time. No one will pay attention to new comments.
If you are seeing this bug or have new data, please click New Issue to start a new bug.
Labels: Restrict-AddIssueComment-Commit
Mar 10, 2013
#55 bugdro...@chromium.org
(No comment was entered for this change.)
Labels: -Area-WebKit Cr-Content
Apr 5, 2013
#56 bugdro...@chromium.org
(No comment was entered for this change.)
Labels: -Cr-Content Cr-Blink
Sign in to add a comment

Powered by Google Project Hosting