XMLHttpRequest, FormData and sending binary file via web form

688 views
Skip to first unread message

Virtuos86

unread,
Sep 20, 2016, 3:43:23 AM9/20/16
to DroidScript
Hi!
I have a web form:
<form method="POST" action="/cgi-bin/login/page.pl" enctype="multipart/form-data">
<input type="hidden" name="block" value="save">
<input type="hidden" name="file" value="">
<input type="hidden" name="code" value="8338958023">
<div class="margins">
<b><input type="text" name="title" value="" size="62" maxlength="100" ID="textheader"></b><br>
<textarea rows="25" name="text" cols="60" wrap="virtual" ID="textform"></textarea></font>
<script>document.forms[0].title.focus()</script>
</div>

<div ID="textlink">
Add image:
<input type="file" name="photo" size="32" ID="butt">
&nbsp;(
<a href="/about/pics.html" target="_blank">about illustrations</a>)
</div>

<div class="margins">
<input type="submit" value="Send post">
</div>
</form>
I fill this form and send it:

formData
.append( "block", "save" );


Sending form goes succesful, but image is not uploaded.

Virtuos86

unread,
Sep 20, 2016, 3:50:20 AM9/20/16
to DroidScript
I fill this form and send it:
const XHR = new XMLHttpRequest();
XHR.open( 'POST', "http://www.***.ru/cgi-bin/login/page.pl", false );
var formData = new FormData();
formData.append( "block", "save" );
formData.append( "file", "" );
formData.append( "code", "8338958023" );
formData.append( "title", "Title" );
formData.append( "text", "Text." );
formData.append( "photo", new Blob([app.ReadFile( "image.jpg" )], { "type": "image/jpeg" }) );
XHR.send( formData );
var data = XHR.responseText;

Chris

unread,
Sep 20, 2016, 9:07:43 AM9/20/16
to DroidScript
app,Readfile does not load 'binary', so it actually is returning a null or '' for your image, that is why it is not there.

Dave added a 'binary' option to ReadFile but it is in beta, or maybe still alpha, I do not remember. I would suggest contacting him to be a beta member.

Chris

Virtuos86

unread,
Sep 20, 2016, 9:16:43 AM9/20/16
to DroidScript
Thank you, Chris. Unfortunately conscience does not allow me to become a beta tester just to get the coveted opportunity. I'll wait for the future release of a new version.

JustAnotherDude

unread,
Sep 20, 2016, 3:07:51 PM9/20/16
to DroidScript
does ot upload zip files? (or only Ascii?) :3

sorry i'm not realy good in english today xD

Virtuos86

unread,
Sep 21, 2016, 2:53:07 AM9/21/16
to DroidScript
Any non-text file is binary.

JustAnotherDude

unread,
Sep 21, 2016, 12:06:05 PM9/21/16
to DroidScript
and why not rename Name.zip to Name.txt and then upload?^^

Chris

unread,
Sep 21, 2016, 7:59:31 PM9/21/16
to DroidScript
Because the file extension does not dictate the contents. You can't make a binary file contain text by simply renaming.

There are a few ways to handle this, but i'm not reinventing the wheel. The beta has a binary option for ReadFile and makes this sort of thing easy.

Chris

Reply all
Reply to author
Forward
0 new messages