使用 HTTP 上传 G 级的文件之 Node.js 版本

开发 前端
我们将使用 Visual Studio Express 2013 for Web 作为开发环境, 不过它还不能被用来做 Node.js 开发。为此我们需要安装 Node.js Tools for Visual Studio。 装好后 Visual Studio Express 2013 for Web 就会转变成一个 Node.js IDE 环境,提供创建这个应用所需要的所有东西.。而基于这里提供的指导,我们需要:

不管喜欢与否,javascript无处不在。 我们可以在客户端的前台应用中找到它,也可以在大量的框架、类库中找到它,而且可以在服务器端的后台应用中找到它。

近年来, Javascript越来越流行,这似乎是由于 Javascript 生态系统正在帮助提高生产率、减少入门所需的时间。 在我的***篇文章中,我介绍了使用 ASP.NET Web 后端 API 实现 G级文件上传,发表完这篇文章后,我决定试一下使用 Node.js 能否达到同样的效果。  这意味着我需要实现 UploadChunk和 MergeAll方法,在 Node.js中我发表的 ***一篇文章谈到了这一点。

开发环境

我们将使用 Visual Studio Express 2013 for Web 作为开发环境, 不过它还不能被用来做 Node.js 开发。为此我们需要安装 Node.js Tools for Visual Studio。  装好后 Visual Studio Express 2013 for Web 就会转变成一个 Node.js IDE 环境,提供创建这个应用所需要的所有东西.。而基于这里提供的指导,我们需要:

下载安装 Node.js  Windows 版,选择适用你系统平台的版本, Node.js (x86) 或者 Node.js (x64)。

下载并安装 Node.js 的 Visual Studio 工具。

安装完成后我们就会运行 Visual Studio Express 2013 for Web, 并使用 Node.js 的交互窗口来验证安装. Node.js 的交互窗口可以再 View->Other Windows->Node.js Interactive Window 下找到. Node.js 交互窗口运行后我们要输入一些命令检查是否一切OK.

Figure 1 Node.js Interactive Window

现在我们已经对安装进行了验证,我们现在就可以准备开始创建支持GB级文件上传的Node.js后台程序了. 开始我们先创建一个新的项目,并选择一个空的 Node.js Web应用程序模板.

Figure 2 New project using the Blank Node.js Web Application template

项目创建好以后,我们应该会看到一个叫做 server.js 的文件,还有解决方案浏览器里面的Node包管理器 (npm). 

 

图3 解决方案管理器里面的 Node.js 应用程序

server.js 文件里面有需要使用Node.js来创建一个基础的hello world应用程序的代码.

Figure 4 The Hello World application

我现在继续把这段代码从 server.js 中删除,然后在Node.js中穿件G级别文件上传的后端代码。下面我需要用npm安装这个项目需要的一些依赖:

  •  Express - Node.js网页应用框架,用于构建单页面、多页面以及混合网络应用

  •  Formidable - 用于解析表单数据,特别是文件上传的Node.js模块

  •  fs-extra - 文件系统交互模块 

 

图5 使用npm安装所需模块

模块安装完成后,我们可以从解决方案资源管理器中看到它们。

 

图6 解决方案资源管理器显示已安装模块

下一步我们需要在解决方案资源管理器新建一个 "Scripts" 文件夹并且添加  "workeruploadchunk.js" 和   "workerprocessfile.js" 到该文件夹。我们还需要下载 jQuery 2.x 和  SparkMD5 库并添加到"Scripts"文件夹。 ***还需要添加 "Default.html" 页面。这些都在我之前的 post 中介绍过。

#p#

创建Node.js后台

首先我们需要用Node.js的"require()"函数来导入在后台上传G级文件的模块。注意我也导入了"path"以及"crypto" 模块。"path"模块提供了生成上传文件块的文件名的方法。"crypto" 模块提供了生成上传文件的MD5校验和的方法。

  1. // The required modules         
  2. var   express = require('express');       
  3. var   formidable = require('formidable');       
  4. var   fs = require('fs-extra');       
  5. var   path = require('path');   
  6. var   crypto = require('crypto'); 

