When posting a form that contains pictures, such as uploading a picture, you need to add the parameter enctype= "Multipart/form-data" in the <form> field, indicating that the data is transferred in binary mode. If the form contains other text parameters, such as user name username, you cannot get the post parameters in the usual way, such as:
<HTML> <Head> <Metahttp-equiv= "Content-type"content= "text/html"CharSet= "UTF-8" /> </Head> <Body> <formAction= "/upload"enctype= "Multipart/form-data"Method= "POST"> <inputtype= "File"name= "Upload" /> <inputtype= "text"name= "username"value= "123"/> <inputtype= "Submit"value= "OK" /> </form> </Body></HTML>
var postdata = ""; Request.addlistener ("data",function(postchunk) { + = postchunk; }) ; // post End output result Request.addlistener ("End",function() { console.log (postdata); var params = querystring.parse (postdata); Console.log (params["username"]); });
The console will display the undefined, not the 123 we expected. Because the entire form is transmitting data in binary, request obtains the value of the Username field.
In this case, you need to introduce a third-party formidable module:
var New Formidable. Incomingform (); = Uploadimgdir; function (Error, fields, files) { Console.log ("parsing done"); Console.log (Files.upload.path); Console.log (fields["username"]); + "Name.png"); });
In the call formidable. After Incomingform's Parse method, its callback returns three parameters, respectively, the exception, the text parameter object, and the picture file data.
Use fields["username" in the callback to get the username parameter normally.
Submit a form using post how to get picture data and other text parameters [NodeJS]