Node file Upload, ZIP

Source: Internet
Author: User

Upload file:

Many people use the third package for file uploads, such as formidable.

I have also studied it, but when used with the express3.x framework, it is found that uploaded files are always missing. The result is that the following sentence causes:

App.use (Express.bodyparser ({' uploaddir:xxx '}));

The node-side upload file will temporarily save the uploaded file to a directory, and then through our code to move the file to our designated directory;

Because the default upload temp directory for express3.x is inconsistent with the temporary upload directory of formidable, formidable has been unable to find the uploaded file.

In addition, formidable also has a bug, can refer to https://cnodejs.org/topic/4f5c62932373009b5c0b027b.

The following is not through the third party package, express the original way to upload files:

1. Front desk writing, very simple, form form submission. Note the Name property of the input file control, which is associated with the background.

<formAction= "Background Routing"enctype= "Multipart/form-data"Method= "POST">    <inputtype= "File"name= "upload"multiple= "multiple"><BR>    <inputtype= "Submit"value= "Upload"></form>

2. Background notation

App.post ('/upload ',function(req, res, next) {//Req.files is the upload file object, Req.files.upload here upload is your foreground input file control's Name property value    //get the type of upload file (please refer to MIME for each type of upload file)    varFile_type =Req.files.upload.type; //determine the upload type, where only the type of the image can be uploaded    if(File_type! = "Image/png" && file_type! = "Image/jpeg" && file_type! = "Image/bmp" &&File_type! = "Image/gif") {        returnRes.jsonp ({success:0, msg: ' FileType is Error ' }); }    //Temporary file path    varTempPath =Req.files.upload.path; //The temporary path is also available, down to the movement of files through FS, renaming, and deleting temporary files. It's not much to write again.})

Compression and decompression

Compression Package module: EASY-ZIP2 Unpacking module: Unzip

At that time to find this compression package module cost a great effort, such as Adm-zip and so on, although many stars, but measured down more or less have problems.

Directly on the code:

varEasyzip = require (' EASY-ZIP2 '));/** * Compressed package (only compressed folder) * @param folder zipped path * @param zipname Compressed package path (with zip suffix) * @param callback callback*/varZip =function(folder, Zipname, callback) {callback= callback? Callback:function () {    }; varZip =NewEasyzip.    Easyzip (); //To add a compressed folderZip.zipfolder (folder,function(err) {if(ERR)returnCallback ("Folder is not find",NULL); //Compress FilesZip.writetofile (Zipname,function(err) {if(ERR)returnCallback (Err,NULL); Callback (NULL, ' OK ');    }); });}

Above I was compressed the entire folder, of course, can also compress files, specific please find out their own learning.

varunzips = require (' unzip '));/** * Unpack Package * @param zippath Compressed package path * @param unzippath decompression Path * @param callback callback*/functionUnzip (Zippath, Unzippath, callback) {callback= callback? Callback:function () {    }; //determine if a compressed package existsFs.exists (Zippath,function(exists) {if(!exists)returnCallback (' Zippath is not esists ',NULL); //unzip in a stream way        varUnzipextractor =unzips.        Extract ({path:unzippath}); //adding error Listener EventsUnzipextractor.on (' Error ',function(Err) {callback (Err,NULL);        }); //adding complete listener eventsUnzipextractor.on (' Close ',function() {Callback (NULL, ' OK ');        }); Fs.createreadstream (Zippath). On (' Error ',function(Err) {callback (Err,NULL);    }). pipe (Unzipextractor); });}

HTTP Header

Recently there is a need to download the zip file on the browser side.

So this function is realized through stream and pipe.

But 2 questions were encountered:

1.ie, Firefox and other browser downloaded files do not have a suffix, you must manually add the suffix after downloading.

2. The file name of the download is wrong, all is download, not the actual my compressed package files.

After the big God point, it is to return to set the header information, specify the MIME type of the file, and the file name only line.

The following is an example of a ZIP file:

// set MIME type res.setheader (' Content-type ', ' Application/zip '); // Set file name res.setheader (' content-disposition ', ' attachment;filename= ' + zipname + '. zip ');
Fs.createreadstream (' xxx '). pipe (RES);

Node file Upload, ZIP

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.