Re: using BlobBuilder append method with generated binary data

That's what I thought when I first looked at it, however you can pre-set the
array length and then iterate over every byte and set it. It is not ideal,
but works.  I have it running on http://appmator.appspot.com/ but it
basically boils down to the following:

var ui8a = new Uint8Array(output.length);

for(var i = 0; i< output.length; i++) {
  ui8a[i] = output.charCodeAt(i);
}

bb.append(ui8a.buffer);

var blob = bb.getBlob("application/octet-stream");

var saveas = document.createElement("iframe");
saveas.style.display = "none";
saveas.src = window.createObjectURL(blob);

P

On Mon, Dec 6, 2010 at 8:34 PM, Toni Ruottu <toni.ruottu@iki.fi> wrote:

> I do not think UInt8Array has any data. It is just a view for
> accessing the buffer.
>
>  --Toni
>
> On Sat, Dec 4, 2010 at 1:12 AM, Paul Kinlan <paulkinlan@google.com> wrote:
> > I might have missed something but for appending client-side generated
> > data to a blob can't you just append a UInt8Array as that is based off
> > ArrayBuffer?
> >
> > I use it to generate zip files client side and attach them to an
> > iframe so they are downloaded.
> >
> > P
> > On Thursday, December 2, 2010, Toni Ruottu <toni.ruottu@iki.fi> wrote:
> >> My code had a bug in it. The APIs seem to be working ok for my purposes.
> >> Using the APIs, I was able to write Firefox's sendAsBinary method for
> chrome.
> >> It is available from http://javascript0.org/wiki/Portable_sendAsBinary
> >>
> >>   --Toni
> >>
> >> On Tue, Nov 16, 2010 at 5:25 PM, Toni Ruottu <toni.ruottu@iki.fi>
> wrote:
> >>> Sorry for slow answer. Took me a while to get on top of this. I think
> >>> they just might answer my problem. However, I can not really tell
> >>> before I see a working implementation. I wrote a test. See
> >>> http://www.cs.helsinki.fi/u/twruottu/testi/bpost3.html It does not
> >>> currently work with the latest development version of Chrome. I
> >>> wonder, if I am doing something wrong.
> >>>
> >>>  --Toni
> >>>
> >>> On Wed, Oct 20, 2010 at 6:17 PM, Eric Uhrhane <ericu@google.com>
> wrote:
> >>>> Toni:
> >>>>
> >>>>    BlobBuilder now has an append() method that takes an ArrayBuffer,
> >>>> and FileReader has readAsArrayBuffer.  Do these together satisfy your
> >>>> needs?
> >>>>
> >>>>        Eric
> >>>>
> >>>> On Wed, Oct 20, 2010 at 3:39 AM, Toni Ruottu <toni.ruottu@iki.fi>
> wrote:
> >>>>> I have discussed the topic before on some Chromium bug threads. I
> >>>>> searched the archives of this mailing list for blobs and generated
> >>>>> content, but was not too successful. There may be multiple different
> >>>>> reasons why one would need to build blobs from generated data. The
> one
> >>>>> that concerns me, is being able to do binary POSTs with
> XmlHTTPRequest
> >>>>> in a standard manner. To send binary data over XHR one is required to
> >>>>> send out a blob, but first the blob needs to be constructed with
> >>>>> BlobBuilder. Let me fill you in on how I see the current situation.
> >>>>> Maybe someone can spot an error in my line of thought.
> >>>>>
> >>>>> A new (empty) BlobBuilder is created by stating...
> >>>>> var bb = new BlobBuilder();
> >>>>>
> >>>>> After creating a BlobBuilder, the user appends some data into the
> blob
> >>>>> being generated by using the append function. The append function is
> >>>>> overloaded and can be used to append multiple different types of data
> >>>>> into the blob. Currently only existing blobs, and utf-8 text are
> >>>>> supported.
> >>>>>
> >>>>> One can add an existing blob into the new one by doing...
> >>>>> bb.append(existingBlob);
> >>>>>
> >>>>> One can add a text string in utf-8 format by doing...
> >>>>> bb.append(aTextString);
> >>>>>
> >>>>> Once the data is in there, it is possible to construct the new blob
> with...
> >>>>> var blob = bb.getBlob()
> >>>>>
> >>>>> A blob has a slice method which makes it possible to turn a large
> blob
> >>>>> into smaller ones. It is thus possible to cut out single bytes from a
> >>>>> blob and use them construct new blobs by appending copies of these
> >>>>> sample bytes together with the BlobBuilder.
> >>>>>
> >>>>> As sample bytes can only be generated by writing text into the blob
> as
> >>>>> utf-8, it is not possible to generate all 8-bit patterns. More
> >>>>> specifically, the bit patterns that are illegal in any given utf-8
> >>>>> string, are not possible to generate using the current API. This
> leads
> >>>>> developers into using various hacks to overcome the issue.
> >>>>>
> >>>>> For example one could ask the user to provide a file with sample
> >>>>> bytes. Methods for reading a file into a blob are available, so there
> >>>>> is no need to generate the sample bytes. There is however no way to
> >>>>> make sure that the user provided a file with correct bit-patterns as
> >>>>> the blob interface is to a large degree a read-only interface. A
> >>>>> demonstration of using a file to overcome the issue is available at
> >>>>> http://www.cs.helsinki.fi/u/twruottu/testi/bpost.html
> >>>>>
> >>>>> To solve the problem a third append function which accepts a list of
> >>>>> integers, and adds the corresponding bytes into the blob is needed.
> >>>>> Once such method has been introduced it should be possible to write
> >>>>> the 7-bit ascii string "foo" into a BlobBuilder by doing...
> >>>>> bb.append([102,111,111]);
> >>>>>
> >>>>> It should also be possible to add bytes 0x00 a
> >
> > --
> > Paul Kinlan
> > Developer Advocate @ Google for Chrome and HTML5
> > t: +447730517944
> > tw: @Paul_Kinlan <http://twitter.com/paul_kinlan>
> > LinkedIn: http://uk.linkedin.com/in/paulkinlan
> > Blog: http://paul.kinlan.me
> > Skype: paul.kinlan
> >
>



-- 
Paul Kinlan
Developer Advocate @ Google for Chrome and HTML5
t: +447730517944
tw: @Paul_Kinlan
LinkedIn: http://uk.linkedin.com/in/paulkinlan
Blog: http://paul.kinlan.me
Skype: paul.kinlan

Received on Monday, 6 December 2010 20:41:36 UTC