美文网首页
NodeJS 文件上传

NodeJS 文件上传

作者: 不知道的是 | 来源:发表于2019-01-10 10:20 被阅读0次

keywords

  1. multipart/form-data
  2. HTML entity

各依赖功能描述

  • formidable
    A Node.js module for parsing form data, especially file uploads.

  • html-entities
    Fast html entities library.

upload_file.gif

files.filetoupload.name 如果用户上传的文件名称中包含汉字等字符则会被转换为 HTML 字符实体(在做上传文件永久存储的时候通过 html-entities 将 HTML entity 转换回汉字即可)

image.png
// app.js
const http = require('http')
const formidable = require('formidable')
const fs = require('fs')
// https://www.npmjs.com/package/html-entities
const Entities = require('html-entities').XmlEntities
const entities = new Entities()
const port = 8080

http.createServer((req, res) => {
  if (req.url == '/fileupload') {
    const form = new formidable.IncomingForm()
    form.parse(req, (err, fields, files) => {
      // console.log(files)
      const oldpath = files.filetoupload.path
      const newpath = 'C:/Users/lulu/' + entities.decode(files.filetoupload.name)
      fs.rename(oldpath, newpath, err => {
        if (err) throw err
        res.write('File uploaded and moved!')
        res.end()
      })
    })
  } else {
    res.writeHead(200, { 'Content-Type': 'text/html' })
    res.write('<form action="fileupload" method="post" enctype="multipart/form-data">')
    res.write('<input type="file" name="filetoupload"><br>')
    res.write('<input type="submit">')
    res.write('</form>')
    return res.end()
  }
}).listen(port, () => {
  console.log(`http://localhost:${port}`)
})

HTML entity encode/decode

HTML entity encode_decode.gif

https://www.w3schools.com/nodejs/nodejs_uploadfiles.asp
https://mothereff.in/html-entities
https://www.npmjs.com/package/html-entities
https://blog.teamtreehouse.com/uploading-files-ajax
https://www.npmjs.com/package/formidable

相关文章

  • 前端干货链接-持续更新

    前端文件上传 拖拽上传、图片预览、文件上传nodejs-process-excel :nodejs解析或导出exc...

  • 求解一个nodeJs 的文件访问问题

    开发环境 后端:nodeJs 前端:Vue 需求:视频文件上传,视频预览 问题:上传已经可以实现,存在nodeJs...

  • Nodejs 文件上传

    node js 接收表单数据原理 使用 formidable 上传文件

  • NodeJS文件上传

    使用multer上传文件,默认不会给文件添加扩展名 一、package.json 二、app.js 三、uploa...

  • nodejs 文件上传

    图片文件上传、在调试的时候 使用 multer 一直报错,所以最后选择使用 connect-multiparty ...

  • NodeJS 文件上传

    keywords multipart/form-data HTML entity 各依赖功能描述 formidab...

  • ftp 上传模式

    在使用 nodejs 实现 ftp 文件上传的过程中,我发现文本文件可以正常上传,但是图片文件则不行。图片被上传后...

  • node安装(linux)

    1、下载 https://nodejs.org/zh-cn/download/ 2、上传 rz上传文件 3、解压 ...

  • nodejs 实现文件上传

    文件上传需要解析文件,之前我们解析数据使用body-parser,解析文件可以用multer。body-parse...

  • nodejs 实现文件上传

    前端页面结构 node 服务端 需用到connect-multiparty 模块 npm install conn...

网友评论

      本文标题:NodeJS 文件上传

      本文链接:https://www.haomeiwen.com/subject/ymeorqtx.html