Submit a form using post how to get picture data and other text parameters [NodeJS]

Source: Internet
Author: User

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]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.