android - Sent Blob results empty -
in cordova/phonegap app i'm testing on android 4.0 need send file server multipart/form-data
through ajax.
i have file content in arraybuffer
, put in formdata
, firstly creating blob
it.
the problem file sent appears empty.
this console session (performed on android platform through weinre) can see that:
- the file content loaded in
mybuf
- a
blob
createdmybuf
(and size non-zero) - a
formdata
object created sent (fd
)
(i'm using webkitblobbuilder
because blob
constructor raises typeerror
on platform)
❯ mybuf ▼ arraybuffer bytelength: 23673 ▶ __proto__: arraybuffer ❯ var bb = new webkitblobbuilder() undefined ❯ bb.append(mybuf) undefined ❯ myblob = bb.getblob("image/jpeg") ▼ blob size: 23673 type: "image/jpeg" ▶ __proto__: blob ❯ fd = new formdata() ▶ formdata ❯ fd.append("pics[]", myblob, "1433412118197.jpg") undefined
when perform ajax request passing fd
object data, see file sent (i see name="pics[]"
in request), content empty.
this echo of request:
post /test/post/ http/1.1 host: 192.168.1.88:50000 connection: keep-alive content-length: 212 origin: file:// content-type: multipart/form-data; boundary=----webkitformboundarywfaax1aqkbz6uuof accept: */* user-agent: mozilla/5.0 (linux; u; android 4.0.4; it-it; m-mp706i build/imm76d) applewebkit/534.30 (khtml, gecko) version/4.0 safari/534.30 accept-encoding: gzip,deflate accept-language: it-it, en-us accept-charset: utf-8, iso-8859-1, utf-16, *;q=0.7 ------webkitformboundarywfaax1aqkbz6uuof content-disposition: form-data; name="pics[]"; filename="blobd380f922a76d4b03908c426487d2fa68" content-type: image/jpeg ------webkitformboundarywfaax1aqkbz6uuof--
what tried
i tried build uint8array
arraybuffer
, build blob in several ways, none worked. uint8array
seems ok:
❯ var myu8buf = new uint8array(mybuf) undefined ❯ myu8buf ▼ uint8array 0: 255 1: 216 2: 255 3: 225 4: 1 5: 20 6: 69 7: 120 ...
then, accordingly append blobbuilder
, blob size varies:
bb.append([myu8buf])
- blob size: 19bb.append(myu8buf)
- blob size: 19bb.append([myu8buf.buffer])
- blob size: 20bb.append(myu8buf.buffer)
- blob size: 23673
so last 1 seems right one, if sent behaves original arraybuffer
(i.e. empty file sent).
file read , ajax functions
this function i'm using send formdata
(with jquery):
function sendform(fd) { $.ajax({ url: 'http://192.168.1.88:50000/test/post/', data: fd, processdata: false, contenttype: false, type: 'post', success: function(d){ console.log("data received:"); console.log(d); }, error: function(d) { console.log("ajax error!"); } }); }
and function read file content in arraybuffer
:
function file2buf(filename, callback) { window.resolvelocalfilesystemurl(filename, function(fileentry) { return fileentry.file(function(file) { // success (fileentry.file) var reader = new filereader(); reader.onloadend = function(filereadresult) { console.log("file type: "+file.type); console.log("file name: "+file.name); callback(filereadresult.target.result); }; return reader.readasarraybuffer(file); }, function(e) { // fail (fileentry.file) console.log("err in fileentry.file"); console.log(e); }); }, function(e) { // fail (resolvelocalfilesystemurl) console.log("err in resolvelocalfilesystemurl"); console.log(e); }); }
Comments
Post a Comment