源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

在Node.js中使用HTTP上传文件的方法

  • 时间:2022-03-19 06:36 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:在Node.js中使用HTTP上传文件的方法
[b]开发环境 [/b]我们将使用 Visual Studio Express 2013 for Web 作为开发环境, 不过它还不能被用来做 Node.js 开发。为此我们需要安装 [url=http://nodejstools.codeplex.com/]Node.js Tools for Visual Studio[/url]。  装好后 Visual Studio Express 2013 for Web 就会转变成一个 Node.js IDE 环境,提供创建这个应用所需要的所有东西.。而基于这里提供的[url=https://nodejstools.codeplex.com/wikipage?title=Installation]指导[/url],我们需要: [list] [*]    下载安装 Node.js  Windows 版,选择适用你系统平台的版本, [url=http://nodejs.org/dist/v0.10.33/node-v0.10.33-x86.msi]Node.js (x86)[/url] 或者[url=http://nodejs.org/dist/v0.10.33/x64/node-v0.10.33-x64.msi]Node.js (x64)[/url] 。[/*] [*]    [url=https://nodejstools.codeplex.com/downloads/get/925192]下载[/url]并安装 Node.js 的 Visual Studio 工具。[/*] [/list] 安装完成后我们就会运行 Visual Studio Express 2013 for Web, 并使用 Node.js 的交互窗口来验证安装. Node.js 的交互窗口可以再 View->Other Windows->Node.js Interactive Window 下找到. Node.js 交互窗口运行后我们要输入一些命令检查是否一切OK. [img]http://files.jb51.net/file_images/article/201506/2015623103550896.png?2015523103650[/img] Figure 1 Node.js Interactive Window 现在我们已经对安装进行了验证,我们现在就可以准备开始创建支持GB级文件上传的Node.js后台程序了. 开始我们先创建一个新的项目,并选择一个空的 Node.js Web应用程序模板. [img]http://files.jb51.net/file_images/article/201506/2015623103701380.png?2015523103711[/img] Figure 2 New project using the Blank Node.js Web Application template 项目创建好以后,我们应该会看到一个叫做 server.js 的文件,还有解决方案浏览器里面的Node包管理器 (npm).  [img]http://files.jb51.net/file_images/article/201506/2015623103722152.png?2015523103731[/img] 图3 解决方案管理器里面的 Node.js 应用程序 server.js 文件里面有需要使用Node.js来创建一个基础的hello world应用程序的代码. [img]http://files.jb51.net/file_images/article/201506/2015623103740566.png?2015523103748[/img] Figure 4 The Hello World application  我现在继续把这段代码从 server.js 中删除,然后在Node.js中穿件G级别文件上传的后端代码。下面我需要用npm安装这个项目需要的一些依赖: [list] [*]     Express - Node.js网页应用框架,用于构建单页面、多页面以及混合网络应用[/*] [*]     Formidable - 用于解析表单数据,特别是文件上传的Node.js模块[/*] [*]     fs-extra - 文件系统交互模块 [/*] [/list] [img]http://files.jb51.net/file_images/article/201506/2015623103757338.png?201552310385[/img] 图5 使用npm安装所需模块 模块安装完成后,我们可以从解决方案资源管理器中看到它们。 [img]http://files.jb51.net/file_images/article/201506/2015623103815638.png?2015523103824[/img]  图6 解决方案资源管理器显示已安装模块 下一步我们需要在解决方案资源管理器新建一个 "Scripts" 文件夹并且添加  "workeruploadchunk.js" 和   "workerprocessfile.js" 到该文件夹。我们还需要下载[url=http://code.jquery.com/jquery-2.1.1.min.js]jQuery 2.x[/url] 和  [url=https://github.com/satazor/SparkMD5/archive/master.zip]SparkMD5 [/url]库并添加到"Scripts"文件夹。 最后还需要添加 "Default.html" 页面。 [b] 创建Node.js后台[/b] 首先我们需要用Node.js的"require()"函数来导入在后台上传G级文件的模块。注意我也导入了"path"以及"crypto" 模块。"path"模块提供了生成上传文件块的文件名的方法。"crypto" 模块提供了生成上传文件的MD5校验和的方法。
// The required modules  
var express = require('express');  
var formidable = require('formidable');  
var fs = require('fs-extra');  
var path = require('path'); 
var crypto = require('crypto');

下一行代码就是见证奇迹的时刻。
[url=https://github.com/adminastoolsmith/CelerFTwithNodeJS]github 资源库[/url]上找到
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部