POST a file as a binary stream to server using javascript -
i have doubt, have tried searching on web generally, not find answer want. local system, have uploaded file using html input type=file attribute. make file binary stream using javascript , post of stream server. have idea or example code me understand how works?
for ex: have
<input type="file" id="myfile"> <button onclick="myfunction()">upload</button> <script> function myfunction() { var x = document.getelementbyid("myfile"); //convert x iostream* //do http post request server , write file stream request.getstream } </script>
is possible js? @ moment focusing on uplaoding media files images. has uploaded iostream because format accepted server. also, has work safari!! in advance!
filereader methods support
filereader.readasbinarystring(
) is deprecated. don't use it! it's no longer in w3c file api working draft:
void abort(); void readasarraybuffer(blob blob); void readastext(blob blob, optional domstring encoding); void readasdataurl(blob blob);
nb: note file
kind of extended blob
structure.
mozilla still implements readasbinarystring()
, describe in mdn fileapi documentation:
void abort(); void readasarraybuffer(in blob blob); requires gecko 7.0 void readasbinarystring(in blob blob); void readasdataurl(in blob file); void readastext(in blob blob, [optional] in domstring encoding);
the reason behind readasbinarystring()
deprecation in y opinion following: standard javascript strings domstring
accept utf-8 characters, not random binary data. don't use readasbinarystring(), that's not safe , ecmascript-compliant @ all.
we know javascript strings not supposed store binary data mozilla in sort can. that's dangerous in opinion. blob
, typed arrays
(arraybuffer
, not-yet-implemented not necessary stringview
) invented 1 purpose: allow use of pure binary data, without utf-8 strings restrictions.
xmlhttprequest upload support
xmlhttprequest.send() has following invocations options:
void send(); void send(arraybuffer data); void send(blob data); void send(document data); void send(domstring? data); void send(formdata data);
xmlhttprequest.sendasbinary() has following invocations options:
void sendasbinary( in domstring body );
sendasbinary() not standard , may not supported in chrome.
solutions
have several options:
send()
filereader.result
offilereader.readasarraybuffer ( fileobject )
. more complicated manipulate (you'll have make separate send() it) it's recommended approach.send()
filereader.result
offilereader.readasdataurl( fileobject )
. generates useless overhead , compression latency, requires decompression step on server-side it's easy manipulate string in javascript.being non-standard ,
sendasbinary()
filereader.result
offilereader.readasbinarystring( fileobject )
mdn states that:
the best way send binary content (like in files upload) using arraybuffers or blobs in conjuncton send() method. however, if want send stringifiable raw data, use sendasbinary() method instead, or stringview (non native) typed arrays superclass.
Comments
Post a Comment