Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

Image and other field save using Multiparty

◉ server/lib/util/index.js ===>
    const uploadImage =   (file, imgpath, callback)=> {
      if( file.originalFilename=="" ){
          callback(null, { "nofile":true } );
      }else{

          var filePath = file.path;
          var params = {
              Bucket: process.env.AWS_S3_BUCKET,
              Body: fs.createReadStream(filePath),
              Key: imgpath + "/" + Date.now() + "_" + path.basename(filePath),
              ContentType: file.headers['content-type'],
              ACL: 'public-read'
          };

          return new Promise(  (resolve,reject)=>{
              s3.upload(params, function (err, data) {
                  if (err) {
                      reject(err);
                  } else {
                      resolve(data);
                  }
              });
          });
      }
  };
----------------------------------------------------------------------------
◉ server/src/api/routes/auth/authController.js ===>
  
  const multiparty = require('multiparty');
  const { uploadImage } = require('../../../../lib/util');



  async updateProfile(req, res, next) {
          console.log('===='+req.user)
          try {
              let user = await User.findOne({ _id: req.user._id });
              if (user) {
                  let form = new multiparty.Form()
                  form.parse(req, async function(err, fields, files) {
                      let fileupload = files.image[0];
                      console.log('===fields==='+JSON.stringify(fields)+'====')
                      console.log('===files==='+JSON.stringify(files)+'====')


                      _.forOwn( fields,(field,key)=>{
                          user[key] = field[0];
                      })
                      try{
                          console.log(fileupload)

                          let image = await uploadImage( fileupload,'users' );
                          console.log('======fileupload===='+image+"===")
                        // here image and user_name save in schema but user name not display here bqz 
                        // above _.forown(){} method automatic take other field like user_name  
                          user['image'] = image.key;   
                          console.log(user)
                          await user.save();
                          return res.success({language: req.headers['accept-language']},req.__('PROFILE_UPDATED'));
                          }catch(err){
                              return next(err);
                          }
                  }); 
              } else {
                  return res.warn('', req.__('USER_NOT_FOUND'));
              }
          } catch (err) {
              return next(err);
          }
      }
Comment

PREVIOUS NEXT
Code Example
Javascript :: how to create hexadecimal encoded files in javascript 
Javascript :: react birthday 
Javascript :: In JavaScript, all numbers are stored in the format float64 
Javascript :: Viewport ch. 
Javascript :: javascript copy input value to clipboard on click 
Javascript :: jquery select convert into input text 
Javascript :: extract rar file nodejs 
Javascript :: npm i react-use-navigator-permissions 
Javascript :: reinitialise or reset all values in mapping in solidity 
Javascript :: this in js class method 
Javascript :: Javascript index,length,push,pop,shift,unshift 
Javascript :: React Rendering Movies 
Javascript :: how to get the value of state of on and off 
Javascript :: Fibonacci perticular position in javascript 
Javascript :: js to jquery converter online 
Javascript :: angular refresh component on button click 
Javascript :: ver versao do sublime text 
Javascript :: template literals multiline js 
Javascript :: tictactoe - javascript 
Javascript :: build class component react 
Javascript :: toast not at bottom 
Javascript :: Zoho Creator Javascript Loop through more than 200 records 
Javascript :: useLinkPressHandler 
Javascript :: create immutable array in javascript 
Javascript :: how to get min value from array of objects in javascript 
Javascript :: print each word in a string javascript 
Javascript :: <Link> react import 
Javascript :: React Tools - Suspense 
Javascript :: array of function 
Javascript :: my code agly because alot of if and else dev community 
ADD CONTENT
Topic
Content
Source link
Name
5+5 =