How to access <input type=file> contents?
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 i开发者_如何学Cnput contents and how to forge POST request with file inputs.
Does anybody know, is it possible?
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.
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.
精彩评论