Node使用multiparty包上传文件
http方式
var multiparty = require('multiparty');
var http = require('http');
var util = require('util');
var fs = require("fs");
http.createServer(function(req, res) {
if (req.url === '/upload' && req.method === 'POST') {
// 解析一个文件上传
var form = new multiparty.Form();
//设置编辑
form.encoding = 'utf-8';
//设置文件存储路径
form.uploadDir = "uploads/images/";
//设置单文件大小限制
form.maxFilesSize = 2 * 1024 * 1024;
//form.maxFields = 1000; 设置所以文件的大小总和
form.parse(req, function(err, fields, files) {
var fileOne = files.upload[0]
/*console.log(files);
console.log(fileOne.originalFilename);
console.log(fileOne.path);*/
//同步重命名文件名
//fs.renameSync(fileOne.path,form.uploadDir + fileOne.originalFilename);
res.writeHead(200, {'content-type': 'text/plain'});
res.write('received upload:\n\n');
res.end(util.inspect({fields: fields, files: files}));
});
return;
}
// show a file upload form
res.writeHead(200, {'content-type': 'text/html'});
res.end(
'<form action="/upload" enctype="multipart/form-data" method="post">'+
'<input type="text" name="title"><br>'+
'<input type="file" name="upload" multiple="multiple"><br>'+
'<input type="submit" value="Upload">'+
'</form>'
);
}).listen(8080);
express方式
var multiparty = require('multiparty');
var http = require('http');
var util = require('util');
var fs = require("fs");
var express = require('express')
var path = require('path')
var app = express()
app.use('/qushan/', express.static(path.join(__dirname, 'uploads')))
var server = app.listen(3002, function(){
console.log('server is listening...')
})
app.get('/qushan',function(req,res){
res.send(
'<form id="myForm" action="/qushan/upload" enctype="multipart/form-data" method="post">'+
'<input type="text" name="title"><br>'+
'<input type="file" name="upload" multiple="multiple" onchange="ajaxUpload()"><br>'+
'<div id="uploadImg"><img src=""></div>'+
'<a onclick="ajaxUpload()">ajax</a>'+
'<input type="submit" value="Upload">'+
'</form>'+
'<script src="/qushan/images/jquery.min.js"></script>'+
'<script>'+
'function ajaxUpload(){'+
'$.ajax({'+
' url: "http://www.likefun.cn/qushan/upload",'+
' type: "POST",'+
' data: new FormData($("#myForm")[0]),'+
' processData: false,'+
' contentType: false,'+
' success:function(responseStr){'+
' console.log(responseStr.newPath);'+
' $("#uploadImg img").attr("src",responseStr.newPath)'+
' }'+
'})'+
'}'+
'</script>'
)
})
app.post('/qushan/upload',function(req,res){
var form = new multiparty.Form();
form.encoding = 'utf-8';
form.uploadDir = "uploads/images/";
form.maxFilesSize = 2 * 1024 * 1024;
form.parse(req, function(err, fields, files) {
var fileOne = files.upload[0]
console.log(fileOne)
var imgUrl = '/qushan/' + fileOne.path.substring(8)
res.json({"newPath":imgUrl})
});
return
})
formidable用法参考https://segmentfault.com/a/1190000005706031
Node使用multiparty包上传文件的更多相关文章
- Nodejs学习笔记(八)--- Node.js + Express 实现上传文件功能(felixge/node-formidable)
目录 前言 formidable简介 创建项目并安装formidable 实现上传功能 运行结果 部分疑惑解析 写在之后 前言 前面讲了一个构建网站的示例,这次在此基础上再说说web的常规功能---- ...
- Nodejs学习笔记(八)—Node.js + Express 实现上传文件功能(felixge/node-formidable)
前言 前面讲了一个构建网站的示例,这次在此基础上再说说web的常规功能----文件上传,示例以一个上传图片的功能为例子 上传功能命名用formidable实现,示例很简单! PS:最近比较忙,距上一次 ...
- 使用MultipartFile上传文件
转载地址:https://www.cnblogs.com/lunaticcoder/p/9813483.html(具体的看这个这个大佬的博客) 依赖包: <!-- 上传文件依赖组件 --> ...
- 记录一次node中台转发表单上传文件到后台过程
首发掘金 记录一次node中台转发表单上传文件到后台过程 本篇跟掘金为同一个作者leung 公司几个项目都是三层架构模式即前台,中台(中间层),后台.前台微信端公众号使用vue框架,后台管理前端使 ...
- [Node.js] 使用File API 异步上传文件
原文地址:http://www.moye.me/2014/11/05/html5-filereader/ 最近在做一个网盘的项目,不出意外的涉及到大文件的上传,那么问题来了:如何实时的显示文件上传的进 ...
- node.js 上传文件
在工作中碰到了这样的需求,需要用nodejs 来上传文件,之前也只是知道怎么通过浏览器来上传文件, 用nodejs的话, 相当于模拟浏览器的行为. google 了一番之后, 明白了浏览器无非就 ...
- node 上传文件 路径 重命名等问题
最近在学习node,想做一个简单的网站.首先想到的是上传文件的功能,查了下,发现有一个formidable模块,操作方便,便拿来尝试了一下,结果很纠结. 下载安装的就不用说了,用npm即可.说一下,自 ...
- hadoopmaster主机上传文件出错: put: File /a.txt._COPYING_ could only be replicated to 0 nodes instead of minReplication (=1). There are 3 datanode(s) running and 3 node(s) are excluded in this operation.
刚开始装好hadoop的时候,namenode机上传文件没有错误,今天打开时突然不能上传文件,报错 put: File /a.txt._COPYING_ could only be replicate ...
- SpringBoot项目打成jar包后上传文件到服务器 目录与jar包同级问题
看标题好像很简单的样子,但是针对使用jar包发布SpringBoot项目就不一样了.当你使用tomcat发布项目的时候,上传文件存放会变得非常简单,因为你可以随意操作项目路径下的资源.但是当你使用Sp ...
随机推荐
- 一些LINQ的使用
var list = from staff in staffList from extraRecord in extraList where staff.staffID == extraRecord. ...
- 使用axis开发web service服务端
一.axis环境搭建 1.安装环境 JDK.Tomcat或Resin.eclipse等. 2.到 http://www.apache.org/dyn/closer.cgi/ws/axis/1_4下载A ...
- 网络第一节——NSURLConnection
一.NSURLConnection的常用类 (1)NSURL:请求地址 (2)NSURLRequest:封装一个请求,保存发给服务器的全部数据,包括一个NSURL对象,请求方法.请求头.请求体.... ...
- Swift3.0P1 语法指南——继承
原档:https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programmi ...
- nubia Z5 mini 小牛 黑砖qhsusb dload修复
给手机分区,用了分区助手,将一些分区移动. 结果分区坏了,只有两三个分区在电脑显示,当时晕菜把数据线拔了重插. 手机变砖,不能启动,黑屏,不能进recovery... 参考 http://bbs.nu ...
- js 中histroy.back()与history.go()的区别
样例: js6.jsp <%@ page language="java" import="java.util.*" pageEncoding=" ...
- python之路二十一
URL - 两个 Views - 请求的其他信息 from django.core.handlers.wsgi import WSGIRequest ...
- Windows+Caffe+VS2013+python接口配置过程
前段时间在笔记本上配置了Caffe框架,中间过程曲曲折折,但由于懒没有将详细过程总结下来,这两天又在一台配置较高的台式机上配置了Caffe,配置时便非常后悔当初没有写到博客中去,现已配置好Caffe, ...
- 344. Reverse String
https://leetcode.com/problems/reverse-string/ Python语法糖爆炸时间 class Solution(object): def reverseStrin ...
- 377. Combination Sum IV
问题 Given an integer array with all positive numbers and no duplicates, find the number of possible c ...