In the work encountered such a demand, need to use Nodejs to upload files, before also just know how to upload files through the browser, with nodejs words, the equivalent of analog browser behavior. Google after a while, understand that the browser is nothing more than the use of HTTP protocol to the server to transmit data, the specific agreement is "RFC 1867-form-based file Upload in HTML", in the browser through form forms to upload files is through this agreement, We can first look at the browser to the server to send what data, you can according to gourd painting scoop of the upload function to achieve. Speaking of form form upload file, you should be very familiar with:
<form action= "http://www.qq.com/" method= "POST" >
<input type= "text" name= "Text1"/><br
/> <input type= "text" name= "Text2"/><br/> <input type=
"Submit"/>
When submitting, you can see this data being sent to the server with the Fiddler grab:
POST http://www.qq.com/HTTP/1.1
Host:www.qq.com
Content-length:23
content-type:application/x-www-form-urlencoded; Charset=utf-8
Text1=hello&text2=world
It is worth noting that Content-type defaults to application/x-www-form-urlencoded, so messages are encoded by URL. For example, "Hello" will be encoded as%E4%BD%A0%E5%A5%BD.
Let's take a look at how to upload through form forms. We should also be familiar:
<form action= "http://www.qq.com" method= "post" enctype= "Multipart/form-data" >
<input type= "file" name= "MyFile"/>
<input type= "Submit" value= "Submit"/>
</form>
Then create a new upload.txt text file that only has the word Hello world, and we'll eat it with fiddler to catch the packet, and we can find that sending the data from the past is a little bit more complicated (has removed a lot of other okay requests, such as cache control and cookies, etc.):
POST http://www.qq.com/HTTP/1.1
Host:www.qq.com
content-length:199
Content-type:multipart/form-data; boundary=----webkitformboundarywr3x7sxbyqq4zf5g
------webkitformboundarywr3x7sxbyqq4zf5g
Content-disposition:form-data; Name= "MyFile"; Filename= "Upload.txt"
Content-type:text/plain
Hello World
------webkitformboundarywr3x7sxbyqq4zf5g--
Based on the definition of RFC 1867, we need to generate a piece of boundary data that cannot be found elsewhere in the content, which can be defined by itself, and the generation algorithms in each browser may be different, with the above boundary separating the data and creating a separate data You can put the separate data in the head of the content-type inside the delivery to the service side, that is, the above content-type:multipart/form-data; boundary=----webkitformboundarywr3x7sxbyqq4zf5g, in addition, the uploaded content needs to be separated into several segments with separate data, and then each piece of data has a file name, and the name of the upload, The server is using this name to receive the file, and the file type Content-type, in this case is text/plain, if the upload is PNG picture is image/png. The file type of a blank line is the content of the uploaded file, in this example is also in order to easily understand the text file so that the content can be directly displayed, if the upload is a picture file, because it is binary files, fiddler will show is garbled. The contents of the file are followed by a blank line plus boundary data.
After understanding the details of the send format, the next step is to use Nodejs to program the implementation, in simple terms, it is in accordance with the format of the data sent to the service side on the line.
Const HTTP = require (' http '); const FS = require (' FS ');//post address is a local service PHP that tests whether the upload was successful var option s = {hostname: ' localhost ', port:80, Path: '/get.php ', Method: ' POST '}//Generate delimited data var Boundarykey = '----Webkitformbounda
Ryjlvkbqxtii0ygpab '; Read the contents of the file to be uploaded fs.readfile ('./upload.txt ', function (err, data) {//assemble separate segment var payload = '-' + Boundarykey + ' \ r \ n ' + ' Co Ntent-disposition:form-data; Name= "MyFile";
Filename= "Upload.txt" \ r \ n ' + ' content-type:text/plain\r\n\r\n ';
Payload + = data;
Payload + = ' \r\n--' + Boundarykey + '--'; Send request var req = http.request (options, function (res) {res.setencoding (' utf8 '); Res.on (' Data ', function (chunk) {Console
. log (' body: ' + chunk);
});
});
Req.on (' Error ', function (e) {console.error ("error:" +e);});
The boundary, the size of the data to be sent, and the data itself are written into the request Req.setheader (' Content-type ', ' multipart/form-data; boundary= ' +boundarykey+ ');
Req.setheader (' Content-length ', buffer.bytelength (payload, ' UTF8 '));
Req.write (payload);
Req.end (); });
This article focuses on understanding the protocol and implementing it in code, and there are a lot of optimizations on the code organization.
Finally, in local Apache, simply write a php to save uploaded files to use as a test:
<?php
$filePath = './upload.txt ';
Move_uploaded_file ($_files[' myfile '] [' tmp_name '], $filePath);
echo "OK";
? >
In addition, according to RFC 1867 can also achieve the ability to upload multiple files at a time, this is not detailed here, if necessary, can be detailed reference to the RFC 1867来 implementation.
The above is a small set to introduce the Node.js implementation of file upload, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!