Passing data from server/app.js to controller Angular-fullstack + multer

535 views Asked by At

I'm struggling to get a filename passed from server/app.js to a controller in client/app/

I am using Multer to deal with the file upload which is working fine but i need to pass the filename back to the client to display it in the next view.

Here is the code i have:

server/app.js

app.use(multer({ dest: 'client/assets/uploads',
 rename: function (fieldname, filename) {
    return filename+Date.now();
  },
    onFileUploadStart: function (file) {
      console.log(file.originalname + ' is starting ...')
    },
    onFileUploadComplete: function (file) {
      console.log(file.fieldname + ' uploaded to  ' + file.path)
      done=true;
    }
}));


app.post('/api/photo',function(req,res){
  if(done==true){
    photoName = req.files.userPhoto.name;
    res.json(photoName);
  }
});

It's showing the new filename but i need it to be in a usable form and as i'm doing all this server side i have nothing in my controller to handle the incoming response.

Any help would be greatly appreciated.

1

There are 1 answers

0
Marvin Speakman On

OK so after battling with this all day i've got a dirty fix that works so i thought i'd share it with you, but there really must be a cleaner, more elegant way of doing this:

app.post('/api/photo',function(req,res){
  if(done==true){
    photoName = req.files.userPhoto.name;
    photoName = encodeURIComponent(photoName);
    res.redirect('../course?img=' + photoName);
  }
});

Just send the data i need in a querystring and grab it client side.

If anyone can improve on this please post.

cheers.