views:

153

answers:

2

I'm working on a webapp that uses several cutting-edge WebKit features. It essentially does this: reads a local file with the FileReader, unzips each file into a string using a JavaScript unzip library, and POSTs each file using XMLHttpRequest. This works great for text files, but unfortunately it corrupts binary files (in this case, images). Firefox has a sendAsBinary method that solves this problem, but it is non-standard, and more to the point, it doesn't work on WebKit/Chrome which we depend on for other features.

There are a TON of workarounds, and so far none of them work for me:

  • Mocking a file upload request with headers, boundaries, and so forth in a long string (like this).
  • Setting a bunch of headers on the xhr object (as such)
  • Using the BlobBuilder, appending the string to the builder, and using getBlob to get a blob to upload (as recommended in the Chrome issue thread about this)

What I'm looking for, most of all, is a forward-compatible solution. Thanks!

+1  A: 

You can encode it with base64 and decode it on the server.

jcubic
A: 

i'm working on a similiar bit of code and have encountered the same issue. base64 encoding inflates the file size dramatically plus you can't take advantage of blob slicing.

did you ever find a reasonable solution? if so - can you post it?

Keith Fitzgerald
I relented and used base64 encoding -- it definitely inflates file size but it's otherwise pretty foolproof. I'm pretty convinced there isn't a better way at the moment.
heydenberk