通过node js和Angular上传文件到multer。

问题描述 投票:0回答:1

我想上传一个文件到我的SQLite3数据库,我一直收到一个500错误,当我尝试下面的代码时,我的请求中甚至没有一个500错误,我还尝试使用formData,但我收到同样的错误,它看起来像文件甚至没有提出请求。

profile.component.html

<div *ngIf="!close" class="Modal-Background toggle-Modal">
                    <div class="Center-Block Absolute-Center is-Fixed is-Variable Modal" id="Fixed-Modal">
                      <div class="Center-Content">
                            <div class="title" > 
                                    <h3>Edit Profile</h3> <p class="x" (click)="closeWindow()">X</p></div> 

                            <form [formGroup]="edit"enctype="multipart/form-data">
                                    <input type="file" name="file" (change)="selectImage($event)" />
                                    <button (click)="submit()">Submit</button>
                                    </form>
                      </div>
                    </div>
                  </div>

      <div >

profile.component.ts


import { Component, OnInit } from '@angular/core';
import { NavbarComponent } from '../navbar/navbar.component';
import { ActivatedRoute, Router } from '@angular/router';
import { HttpClient } from '@angular/common/http';
import {FormControl, FormGroup,Validators} from '@angular/forms';

export class ProfileComponent implements OnInit {

  images;




  constructor(private http:HttpClient,private router:ActivatedRoute,private _router:Router) { }

  ngOnInit(){
...
}
 selectImage(event) {
    if (event.target.files.length > 0) {
      const file = event.target.files[0];
      this.images = file;
    }
  }
  submit(){
    const formData = new FormData();
    formData.append('file', this.images);
    console.log(formData)
    this.http.post<any>('http://localhost:5000/file', {images:this.images}).subscribe(
      (res) => console.log(res),
      (err) => console.log(err)
    );

data-router.js


const express = require('express');

const data = require('./data-model')

const router = express.Router();
const bcrypt = require("bcryptjs")
const jwt = require("jsonwebtoken")
const multer = require("multer")

// router.use(cors({ origin: "*" }));
// router.use(bodyParser.json());

var store = multer.diskStorage({
  destination:function(req,file,cb){
      cb(null, './uploads');
  },
  filename:function(req,file,cb){
      cb(null, Date.now()+'.'+file.originalname);
  }
});


var upload = multer({storage:store})

router.post('/file', upload.single('file'), (req, res, next) => {
  // console.log(req.body);
  const file = req.file;
  console.log(file.filename);
  if (!file) {
    const error = new Error('No File')
    error.httpStatusCode = 400
    return next(error)
  }
    res.send(file);
})

javascript node.js angular express multer
1个回答
0
投票

const express = require('express');
const router = express.Router();
const multer = require("multer")

const upload = multer({
    dest: './uploads',

})

router.post('/file', upload.single('file'), (req, res, next) => {
    // console.log(req.body);
    const file = req.file;
    console.log(file.filename);
    if (!file) {
        const error = new Error('No File')
        error.httpStatusCode = 400
        return next(error)
    }
    res.send(file);
})

Multer存储配置有误

© www.soinside.com 2019 - 2024. All rights reserved.