New issue
Advanced search Search tips
Note: Color blocks (like or ) mean that a user may not be available. Tooltip shows the reason.
Starred by 59 users
Status: WontFix
Owner: ----
Closed: Feb 2012
Cc:
Components:
EstimatedDays: ----
NextAction: ----
OS: ----
Pri: 2
Type: Feature

Restricted
  • Only users with Commit permission may comment.



Sign in to add a comment
Extend XmlHttpRequest with getAsBinary() and sendAsBinary() methods, like in FF3
Reported by edoardo....@gmail.com, Feb 14 2010 Back to list
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).


 
Comment 1 by Deleted ...@, Jun 3 2010
The chrome implementation is useless without getAsBinary()
Labels: not-extensions
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.
Comment 4 by ora...@gmail.com, Aug 2 2010
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).
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 http://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.

Comment 6 by darin@chromium.org, Sep 1 2010
Labels: -Area-Undefined Area-WebKit
Status: Untriaged
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
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.
Yes a more general BlobBuilder.append would do... anything that just lets you build and send a binary object ;)
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.
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.
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?
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] http://code.google.com/p/chromium/issues/detail?id=52486
Comment 13 Deleted
Comment 14 Deleted
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.
Labels: Mstone-X
Comment 17 by bslat...@gmail.com, Sep 22 2010
The lack of a solution here or any work around significantly impacts what Chrome extensions you can build. Sucks.
Sending binary blobs is already possible. A separate bug for retrieving binary blobs has been filed at http://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.
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


I have a new bug, where I request support for adding arbitrary bytes into a blob. See http://code.google.com/p/chromium/issues/detail?id=58264 for details.
Comment 21 by aku...@gmail.com, Oct 10 2010
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?



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.
Wow, @downchuck, if this is indeed the case, that's at long last a solution!!! Great :-)
This fixes the problem, but not before  bug 58264  ( http://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.
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.
Comment 26 by powrs...@gmail.com, Nov 24 2010
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.
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.
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);
}

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.
Comment 30 by ise...@gmail.com, Nov 25 2010
The bug submitter most likely confused it with the original Mozilla File API (not the HTML5 File API)'s getAsBinary() method on files.
In that case I think this bug could finally be closed. :-)
I wrote about the method on my blog to get some more feedback
http://objectprog.blogspot.com/2010/11/sendasbinary-for-google-chrome.html
It turns out Blogger does not support code in comments. I have moved development of the function to http://javascript0.org/wiki/Portable_sendAsBinary
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);
}
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));
}
Comment 36 by Deleted ...@, Dec 14 2010
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 ?
It's in M9
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);
}

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);
}
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);
}

Labels: -Type-Bug Type-Feature
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.
Comment 43 by kareng@google.com, May 13 2011
Status: Available
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.
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

Comment 46 by Deleted ...@, Nov 9 2011
The comment 40 update breaks Chrome 14 on Ubuntu, the Array.prototype.map.call(..) fails. The previous version in comment 39 works fine.
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]
Cc: kinuko@chromium.org
Can we mark this bug as FIXED? I'm having no issues working with binary XHR in M16.
Can we mark this bug as FIXED? I'm having no issues working with binary XHR in M16.
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.
Status: WontFix
All the pieces are in place to "sendAsBinary()", but using standards-based constructs.  Marking this as FIXED. If someone feels otherwise, please reopen 
Comment 53 by Deleted ...@, Feb 6 2012
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 ?)
Project Member Comment 54 by bugdroid1@chromium.org, Oct 13 2012
Labels: Restrict-AddIssueComment-Commit
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.
Project Member Comment 55 by bugdroid1@chromium.org, Mar 11 2013
Labels: -Area-WebKit Cr-Content
Project Member Comment 56 by bugdroid1@chromium.org, Apr 6 2013
Labels: -Cr-Content Cr-Blink
Sign in to add a comment