欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

node中間層實(shí)現(xiàn)文件上傳功能

 更新時(shí)間:2018年06月11日 08:21:53   作者:憐白  
這篇文章主要介紹了node中間層實(shí)現(xiàn)文件上傳功能,本文給大家介紹的非常詳細(xì),需要的朋友可以參考下

一般情況下,前端的文件上傳一般都是通過(guò)form表單的(<input type="file" />)來(lái)完成文件的上傳,如果使用node中間層完成跨域,文件的上傳就需要在node中間層處理成可讀流,轉(zhuǎn)成formData完成轉(zhuǎn)發(fā)。

一、form表單文件上傳

  這是最常見(jiàn)的文件上傳方式,通過(guò)form表單實(shí)現(xiàn),簡(jiǎn)單實(shí)用,設(shè)置一下method、enctype、action屬性就可以了,多文件上傳需要設(shè)置multiple屬性(部分瀏覽器支持還是有些問(wèn)題的)。

<form method="post" enctype="multipart/form-data" action="/api/upload">
  <input type="text" name="username">
  <input type="password" name="password">
  <input type="file" name="file">
  <input type="submit">
</form>

二、FormData實(shí)現(xiàn)文件上傳

  FormData對(duì)象用以將數(shù)據(jù)編譯成鍵值對(duì),以便向后臺(tái)發(fā)送數(shù)據(jù)。其主要用于發(fā)送表單數(shù)據(jù),但亦可用于發(fā)送帶鍵數(shù)據(jù)(keyed data),而獨(dú)立于表單使用。對(duì)部分瀏覽器對(duì)multiple屬性不支持的情況,可以使用formData的提交方式完成。

<!-- 獲取上傳文件轉(zhuǎn)成formData類型的文件 -->
<input multiple id="file" name="file" type="file" />
<button id="btn">提交</button>

const oFile = document.getElementById('file')
const oBtn = document.getElementById('btn')

oBtn.addEventListener('click', () => {
  files = oFile.files
  const formData = new FormData()
  formData.append('file', files[0])
  formData.append('file1', files[1])

  fetch('/api/upload', {
    method: "POST",
    body: formData
  })
})

使用fetch請(qǐng)求不要設(shè)置Content-Type,否則無(wú)法請(qǐng)求

fetch請(qǐng)求默認(rèn)是不帶cookie 

三、node中間層完成文件上傳跨域

  跨域是因?yàn)闉g覽器的同源策略造成,跨域的方法有很多中,這里使用的是node中間層代理完成(服務(wù)端之間的請(qǐng)求是不存在跨域問(wèn)題)。

  node無(wú)法直接解析上傳的文件,需要引入拓展包c(diǎn)onnect-multiparty完成,這樣就可以拿到文件數(shù)據(jù)。

  拿到上傳文件,需要在node中轉(zhuǎn)發(fā)請(qǐng)求后臺(tái)server,這里的文件不能直接發(fā)給后臺(tái),需要將上傳的文件使用fs.createReadStream轉(zhuǎn)成可讀流,同時(shí)引入 form-data 包(node環(huán)境是沒(méi)有formData對(duì)象的),這樣就可以實(shí)現(xiàn)node中間層轉(zhuǎn)發(fā)文件類型

  node部分代碼:

const fs = require('fs')
const path = require('path')
const FormData = require('form-data')
const express = require('express')
const fetch = require('node-fetch')
const router = express.Router()
const multipart = require('connect-multiparty');
var multipartMiddleware = multipart()

router.post('/upload', multipartMiddleware, function (req, res) {
  // console.log(req.body, req.files);

  const { path: filePath, originalFilename } = req.files.file
  const newPath = path.join(path.dirname(filePath), originalFilename)

  fs.rename(filePath, newPath, function (err) {
    if (err) {
      return;
    }
    else {
      const file = fs.createReadStream(newPath)
      const form = new FormData()
      form.append('file', file)

      fetch('http://localhost:8080/upload', {
        method: "POST",
        body: form
      })
    }
  })
  res.json({})
});

module.exports = router;

注意:

  • node無(wú)法直接解析上傳文件,需要引入npm包c(diǎn)onnect-multiparty中間件,或者引入npm包multiparty
  • node拿到文件,需要使用fs.createReadStream轉(zhuǎn)成可讀流
  • node環(huán)境沒(méi)有formData對(duì)象,需要引入npm包form-data
  • fetch請(qǐng)求提交formData數(shù)據(jù),不能設(shè)置Comtemt-Type

最后給大家附上完整的代碼: node中間層實(shí)現(xiàn)文件上傳

總結(jié)

以上所述是小編給大家介紹的node中間層實(shí)現(xiàn)文件上傳功能,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!

相關(guān)文章

最新評(píng)論