- Screen name: vjeuxx
vjeuxx's Profile
2 Posts
1 Responses
0
Followers
Show:
- Expanded view
- List view
Private Message
- 17-Aug-2011 09:52 PM
- Forum: Developing jQuery Core
Hello,ProblemI have been working on an shim to provide the DataView API for all the browsers (http://blog.vjeux.com/2011/javascript/jdataview-read-binary-file.html). As cross-browser XHR request is not something easy, I decided to rely on jQuery.In order to get binary data with XHR you can either receive it as an ArrayBuffer or Binary String:- // Request Modifiers
- xhr.responseType = 'arraybuffer';
- xhr.overrideMimeType('text/plain; charset=x-user-defined');
- // Data Retrieval
- if ('mozResponseArrayBuffer' in xhr) {
- data = xhr.mozResponseArrayBuffer;
- }
- else if ('responseType' in xhr && xhr.responseType === 'arraybuffer' && xhr.response) {
- data = xhr.response;
- }
- else {
- data = xhr.responseText;
- }
It is possible to add the modifiers using the ajaxPrefilter API:- jQuery.ajaxPrefilter('dataview', function(options, originalOptions, jqXHR) {
- if (!options.hasOwnProperty('xhrFields')) {
- options.xhrFields = {};
- }
- options.xhrFields.responseType = 'arraybuffer';
- options.mimeType = 'text/plain; charset=x-user-defined';
- });
However there is no hook for the xhr.responseProposalAddition of a responseHandler option. It is an object that associates a dataType with its handling function.This would be used like this:- jQuery.ajaxSetup({
- responseHandler: {
- dataview: function (responses, options, xhr) {
- // Array Buffer Firefox
- if ('mozResponseArrayBuffer' in xhr) {
- responses.text = xhr.mozResponseArrayBuffer;
- }
- // Array Buffer Chrome
- else if ('responseType' in xhr && xhr.responseType === 'arraybuffer' && xhr.response) {
- responses.text = xhr.response;
- }
- // Older Browsers
- else {
- responses.text = xhr.responseText;
- }
- }
- }
- });
Here is a possible implementation:- // jQuery 1.6.2 - Line 7909
- if (s.responseHandler && s.responseHandler[ s.dataType ]) {
- s.responseHandler[ s.dataType ](responses, s, xhr);
- }
- else {
- xml = xhr.responseXML;
- // Construct response list
- if ( xml && xml.documentElement /* #4958 */ ) {
- responses.xml = xml;
- }
- responses.text = xhr.responseText;
- }
You can view the patch in action in http://fooo.fr/~vjeux/github/jsWoWModelViewer/modelviewer.htmlThoughtsThe only way I found to add support for binary XHR in jQuery without altering the source code is to update the whole default ajaxTransport. However since it's a 170 lines function, it will not be resilient to library changes.The responseHandler proposal is the solution that involves as few changes in jQuery as possible.I believe that this hook is important to jQuery as it will allow plugins to enable cross-browser processing of binary files.Thanks!Hello,
For a WebGL application, I need to be able to parse a binary encoded file. The basic Ajax request response is a Unicode string with data loss (multiple byte combinations can produce the same non-valid unicode character). An option is available to the XMLHttpRequest component to allow this binary transfer. We just need to add this line:
req.overrideMimeType('text/plain; charset=x-user-defined');
You can read more about this technique at the Mozilla Developer Center: https://developer.mozilla.org/En/Using_XMLHttpRequest#Receiving_binary_data
It doesn't work on all browsers and I couldn't find any workaround for unsupported browsers.
- Chrome 4.0.295.0: Works
- Firefox 3.5.7: Works
- Safari 4.0.4: Works
- Internet Explorer 8: Does not work. Doesn't have the overrideMimeType method.
- Opera 10.10: Does not work. Have the overrideMimeType method but doesn't take it in account.
That would be very handy to have this support on jQuery instead of having to rewrite our own custom Ajax handler just for one line. I don't know what is your policy about methods that are not supported by some browsers, but this would be a great addition!If you want to see an application, I've setup a BinaryReader class that allows you to work with binary stream directly in javascript as you would do in C++, .NET or Java: http://blog.vjeux.com/2010/javascript/javascript-binary-reader.html--Vjeux- «Prev
- Next »
Moderate user : vjeuxx