下一行代码就是见证奇迹的时刻。

  1. <span style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-removed: initial; background-repeat: initial; background-size: initial; color: #000066; font-family: Consolas; font-size: 9pt;">var</span><span style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-removed: initial; background-repeat: initial; background-size: initial; font-family: Consolas; font-size: 9pt;"> app <span style="color: #339933;">=</span> express<span style="color: #009900;">()</span><span style="color: #339933;">;</span></span> 

这行代码是用来创建express应用的。express应用是一个封装了Node.js底层功能的中间件。如果你还记得那个由Blank Node.js Web应用模板创建的"Hello World" 程序,你会发现我导入了"http"模块,然后调用了"http.CreateServer()"方法创建了 "Hello World" web应用。我们刚刚创建的express应用内建了所有的功能。

现在我们已经创建了一个express应用,我们让它呈现之前创建的"Default.html",然后让应用等待连接。

  1. // Serve up the Default.html page   
  2. app.use(express.static(__dirname, { index: 'Default.html' }));       
  3.   
  4. // Startup the express.js application   
  5. app.listen(process.env.PORT || 1337);       
  6.   
  7. // Path to save the files   
  8. var   uploadpath = 'C:/Uploads/CelerFT/'

express应用有app.VERB()方法,它提供了路由的功能。我们将使用app.post()方法来处理"UploadChunk" 请求。在app.post()方法里我们做的***件事是检查我们是否在处理POST请求。接下去检查Content-Type是否是mutipart/form-data,然后检查上传的文件块大小不能大于51MB。

  1. // Use the post method for express.js to respond to posts to the uploadchunk urls and   
  2. // save each file chunk as a separate file   
  3. app.post('*/api/CelerFTFileUpload/UploadChunk*'function(request,response) {       
  4.   
  5.     if (request.method === 'POST') {       
  6.         // Check Content-Type      
  7.         if (!(request.is('multipart/form-data'))){       
  8.             response.status(415).send('Unsupported media type');       
  9.             return;       
  10.         }       
  11.    
  12.         // Check that we have not exceeded the maximum chunk upload size   
  13.         var maxuploadsize =51 * 1024 * 1024;       
  14.   
  15.         if (request.headers['content-length']> maxuploadsize){       
  16.             response.status(413).send('Maximum upload chunk size exceeded');       
  17.             return;       
  18.         } 

一旦我们成功通过了所有的检查,我们将把上传的文件块作为一个单独分开的文件并将它按顺序数字命名。下面最重要的代码是调用fs.ensureDirSync()方法,它使用来检查临时目录是否存在。如果目录不存在则创建一个。注意我们使用的是该方法的同步版本。

  1. // Get the extension from the file name   
  2. var extension =path.extname(request.param('filename'));       
  3.   
  4. // Get the base file name   
  5. var baseFilename =path.basename(request.param('filename'), extension);       
  6.   
  7. // Create the temporary file name for the chunk   
  8. var tempfilename =baseFilename + '.'+       
  9. request.param('chunkNumber').toString().padLeft('0', 16) + extension + ".tmp";       
  10.   
  11.   
  12. // Create the temporary directory to store the file chunk   
  13. // The temporary directory will be based on the file name   
  14. var tempdir =uploadpath + request.param('directoryname')+ '/' + baseFilename;       
  15.   
  16. // The path to save the file chunk   
  17. var localfilepath =tempdir + '/'+ tempfilename;       
  18.   
  19. if (fs.ensureDirSync(tempdir)) {       
  20.     console.log('Created directory ' +tempdir);   

正如我之前提出的,我们可以通过两种方式上传文件到后端服务器。***种方式是在web浏览器中使用FormData,然后把文件块作为二进制数据发送,另一种方式是把文件块转换成base64编码的字符串,然后创建一个手工的multipart/form-data encoded请求,然后发送到后端服务器。  

所以我们需要检查一下是否在上传的是一个手工multipart/form-data encoded请求,通过检查"CelerFT-Encoded"头部信息,如果这个头部存在,我们创建一个buffer并使用request的ondata时间把数据拷贝到buffer中。

在request的onend事件中通过将buffer呈现为字符串并按CRLF分开,从而 multipart/form-data encoded请求中提取base64字符串。base64编码的文件块可以在数组的第四个索引中找到。

通过创建一个新的buffer来将base64编码的数据重现转换为二进制。随后调用fs.outputFileSync()方法将buffer写入文件中。

  1. // Check if we have uploaded a hand crafted multipart/form-data request   
  2. // If we have done so then the data is sent as a base64 string   
  3. // and we need to extract the base64 string and save it   
  4. if (request.headers['celerft-encoded']=== 'base64') {      
  5.   
  6.     var fileSlice = newBuffer(+request.headers['content-length']);       
  7.     var bufferOffset = 0;       
  8.   
  9.     // Get the data from the request   
  10.     request.on('data'function (chunk) {       
  11.         chunk.copy(fileSlice , bufferOffset);       
  12.         bufferOffset += chunk.length;       
  13.     }).on('end'function() {       
  14.         // Convert the data from base64 string to binary   
  15.         // base64 data in 4th index of the array   
  16.         var base64data = fileSlice.toString().split('\r\n');       
  17.         var fileData = newBuffer(base64data[4].toString(), 'base64');       
  18.   
  19.         fs.outputFileSync(localfilepath,fileData);       
  20.         console.log('Saved file to ' +localfilepath);       
  21.   
  22.         // Send back a sucessful response with the file name   
  23.         response.status(200).send(localfilepath);       
  24.         response.end();       
  25.     });   

二进制文件块的上传是通过formidable模块来处理的。我们使用formidable.IncomingForm()方法得到multipart/form-data encoded请求。formidable模块将把上传的文件块保存为一个单独的文件并保存到临时目录。我们需要做的是在formidable的onend事件中将上传的文件块保存为里一个名字。

  1. else {       
  2.     // The data is uploaded as binary data.       
  3.     // We will use formidable to extract the data and save it       
  4.     var form = new formidable.IncomingForm();       
  5.     form.keepExtensions = true;       
  6.     form.uploadDir = tempdir;      
  7.   
  8.     // Parse the form and save the file chunks to the       
  9.     // default location       
  10.     form.parse(request, function (err, fields, files) {       
  11.         if (err){       
  12.             response.status(500).send(err);       
  13.             return;       
  14.         }       
  15.   
  16.     //console.log({ fields: fields, files: files });       
  17.     });       
  18.   
  19.     // Use the filebegin event to save the file with the naming convention       
  20.     /*form.on('fileBegin', function (name, file) {   
  21.     file.path = localfilepath;   
  22. });*/         
  23.   
  24. form.on('error'function (err) {       
  25.         if (err){       
  26.             response.status(500).send(err);       
  27.             return;       
  28.         }       
  29.     });       
  30.   
  31.     // After the files have been saved to the temporary name       
  32.     // move them to the to teh correct file name       
  33.     form.on('end'function (fields,files) {       
  34.         // Temporary location of our uploaded file              
  35.         var temp_path = this.openedFiles[0].path;       
  36.   
  37.         fs.move(temp_path , localfilepath,function (err){       
  38.   
  39.             if (err) {       
  40.                 response.status(500).send(err);       
  41.                 return;       
  42.             }       
  43.             else {       
  44.                 // Send back a sucessful response with the file name       
  45.                 response.status(200).send(localfilepath);       
  46.                 response.end();       
  47.             }      
  48.         });      
  49.     });       
  50.   
  51. // Send back a sucessful response with the file name       
  52. //response.status(200).send(localfilepath);       
  53. //response.end();       
  54. }   

app.get()方法使用来处理"MergeAll"请求的。这个方法实现了之前描述过的功能。

  1. // Request to merge all of the file chunks into one file   
  2. app.get('*/api/CelerFTFileUpload/MergeAll*'function(request,response) {       
  3.   
  4.     if (request.method === 'GET') {       
  5.   
  6.         // Get the extension from the file name   
  7.         var extension =path.extname(request.param('filename'));       
  8.   
  9.         // Get the base file name   
  10.         var baseFilename =path.basename(request.param('filename'), extension);       
  11.   
  12.         var localFilePath =uploadpath + request.param('directoryname')+ '/' + baseFilename;       
  13.   
  14.         // Check if all of the file chunks have be uploaded   
  15.         // Note we only wnat the files with a *.tmp extension   
  16.         var files =getfilesWithExtensionName(localFilePath, 'tmp')       
  17.         /*if (err) {   
  18.             response.status(500).send(err);   
  19.             return;   
  20.         }*/   
  21.   
  22.         if (files.length !=request.param('numberOfChunks')){      
  23.             response.status(400).send('Number of file chunks less than total count');       
  24.             return;       
  25.         }       
  26.   
  27.         var filename =localFilePath + '/'+ baseFilename +extension;       
  28.         var outputFile =fs.createWriteStream(filename);       
  29.   
  30.         // Done writing the file   
  31.         // Move it to top level directory   
  32.         // and create MD5 hash   
  33.         outputFile.on('finish'function (){       
  34.             console.log('file has been written');       
  35.             // New name for the file   
  36.             var newfilename = uploadpath +request.param('directoryname')+ '/' + baseFilename   
  37.             + extension;       
  38.   
  39.             // Check if file exists at top level if it does delete it   
  40.             //if (fs.ensureFileSync(newfilename)) {   
  41.             fs.removeSync(newfilename);       
  42.             //}  
  43.   
  44.             // Move the file   
  45.             fs.move(filename, newfilename ,function (err) {       
  46.                 if (err) {       
  47.                     response.status(500).send(err);       
  48.                     return;       
  49.                 }       
  50.                 else {       
  51.                     // Delete the temporary directory   
  52.                     fs.removeSync(localFilePath);       
  53.                     varhash = crypto.createHash('md5'),       
  54.                         hashstream = fs.createReadStream(newfilename);      
  55.   
  56.                     hashstream.on('data'function (data) {       
  57.                         hash.update(data)       
  58.                     });       
  59.   
  60.                     hashstream.on('end'function (){      
  61.                         var md5results =hash.digest('hex');       
  62.                         // Send back a sucessful response with the file name   
  63.                         response.status(200).send('Sucessfully merged file ' + filename + ", "       
  64.                         + md5results.toUpperCase());       
  65.                         response.end();       
  66.                     });       
  67.                 }       
  68.             });       
  69.         });       
  70.   
  71.         // Loop through the file chunks and write them to the file   
  72.         // files[index] retunrs the name of the file.   
  73.         // we need to add put in the full path to the file   
  74.         for (var index infiles) {      
  75.             console.log(files[index]);       
  76.             var data = fs.readFileSync(localFilePath +'/' +files[index]);       
  77.             outputFile.write(data);       
  78.             fs.removeSync(localFilePath + '/' + files[index]);       
  79.         }       
  80.         outputFile.end();       
  81.     }   
  82.   
  83. })   ; 

注意Node.js并没有提供String.padLeft()方法,这是通过扩展String实现的。

  1. // String padding left code taken from   
  2. // http://www.lm-tech.it/Blog/post/2012/12/01/String-Padding-in-Javascript.aspx   
  3. String.prototype.padLeft = function (paddingChar, length) {       
  4.     var s = new String(this);       
  5.     if ((this.length< length)&& (paddingChar.toString().length > 0)) {       
  6.         for (var i = 0; i < (length - this.length) ; i++) {       
  7.             s = paddingChar.toString().charAt(0).concat(s);       
  8.         }       
  9.     }      
  10.     return s;   
  11. }   ; 

#p#

一些其它事情

其中一件事是,发表上篇文章后我继续研究是为了通过域名碎片实现并行上传到CeleFT功能。域名碎片的原理是访问一个web站点时,让web浏览器建立更多的超过正常允许范围的并发连接。 域名碎片可以通过使用不同的域名(如web1.example.comweb2.example.com)或者不同的端口号(如8000, 8001)托管web站点的方式实现。

示例中,我们使用不同端口号托管web站点的方式。

我们使用 iisnode 把 Node.js集成到 IIS( Microsoft Internet Information Services)实现这一点。 下载兼容你操作系统的版本 iisnode (x86) 或者  iisnode (x64)。 下载 IIS URL重写包

一旦安装完成(假定windows版Node.js已安装),到IIS管理器中创建6个新网站。将***个网站命名为CelerFTJS并且将侦听端口配置为8000。

图片7在IIS管理器中创建一个新网站

然后创建其他的网站。我为每一个网站都创建了一个应用池,并且给应用池“LocalSystem”级别的权限。所有网站的本地路径是C:\inetpub\wwwroot\CelerFTNodeJS。

图片8 文件夹层级

我在Release模式下编译了Node.js应用,然后我拷贝了server.js文件、Script文件夹以及node_modules文件夹到那个目录下。

要让包含 iisnode 的Node.js的应用工作,我们需要创建一个web.config文件,并在其中添加如下得内容。

  1. <defaultDocument>   
  2.     <files>   
  3.       <add value="server.js" />   
  4.     </files>   
  5.   </defaultDocument>   
  6.   
  7.   <handlers>   
  8.     <!-- indicates that the server.js file is a node.js application to be handled by the        
  9.     iisnode module -->       
  10.     <add name="iisnode" path="*.js" verb="*" modules="iisnode" />   
  11.   </handlers>   
  12.   
  13.   <rewrite>   
  14.     <rules>   
  15.       <rule name="CelerFTJS">   
  16.         <match url="/*" />   
  17.         <action type="Rewrite" url="server.js" />   
  18.       </rule>   
  19.   
  20.       <!-- Don't interfere with requests for node-inspector debugging -->       
  21.       <rule name="NodeInspector" patternSyntax="ECMAScript" stopProcessing="true">   
  22.         <match url="^server.js\/debug[\/]?" />   
  23.       </rule>   
  24.     </rules>   
  25.   </rewrite> 

web.config中各项的意思是让iisnode处理所有得*.js文件,由server.js 处理任何匹配"/*"的URL。

图片9 URL重写规则

如果你正确的做完了所有的工作,你就可以通过http://localhost:8000浏览网站,并进入CelerFT "Default.html"页面。

web.config文件被修改以支持如前面post中所解释的大文件的上传,这里我不会解释所有的项。不过下面的web.config项可以改善 iisnode中Node.js的性能。

  1. <span style="font-family: Consolas; font-size: 9pt;"><iisnode</span><span style="color: #009900; font-family: Consolas; font-size: 9.0pt;"> </span><span style="color: #000066; font-family: Consolas; font-size: 9.0pt;">node_env</span><span style="color: #009900; font-family: Consolas; font-size: 9.0pt;">=</span><span style="color: red; font-family: Consolas; font-size: 9.0pt;">"production"</span><span style="color: #009900; font-family: Consolas; font-size: 9.0pt;"> </span><span style="color: #000066; font-family: Consolas; font-size: 9.0pt;">debuggingEnabled</span><span style="color: #009900; font-family: Consolas; font-size: 9.0pt;">=</span><span style="color: red; font-family: Consolas; font-size: 9.0pt;">"false"</span><span style="color: #009900; font-family: Consolas; font-size: 9.0pt;"> </span><span style="color: #000066; font-family: Consolas; font-size: 9.0pt;">devErrorsEnabled</span><span style="color: #009900; font-family: Consolas; font-size: 9.0pt;">=</span><span style="color: red; font-family: Consolas; font-size: 9.0pt;">"false"</span><span style="color: #009900; font-family: Consolas; font-size: 9.0pt;"> </span><span style="color: #000066; font-family: Consolas; font-size: 9.0pt;">nodeProcessCountPerApplication</span><span style="color: #009900; font-family: Consolas; font-size: 9.0pt;">=</span><span style="color: red; font-family: Consolas; font-size: 9.0pt;">"0"</span><span style="color: #009900; font-family: Consolas; font-size: 9.0pt;"> </span><span style="color: #000066; font-family: Consolas; font-size: 9.0pt;">maxRequestBufferSize</span><span style="color: #009900; font-family: Consolas; font-size: 9.0pt;">=</span><span style="color: red; font-family: Consolas; font-size: 9.0pt;">"52428800"</span><span style="color: #009900; font-family: Consolas; font-size: 9.0pt;"> </span><span style="font-family: Consolas; font-size: 9pt;">/></span><span style="font-family: Consolas; font-size: 9.0pt;"><o:p></o:p></span> 

#p# 

并行上传  

为了使用域名碎片来实现并行上传,我不得不给Node.js应用做些修改。我***个要修改的是让Node.js应用支持跨域资源共享。我不得不这样做是因为使用域碎片实际上是让一个请求分到不同的域并且同源策略会限制我的这个请求。

好消息是XMLttPRequest 标准2规范允许我这么做,如果网站已经把跨域资源共享打开,更好的是我不用为了实现这个而变更在"workeruploadchunk.js"里的上传方法。

  1. // 使用跨域资源共享 // Taken from http://bannockburn.io/2013/09/cross-origin-resource-sharing-cors-with-a-node-js-express-js-and-sencha-touch-app/   
  2. var   enableCORS = function(request,response, next){       
  3.     response.header('Access-Control-Allow-Origin''*');       
  4.     response.header('Access-Control-Allow-Methods''GET,POST,OPTIONS');       
  5.     response.header('Access-Control-Allow-Headers', 'Content-Type, Authorization, Content-   
  6.                     Length,    X-Requested-With'   )   ;   
  7.    
  8.   
  9.     // 拦截OPTIONS方法 
  10.     if ('OPTIONS' ==request.method){       
  11.         response.send(204);       
  12.     }       
  13.     else {       
  14.         next();       
  15.     }       
  16. }   ;         
  17.   
  18. // 在表达式中使用跨域资源共享 
  19. app.   use   (   enableCORS   )   ; 

为了使server.js文件中得CORS可用,我创建了一个函数,该函数会创建必要的头以表明Node.js应用支持CORS。另一件事是我还需要表明CORS支持两种请求,他们是:

  • 简单请求:

             1、只用GET,HEAD或POST。如果使用POST向服务器发送数据,那么发送给服务器的HTTP POST请求的Content-Type应是application/x-www-form-urlencoded, multipart/form-data, 或 text/plain其中的一个。

          2、HTTP请求中不要设置自定义的头(例如X-Modified等)

  • 预检请求:

           1、使用GET,HEAD或POST以外的方法。假设使用POST发送请求,那么Content-Type不能是application/x-www-form-urlencoded, multipart/form-data, or text/plain,例如假设POST请求向服务器发送了XML有效载荷使用了application/xml or text/xml,那么这个请求就是预检的。

            2、在请求中设置自定义头(比如请求使用X-PINGOTHER头)。

在我们的例子中,我们用的是简单请求,所以我们不需要做其他得工作以使例子能够工作。

  1. // We are going to upload to a backend that supports parallel uploads.   
  2. // Parallel uploads is supported by publishng the web site on different ports   
  3. // The backen must implement CORS for this to work   
  4. else if(workerdata.chunk!= null&& workerdata.paralleluploads ==true){      
  5.     if (urlnumber >= 6) {       
  6.         urlnumber = 0;       
  7.     }       
  8.   
  9.     if (urlcount >= 6) {       
  10.         urlcount = 0;       
  11.     }       
  12.   
  13.     if (urlcount == 0) {       
  14.         uploadurl = workerdata.currentlocation +webapiUrl + urlnumber;       
  15.     }       
  16.     else {       
  17.         // Increment the port numbers, e.g 8000, 8001, 8002, 8003, 8004, 8005   
  18.         uploadurl = workerdata.currentlocation.slice(0, -1) + urlcount +webapiUrl +       
  19.         urlnumber;       
  20.     }       
  21.   
  22.     upload(workerdata.chunk,workerdata.filename,workerdata.chunkCount, uploadurl,       
  23.     workerdata.asyncstate);       
  24.     urlcount++;       
  25.     urlnumber++;   
  26.   } 

在 Default.html 页面我对当前的URL进行了保存,因为我准备把这些信息发送给文件上传的工作程序. 只所以这样做是因为:  

  • 我想要利用这个信息增加端口数量

  • 做了 CORS 请求,我需要把完整的 URL 发送给 XMLHttpRequest 对象.

  1. <div class="MsoNoSpacing" style="background: #FFFF99;"
  2. <span style="font-family: "Lucida Console"; font-size: 8.0pt;">// Save current protocol and host for parallel uploads</span></div> 
  3. <div class="MsoNoSpacing" style="background: #FFFF99;"
  4. "font-family: &apos;Lucida Console&apos;; font-size: 8pt;"><span style="color: #000066;">var</span><span style="font-family: &apos;Lucida Console&apos;; font-size: 8pt;"> currentProtocol </span><span style="color: #339933; font-family: &apos;Lucida Console&apos;; font-size: 8pt;">= </span><span style="font-family: &apos;Lucida Console&apos;; font-size: 8pt;">window.</span><span style="color: #660066; font-family: &apos;Lucida Console&apos;; font-size: 8pt;">location</span><span style="font-family: &apos;Lucida Console&apos;; font-size: 8pt;">.</span><span style="color: #660066; font-family: &apos;Lucida Console&apos;; font-size: 8pt;">protocol</span><span style="color: #339933; font-family: &apos;Lucida Console&apos;; font-size: 8pt;">;</span></div> 
  5. <div class="MsoNoSpacing" style="background: #FFFF99;"
  6. "font-family: &apos;Lucida Console&apos;; font-size: 8pt;"><span style="color: #000066;">var</span><span style="font-family: &apos;Lucida Console&apos;; font-size: 8pt;"> currentHostandPort </span><span style="color: #339933; font-family: &apos;Lucida Console&apos;; font-size: 8pt;">=</span><span style="font-family: &apos;Lucida Console&apos;; font-size: 8pt;"> window.</span><span style="color: #660066; font-family: &apos;Lucida Console&apos;; font-size: 8pt;">location</span><span style="font-family: &apos;Lucida Console&apos;; font-size: 8pt;">.</span><span style="color: #660066; font-family: &apos;Lucida Console&apos;; font-size: 8pt;">host</span><span style="color: #339933; font-family: &apos;Lucida Console&apos;; font-size: 8pt;">;</span></div> 
  7. <div class="MsoNoSpacing" style="background: #FFFF99;"
  8. "font-family: &apos;Lucida Console&apos;; font-size: 8pt;"><span style="color: #000066;">var</span><span style="font-family: &apos;Lucida Console&apos;; font-size: 8pt;"> currentLocation </span><span style="color: #339933; font-family: &apos;Lucida Console&apos;; font-size: 8pt;">= </span><span style="font-family: &apos;Lucida Console&apos;; font-size: 8pt;">currentProtocol </span><span style="color: #339933; font-family: &apos;Lucida Console&apos;; font-size: 8pt;">+</span><span style="font-family: &apos;Lucida Console&apos;; font-size: 8pt;"> </span><span style="color: #3366cc; font-family: &apos;Lucida Console&apos;; font-size: 8pt;">"//"</span><span style="font-family: &apos;Lucida Console&apos;; font-size: 8pt;"> </span><span style="color: #339933; font-family: &apos;Lucida Console&apos;; font-size: 8pt;">+</span><span style="font-family: &apos;Lucida Console&apos;; font-size: 8pt;"> currentHostandPort</span><span style="color: #339933; font-family: &apos;Lucida Console&apos;; font-size: 8pt;">;</span></div> 

  1. <span style="font-family: "Calibri","sans-serif"; font-size: 11.0pt; mso-ascii-theme-font: minor-latin; mso-bidi-font-family: "Times New Roman"; mso-bidi-theme-font: minor-bidi; mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-hansi-theme-font: minor-latin;"
  2. </span> 

  1. <span style="font-family: "Calibri","sans-serif"; font-size: 11.0pt; mso-ascii-theme-font: minor-latin; mso-bidi-font-family: "Times New Roman"; mso-bidi-theme-font: minor-bidi; mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-hansi-theme-font: minor-latin;">The code below shows the modification made to the upload message.</span><span style="color: #006600; mso-bidi-font-style: italic;"><o:p></o:p></span> 

  1. <span style="font-family: "Calibri","sans-serif"; font-size: 11.0pt; mso-ascii-theme-font: minor-latin; mso-bidi-font-family: "Times New Roman"; mso-bidi-theme-font: minor-bidi; mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-hansi-theme-font: minor-latin;"
  2. </span> 

  1. <span style="background-color: #ffff99; font-family: &apos;Lucida Console&apos;; font-size: 8pt;">// Send and upload message to the webworker</span> 

  1. "background-color: #ffff99; font-family: &apos;Lucida Console&apos;; font-size: 8pt;"><span style="color: #000066;">case</span><span style="background-color: #ffff99; font-family: &apos;Lucida Console&apos;; font-size: 8pt;"> </span><span style="background-color: #ffff99; color: #3366cc; font-family: &apos;Lucida Console&apos;; font-size: 8pt;">&apos;upload&apos;</span><span style="background-color: #ffff99; color: #339933; font-family: &apos;Lucida Console&apos;; font-size: 8pt;">:</span> 

  1. <span style="background-color: #ffff99; font-family: &apos;Lucida Console&apos;; font-size: 8pt;">// </span><span style="background-color: #ffff99; font-family: &apos;Lucida Console&apos;; font-size: 8pt;">Check to see if backend supports parallel uploads</span> 

  1. var paralleluploads =false;   
  2. if ($('#select_parallelupload').prop('checked')) {   
  3.         paralleluploads = true;   
  4. }   
  5.   
  6. uploadworkers[data.id].postMessage({ 'chunk': data.blob, 'filename':data.filename,   
  7. 'directory': $("#select_directory").val(), 'chunkCount':data.chunkCount,   
  8. 'asyncstate':data.asyncstate,'paralleluploads':paralleluploads, 'currentlocation':   
  9. currentLocation, 'id': data.id });   
  10. break

***修改了 CelerFT 接口来支持并行上传.

带有并行上传的CelerFT

这个项目的代码可以再我的 github 资源库上找到

责任编辑:张伟 来源: oschina
相关推荐

2021-10-03 15:02:50

HTTPNodejs

2023-06-30 23:25:46

HTTP模块内存

2011-09-08 10:41:12

Node.js

2022-08-28 16:30:34

Node.jsDocker指令

2014-09-12 10:35:09

Node.jsHTTP 206

2023-01-10 14:11:26

2022-01-11 17:23:12

配置Node.jsNode

2011-09-08 10:32:27

Node.js

2016-09-18 16:04:24

HTTPNode应用

2013-11-01 09:34:56

Node.js技术

2015-03-10 10:59:18

Node.js开发指南基础介绍

2011-09-08 14:16:12

Node.js

2020-02-25 12:27:59

Node.jsWeb开发前端

2021-07-03 17:43:03

Node.jsNode变量

2017-04-24 08:31:26

Node.jsExpress.jsHTTP

2021-01-14 10:48:34

Docker CompNode.js开发

2020-05-29 15:33:28

Node.js框架JavaScript

2021-12-25 22:29:57

Node.js 微任务处理事件循环

2012-02-03 09:25:39

Node.js

2021-03-03 06:39:05

Nodejs前端开发
点赞
收藏

51CTO技术栈公众号