Angularjs+nodejs image Upload

Source: Internet
Author: User

1. Plugins

(1): Angular plug-in ng-file-upload

(2): Nodejs plug-in Connect-multiparty

2.html

<input type= "file" Ngf-select ng-model= "Files" ngf-multiple= "true"/>

3. Configuration

= Angular.module (' fileUpload', [' Ngfileupload']);   
can refer to Https://github.com/danialfarid/ng-file-upload
1 //inject angular File upload directives and services.2 var app = Angular.module (' fileUpload ', [' ngfileupload ']);3 4 app.controller (' Myctrl ', [' $scope ', ' Upload ', function ($scope, Upload) {5 $scope. $watch (' Files ', function () {6 $scope. Upload ($scope. files);7     });8 //Set default directive values9 //Upload.setdefaults ({ngf-keep:false ngf-accept: ' image/* ', ...});Ten $scope. Upload = function (files) { One if (Files && files.length) { Afor (var i = 0; i<Files. length; i++) { - var file= Files[i]; -                 Upload.upload ({ the url: ' URL ',//URL here is the route to the NODEJS request for the back table - Fields : {' username ': $scope. Username}, - File:file - }). Progress (function (evt) { + var progresspercentage= parseint (100.0* evt.loaded/evt.total); - Console.log (' Progress: ' + progresspercentage + '% ' + evt.config.file.name); + }). Success (function (data, status, headers, config) { A console.log (' file ' + config.file.name + ' uploaded. Response: ' + data '; at }). Error (Function (data, status, headers, config) { - console.log (' Error status: ' + status '); -                 }) -             } -         } -     }; in }]);

4.nodejs

Data received in the background is present in the temporary folder. Therefore, using Fs.rename () will be able to make an error. (My side is really error)

1 var multipart = require (' Connect-multiparty ');2 var fs = require (' FS ');3 var multipartmiddleware = multipart ();4 5 6 7 8 9 TenHere '/url ' should be consistent with the route sent by angular, One router.post ('/url ', multipartmiddleware,function (req, res, next) { A          - var profile_image = req.files.file; - var tmp_path = Profile_image.path;   Here is the address of the page picture, under the Temp folder in the C drive.  the Console.log (req.files); - var path = './url ' + profile_image.name; Here './url ' is the address you wish to store for the uploaded image. Can be an absolute address -            - //cross-domain transfer of files + var is = Fs.createreadstream (Tmp_path); - var os = fs.createwritestream (path); + is.pipe (OS); A Is.on (' End ', function () { at Fs.unlinksync (tmp_path); -                 }); - //cross-domain transfer of files ------------Here you can write the data back to the foreground--------is incomplete and cannot be copied completely. If you write it all, it's too much trouble. -});

From the beginning did not understand, to the back gradually filled the pit, spending too much time. Inside the pit really many, can walk through once, look back again, the code amount is so point. Write about the mistakes I made.

1. Plugin version error. The previous plug-in seems to be called angular-file-upload. I take the ng-file-upload, but use the angular-file-upload.

2.angular-file-upload needs to load ' $upload ' in the Congtroller used, while ng-file-upload loads ' upload '.

The name of the 3.angular-file-upload-loaded middleware is [' angularfileupload '], and ng-file-upload loads [' ngfileupload ']

4.nodejs cannot receive req.files.file. One reason is that there is a problem with the Nodejs plugin selection, and another reason is that the plugin has a problem with its writing. Individuals tend to use connect-multiparty.

5.nodejs fs.rename () method error. Guess: Because the uploaded image exists in the temporary folder in the C drive, the problem of system permissions may be caused by the Fs.rename () method error. So, a way to read files across domains is searched from the Internet.

Angularjs+nodejs image Upload

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.