send FormData (image file) via XHR

1,005 views
Skip to first unread message

Symbroson Development

unread,
Apr 3, 2017, 1:10:51 PM4/3/17
to DroidScript
Hi,
I am trying to send a file '/sdcard/img.png' via multipart/form data. While searching in the www i found smth like this:

var xhr = new XMLHttpRequest(),
fd = new FormData();
fd.append( 'file', input.files[0] );
xhr.open( 'POST', 'http://example.com/script.php', true );
xhr.send( fd );

in an other python sample code I saw that they are using
open(filepath).read() as sent data and
urllib3.PoolManager ().request_encode_body('post',url,fields)
to send files and it worked. - maybe that helps you to help me?

Thanks a lot :)

(note: pythons open().read() returns an other string than DS's app.ReadFile() or app.CreateFile().ReadData() - maybe that's the problem?)

Symbroson Development

unread,
Apr 3, 2017, 2:14:47 PM4/3/17
to DroidScript
for example an image created with
app.CreateImage(null,null,null,'fix',2,2).Save('/sdcard/pic.png');

python open().read() returned
'\x89PNG\n\x1a\n\x00\x00\x00\nIHDR\x00\x00\x00\x02\x00\x00\x00\x02\x08\x06\x00\x00\x00r¶\n$\x00\x00\x00\x04sBIT\x08\x08\x08\x08|\x08d\x88\x00\x00\x00\x0bIDAT\x08\x99c`@\x07\x00\x00\x12\x00\x01oúc·\x00\x00\x00\x00IEND®B`\x82'

How I get the same in JS?

Symbroson Development

unread,
Apr 4, 2017, 3:21:23 AM4/4/17
to DroidScript
I need to read a file in binary mode like python does it with open(path,'rb').read()
But I can't find a js equal. I also tried to make a file object but new File(path) always returns a constructor error.

thanks for any help 😔

Virtuos86

unread,
Apr 4, 2017, 3:37:06 AM4/4/17
to DroidScript
I could not resolve this problem in due time. I think we need some function for reading a binary files into a byte string. app.ReadFile reads files into a character string.

Virtuos86

unread,
Apr 4, 2017, 3:41:34 AM4/4/17
to DroidScript
I learn JS web browser API on the Mozilla's site but many opportunities is unavailable in DS subset of JS.

Dave Smart

unread,
Apr 4, 2017, 5:44:33 AM4/4/17
to DroidScript
You can use the "base64" option with the app.ReadFile() method to get the binary data as base64 text which can be sent to online servers.

Symbroson Development

unread,
Apr 4, 2017, 6:11:56 AM4/4/17
to DroidScript
The problem is that the server where I have to send the file expects the file data as described below and I cant change that.

Dave Smart

unread,
Apr 4, 2017, 6:27:20 AM4/4/17
to DroidScript
I think you will probably need to use the 'json' option on the GetPixelData method which will return an array of raw data values, you can then loop through the array and convert to the hex escaped string you need.

Symbroson Development

unread,
Apr 4, 2017, 6:30:52 AM4/4/17
to DroidScript
Isn't that a problem because the file header of the png file are missing?

Dave Smart

unread,
Apr 4, 2017, 6:32:02 AM4/4/17
to DroidScript
But that might not work because it looks like the other end is expecting png format.   What do you know about the other end?  What formats does it support?

Dave Smart

unread,
Apr 4, 2017, 6:37:03 AM4/4/17
to DroidScript
Look here:-  http://stackoverflow.com/questions/9405621/what-is-this-format

I think you could probably just get away with using the GetPixelData method with the 'pngbase64' option, then appending the data string to this header:-  data:image/png;base64,

Symbroson Development

unread,
Apr 4, 2017, 6:58:49 AM4/4/17
to DroidScript
I am writing a bot api for telegram and stuck on sending files

in python this is the code working:


In JS this is my current (not working) code:-

f=new FormData();
data=''; //the file content
f.append('filename','pic.png');
f.append('filecontent',data);

var httpRequest=new XMLHttpRequest();
httpRequest.onreadystatechange=function(){
if(httpRequest.readyState==4) {
with(httpRequest)
if(status!=200) app.ShowPopup("Error "+status+': '+responseText);
}
};
httpRequest.open('post','https://api.telegram.org/bot'+token+'/sendSticker',true);
// httpRequest.setRequestHeader("Content-Type","multipart/form-data; boundary=---------------------------275932272513031");
httpRequest.send(JSON.stringify({'chat_id':chat_id,'sticker':f})); //send data

Maybe that helps a bit...

Virtuos86

unread,
Apr 5, 2017, 12:21:02 AM4/5/17
to DroidScript
May be this info is useful: on https://developer.mozilla.org/en/docs/Web/API/FormData I find: "[2] XHR in Android 4.0 sends empty content for FormData with blob"

Dave Smart

unread,
Apr 5, 2017, 7:33:13 AM4/5/17
to DroidScript
Have you just tried using the app.UploadFile() method?

Symbroson Development

unread,
Apr 5, 2017, 9:24:40 AM4/5/17
to DroidScript
I have to send a JSON object with other data where to send the msg so that is no option

JustAnotherDude

unread,
Apr 6, 2017, 10:12:41 AM4/6/17
to DroidScript
Dave the app.UploadFile function are not working :3 we tested it on my server where i put a php file that are in the file "input"

the only way that are realy working is via html forms :3

Upload via ajax (jquery) not working (File was not sent to server)
UploadFile() not working (same as above)

Dave Smart

unread,
Apr 7, 2017, 9:09:42 PM4/7/17
to DroidScript
Alex,

If you send me the url of a server I can test with and a sample image to upload, then I might be able to get it working for you.  I think I might need to allow extra form fields to be added to the app.UploadFile() method.

Symbroson Development

unread,
Apr 8, 2017, 4:51:46 AM4/8/17
to DroidScript
I've sent it you privately because the bot token shouldn't be shared. Thanks a lot in advance :)

nikhil baby

unread,
Apr 8, 2017, 4:55:11 AM4/8/17
to DroidScript
I have worked a lot with app.UploadFile();
If the HTML upload work then app.UploadFile(); will also work!!

Please give the url, i can also check it out :)

Symbroson Development

unread,
Apr 8, 2017, 4:55:57 AM4/8/17
to DroidScript
And yes the form fields in uploadfile would be nice :)

Symbroson Development

unread,
Apr 8, 2017, 5:07:00 AM4/8/17
to DroidScript
Nikhil - you can create your bot with the telegram botfather if you also want to try. Thete type /newbot to create one. The token you'll get you have to paste in the token string
t.me/BotFather

I've attached the base code

botcode.txt

nikhil baby

unread,
Apr 8, 2017, 6:22:49 AM4/8/17
to DroidScript
sorry Alex,
the link you shared is not working for me :(

Symbroson Development

unread,
Apr 8, 2017, 6:31:34 AM4/8/17
to DroidScript
I think you need the app Telegram installed to use that
Reply all
Reply to author
Forward
0 new messages