nodejs 上传图片(服务端输出全部代码)
下面代码,全部都是nodejs端的,不用客户端代码。也就是,选择图片的form表单以及上传完毕预览图片的html,都是由node服务端输出的。
1 启动代码:(node upload.js)
var server = require("upload/server");
var router = require("upload/router");
var requestHandlers = require("upload/requestHandlers"); var handle = {}
handle["/"] = requestHandlers.start;
handle["/start"] = requestHandlers.start;
handle["/upload"] = requestHandlers.upload;
handle["/show"] = requestHandlers.show; server.start(router.route, handle);
2 自己封装成upload模块,也就是在node_modules文件夹下有一个upload文件夹。该文件夹下有3个文件,分别如下:
requestHandlers.js
var querystring = require("querystring"),
fs = require("fs"),
formidable = require("formidable"); function start(response) {
console.log("Request handler 'start' was called."); var body = '<html>'+
'<head>'+
'<meta http-equiv="Content-Type" content="text/html; '+
'charset=UTF-8" />'+
'</head>'+
'<body>'+
'<form action="/upload" enctype="multipart/form-data" '+
'method="post">'+
'<input type="file" name="upload" multiple="multiple">'+
'<input type="submit" value="Upload file" />'+
'</form>'+
'</body>'+
'</html>'; response.writeHead(200, {"Content-Type": "text/html"});
response.write(body);
response.end();
} function upload(response, request) {
console.log("Request handler 'upload' was called."); var form = new formidable.IncomingForm();
form.uploadDir = "F:\\nodeTemp\\upload";
console.log("about to parse1");
form.parse(request, function(error, fields, files) {
console.log("parsing done");
console.log(files.upload.path);
fs.renameSync(files.upload.path, "F:\\nodeTemp\\upload\\test.png");
response.writeHead(200, {"Content-Type": "text/html"});
response.write("received image:<br/>");
response.write("<img src='/show' />");
response.end();
});
} function show(response) {
console.log("Request handler 'show' was called.");
fs.readFile("F:\\nodeTemp\\upload\\test.png", "binary", function(error, file) {
if(error) {
response.writeHead(500, {"Content-Type": "text/plain"});
response.write(error + "\n");
response.end();
} else {
response.writeHead(200, {"Content-Type": "image/png"});
response.write(file, "binary");
response.end();
}
});
} exports.start = start;
exports.upload = upload;
exports.show = show;
router.js
function route(handle, pathname, response, request) {
console.log("About to route a request for " + pathname);
if (typeof handle[pathname] === 'function') {
handle[pathname](response, request);
} else {
console.log("No request handler found for " + pathname);
response.writeHead(404, {"Content-Type": "text/html"});
response.write("404 Not found");
response.end();
}
} exports.route = route;
server.js
var http = require("http");
var url = require("url"); function start(route, handle) {
function onRequest(request, response) {
var pathname = url.parse(request.url).pathname;
console.log("Request for " + pathname + " received.");
route(handle, pathname, response, request);
} http.createServer(onRequest).listen(3001);
console.log("Server has started.");
} exports.start = start;
3 这个上传图片,是依赖formidable模块的,所以,需要提前通过npm安装好该模块。
4 还有,这个上传图片,要自己先建立一个文件夹来存放图片。建好之后,把“F:\\nodeTemp\\upload\\test.png”替换就行。
原文:http://www.cnblogs.com/EricaMIN1987_IT/p/3640450.html
我根据自己的理解,改动了下。
可参考:
http://www.cnblogs.com/thingk/archive/2013/11/25/3434032.html
nodejs 上传图片(服务端输出全部代码)的更多相关文章
- 示例 - 25行代码等价实现 - 借助Nodejs在服务端使用jQuery采集17173游戏排行信息
今天在园子里看到一篇文章: 借助Nodejs在服务端使用jQuery采集17173游戏排行信息 感觉用SS来实现相同功能更加简洁, 于是写了一下, 发现25行代码就搞定了 (包括自动翻页), 于是跟大 ...
- 借助Nodejs在服务端使用jQuery采集17173游戏排行信息
Nodejs相关依赖模块介绍 Nodejs的优势这里就不做介绍啦,这年头相信大家对它也不陌生了.这里主要介绍一下用到的第三方模块. async:js代码中到处都是异步回调,很多时候我们需要做同步处理, ...
- TCP通信服务端及客户端代码
Java TCP通信使用的是Socket(客服端)和ServerSocket(服务端),具体代码如下. server端代码: import java.io.BufferedReader; import ...
- nodejs实现服务端重定向
nodejs实现服务端重定向:https://www.jianshu.com/p/5a1500fcd713
- vuejs+nodejs支持服务端渲染的博客系统
感悟 历时两个多月,终于利用工作之余完成了这个项目的1.0版本,为什么要写这个项目?其实基于vuejs+nodejs构建的开源博客系统有很多,但是大多数不支持服务端渲染,也不支持动态标题,只是做到了前 ...
- socket模块实现基于UDP聊天模拟程序;socketserver模块实现服务端 socket客户端代码示例
socket模块 serSocket.setblocking(False) 设置为非阻塞: #coding=utf-8 from socket import * import time # 用来存储所 ...
- 【Azure 应用服务】Azure Mobile App (NodeJS) 的服务端部署在App Service for Windows中出现404 Not Found -- The resource you are looking for has been removed, had its name changed, or is temporarily unavailable.
问题描述 使用NodeJS的后端应用,开发一个Mobile App的服务端,手机端通过REST API来访问获取后端数据.在本地编译好后,通过npm start启动项目,访问效果如下: 但是,当把项目 ...
- nodejs 开发服务端 child_process 调试方法(1)
由于最近正在做一个服务端项目,采用了nodejs引擎开发,主要是master-worker工作机制;主进程可以直接调试,但是子进程调试好像有点麻烦,我没有找到好的方法; worker这里,我分拆成了几 ...
- 微信小程序生成带参数的二维码(小程序码)独家asp.net的服务端c#完整代码
一)我先用的小程序端的wx.request去调用API,发现竟然是一个坑! wx.request({ url: 'https://api.weixin.qq.com/wxa/getwxacodeunl ...
随机推荐
- data standardization
import random import numpy as np l, num, gen_min_, gen_max_ = [], 100, 1, 200 l = [random.randint(ge ...
- thinkphp将APP_DEBUG常量设置为false后报错的问题
ThinkPHP 将 APP_DEBUG 常量设置为 false 后出现了下面的问题: Parse error: syntax error, unexpected T_STRING in \www\R ...
- Python菜鸟之路:Python基础
一.Python版本升级至3.0的必然性 In November 2014, it was announced that Python 2.7 would be supported until 202 ...
- 洛谷 P2486 [SDOI2011]染色
题目描述 输入输出格式 输入格式: 输出格式: 对于每个询问操作,输出一行答案. 输入输出样例 输入样例#1: 6 5 2 2 1 2 1 1 1 2 1 3 2 4 2 5 2 6 Q 3 5 C ...
- Linux安装virtualenvwrapper详细步骤
1.[root@localhost ~]# pip install virtualenvwrapper 2.[root@localhost ~]# pip list [root@localhost ~ ...
- QT5的exe的发布
直接release的exe文件需要很多dll关联,一个一个找又太麻烦. 其实QT5带有一个 windeployqt 工具 Qt Widgets Application可执行程序发布方式 首先用 QtC ...
- Java进阶学习:log4j的学习和使用
Java进阶学习——log4j的学习和使用 简介Loj4j Log4j的组成 Log4j主要由三大组组件构成: Logger: 负责生成日志,并能够对日志信息进行分类筛选,通俗的讲就是决定什么日志信息 ...
- weak 的内部实现原理
问题 weak 变量在引用计数为0时,会被自动设置成 nil,这个特性是如何实现的? 答案 在 Friday QA 上,有一期专门介绍 weak 的实现原理.https://mikeash.com/p ...
- 队列(Queue)
队列(Queue) Queue: 先入先出(FIFO)的数据结构. offer,add区别: 一些队列有大小限制,因此如果想在一个满的队列中加入一个新项,多出的项就会被拒绝. 这时新的 offer 方 ...
- 虚拟机ubuntu14.04系统设置静态ip
ubuntu14.04 设置静态ip vim /etc/network/interfaces 原来只有 auto lo iface lo inet loopback 修改成如下: auto lo if ...