尝试从服务器下载带有express的文件作为客户端 - >意外的令牌

时间:2017-09-26 13:32:33

标签: node.js express

目前我总是在同一个error中运行,我找不到任何解决方案。我试图从服务器下载文件作为客户端与快递。 这是代码服务器端:

// Set up
var nodemailer = require('nodemailer');
var express = require('express');
var multer = require('multer');
var app = express();                               
var mongoose = require('mongoose');                     
var morgan = require('morgan');             
var bodyParser = require('body-parser');    
var methodOverride = require('method-override'); 
var cors = require('cors');

var fs = require('fs');
var path = require('path');
var mkdirp = require('mkdirp');

mongoose.connect('mongodb://localhost:27017/Sharify');

app.use(morgan('dev'));                                         
app.use(bodyParser.urlencoded({ 'extended': 'true' }));            
app.use(bodyParser.json());                                     
app.use(bodyParser.json({ type: 'application/vnd.api+json' })); 
app.use(methodOverride());
app.use(cors());

app.use(function (req, res, next) {
    res.header("Access-Control-Allow-Origin", "*");
    res.header('Access-Control-Allow-Methods', 'DELETE, PUT, GET');
    res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
    next();
});

app.get('/api/download/:projectid/:filename', function (req, res) {
    res.download(__dirname + '/uploads/' + req.params.projectid + '/' + req.params.filename);    
});

Clientside它被调用:

downloadFile(file)
{
  return new Promise(resolve => {
       this.http.get('http://localhost:27017/api/download/' + file.projectID + "/" + file.fileName)
           .map(res => res.json())
           .subscribe(data => {
               resolve(this.data);
           });
   });        
}

我总是在开发者控制台中收到此错误:https://gyazo.com/552eb36def1eb7fab9ea12f0f181ff9e 有人知道解决方案吗?提前谢谢!

1 个答案:

答案 0 :(得分:0)

我不确定this.http.get()是什么,但如果它基于fetch(),您可能希望使用res.blob()代替res.json() (这仅适用于JSON响应,但您的是JPEG文件)。

您可能还必须解决data,而不是this.data

.subscribe(data => {
  resolve(data);
});