danialfarid / ng-file-upload

Lightweight Angular directive to upload files with optional FileAPI shim for cross browser support
MIT License
7.87k stars 1.59k forks source link

How preview image downloading from node server? #2032

Open joweste opened 6 years ago

joweste commented 6 years ago

Hi, I use a third plugin on node server to download image.

I am trying preview the image downloaded from server, but I get an error:

Error:[$parse:lexerr] Lexer Error: Unexpected next character at columns 38-38 [&] in expression [/file_manager/download?action=download&path=%2Fbaba%2FDSC01783.JPG].

What am doing wrong?

//angular controller $scope.imageSrc='/file_manager/download?action=download&path=%2Fbaba%2FDSC01783.JPG'

//markup

<div class="preview">
          <img ngf-src="{{imageSrc}}">
</div>

//node server

routes.get('/download', function (req, res, next) {

  var filePath = path.join(pathResolver.baseDir(req), pathResolver.pathGuard(req.query.path));
  var fileName = path.basename(filePath);
  var promise;
  //console.log('filepath',filePath);
  promise = fs.statAsync(filePath);

  promise = promise.then(function(stat) {

    if(!stat.isFile()) {
      //console.log("Cannot access file " + filePath + " (or is no file)");
      throw new Error("Cannot access file " + filePath + " (or is no file)");
    }

    var mimeType = mime.lookup(filePath);
    //console.log('mimetype: ' + mimeType);

    res.setHeader('Content-disposition', 'attachment; filename=' + encodeURIComponent(fileName));
    res.setHeader('Content-type', mimeType);

    var filestream = fs.createReadStream(filePath);
    filestream.pipe(res);
  });

  promise = promise.catch(function(err,stats) {
    res.status(500);
    res.send({
      "result": {
        "success": false,
        "error": JSON.stringify(err),
        "stats": stats
      }
    });
  });

  return promise;
});