简体   繁体   English

文件上传在localhost Express服务器上不起作用

[英]File upload not working on localhost express server

I'm trying to implement ng file upload in a node app I'm running on localhost. 我正在尝试在本地主机上运行的节点应用程序中实现ng文件上传 I'm going off the demo here but when i change the directory to download to 我要在这里进行演示但是当我更改目录以下载到

file.upload = Upload.upload({
                url: 'uploadImages',
                data: {file: file}
            });

I'm getting a 404: 我得到404:

angular.js:10765 POST http://localhost:8888/uploadImages/ 404 (Not Found) angular.js:10765 POST http:// localhost:8888 / uploadImages / 404(未找到)

Do I need to set up an express route for that directory? 我需要为该目录​​设置快速路由吗? I've attempted that, but it isn't working either with 我已经尝试过了,但是也不能用

app.post('/uploadImages', cors(corsOptions), function(req, res){
    res.sendfile('./uploadImages')
});

Not really sure where to go from here. 不太确定从这里去哪里。

Yes, you need to set up a web server like your Node Express server to accept the POST request. 是的,您需要设置一个Web服务器(例如Node Express服务器)以接受POST请求。 The way I have done this in the past was to use multer , an Express middleware for handling multi-part uploads. 过去我这样做的方法是使用multer ,这是一种Express中间件,用于处理多部分上传。

EXAMPLE

var express = require('express')
var multer = require('multer')

var app = express();

var upload = multer({
    dest: 'uploadImages/'
});

app.post('/uploadImages', upload.any(), function (req, res, next) {
  // req.files is the file uploaded, which multer will write to
  // the dest folder for you. req.body will contain the text fields,
  // if there were any.

  res.json(req.files.file);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM