2

I have two forms named form1,form2. One form with textfields, another with file inputs. I need to forge a POST request to the serverside with contents of textfields and file inputs. Reading contents of textfields is easy. But I cannot find out how to read file input contents and how to forge POST request with file inputs.

Does anybody know, is it possible?

Paul D. Waite
  • 96,640
  • 56
  • 199
  • 270
Sergey Filkin
  • 475
  • 1
  • 6
  • 10

2 Answers2

0

If you will be doing this via an xmlHttpRequest (Ajax) you need to declare the header as multipart/form-data. I believe it would look something like this if you already have a request in the variable xmlhttp.

xmlhttp.open("POST", "file.php", true);
xmlhttp.setRequestHeader("Content-type","multipart/form-data");
xmlhttp.send(queryString());

In this example you would build the query string with paired values in the function queryString() so that the output of the function looks like name=value&name2=value2. I'm not positive but it shouldn't matter that you're sending file data. I'm sure someone will correct me if I'm wrong on that but you can at least try this.

Robert
  • 69
  • 8
  • After reading some of the comments on your question I realized I may have assumed that you were using PHP (and Ajax) so disregard this if that guess was wrong. – Robert Sep 09 '11 at 06:04
  • I know how to use Ajax. I need to know how to access contents of file inputs. – Sergey Filkin Sep 09 '11 at 11:40
  • `document.forms["formname"]["inputname"].value`? Is it not the same code as any other input type? Perhaps this page will help. Edit: http://www.openjs.com/articles/ajax/ajax_file_upload/ – Robert Sep 09 '11 at 16:47
0

Are you trying to send the file using XMLHttpRequest? If so, you will need to use XMLHttpRequest2, the second version of XMLHttpRequest, which now supports uploading binary files from the user's computer. There's a guide at HTML5 Rocks about it.

kirb
  • 2,033
  • 1
  • 18
  • 28