亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

 更新時(shí)間:2015年06月23日 10:50:28   投稿:goldensun  
這篇文章主要介紹了在Node.js中使用HTTP上傳文件的方法,作者以windows下的visual studio作為操作node的環(huán)境,推薦閱讀!需要的朋友可以參考下

開(kāi)發(fā)環(huán)境
我們將使用 Visual Studio Express 2013 for Web 作為開(kāi)發(fā)環(huán)境, 不過(guò)它還不能被用來(lái)做 Node.js 開(kāi)發(fā)。為此我們需要安裝 Node.js Tools for Visual Studio。  裝好后 Visual Studio Express 2013 for Web 就會(huì)轉(zhuǎn)變成一個(gè) Node.js IDE 環(huán)境,提供創(chuàng)建這個(gè)應(yīng)用所需要的所有東西.。而基于這里提供的指導(dǎo),我們需要:

  •     下載安裝 Node.js  Windows 版,選擇適用你系統(tǒng)平臺(tái)的版本, Node.js (x86) 或者Node.js (x64) 。
  •     下載并安裝 Node.js 的 Visual Studio 工具。

安裝完成后我們就會(huì)運(yùn)行 Visual Studio Express 2013 for Web, 并使用 Node.js 的交互窗口來(lái)驗(yàn)證安裝. Node.js 的交互窗口可以再 View->Other Windows->Node.js Interactive Window 下找到. Node.js 交互窗口運(yùn)行后我們要輸入一些命令檢查是否一切OK.

2015623103550896.png (626×177)

Figure 1 Node.js Interactive Window

現(xiàn)在我們已經(jīng)對(duì)安裝進(jìn)行了驗(yàn)證,我們現(xiàn)在就可以準(zhǔn)備開(kāi)始創(chuàng)建支持GB級(jí)文件上傳的Node.js后臺(tái)程序了. 開(kāi)始我們先創(chuàng)建一個(gè)新的項(xiàng)目,并選擇一個(gè)空的 Node.js Web應(yīng)用程序模板.

2015623103701380.png (628×384)

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

項(xiàng)目創(chuàng)建好以后,我們應(yīng)該會(huì)看到一個(gè)叫做 server.js 的文件,還有解決方案瀏覽器里面的Node包管理器 (npm). 

2015623103722152.png (257×444)

圖3 解決方案管理器里面的 Node.js 應(yīng)用程序

server.js 文件里面有需要使用Node.js來(lái)創(chuàng)建一個(gè)基礎(chǔ)的hello world應(yīng)用程序的代碼.

2015623103740566.png (628×275)

Figure 4 The Hello World application
 我現(xiàn)在繼續(xù)把這段代碼從 server.js 中刪除,然后在Node.js中穿件G級(jí)別文件上傳的后端代碼。下面我需要用npm安裝這個(gè)項(xiàng)目需要的一些依賴(lài):

  •      Express - Node.js網(wǎng)頁(yè)應(yīng)用框架,用于構(gòu)建單頁(yè)面、多頁(yè)面以及混合網(wǎng)絡(luò)應(yīng)用
  •      Formidable - 用于解析表單數(shù)據(jù),特別是文件上傳的Node.js模塊
  •      fs-extra - 文件系統(tǒng)交互模塊

2015623103757338.png (585×424)

圖5 使用npm安裝所需模塊

模塊安裝完成后,我們可以從解決方案資源管理器中看到它們。

2015623103815638.png (287×488)

 圖6 解決方案資源管理器顯示已安裝模塊

下一步我們需要在解決方案資源管理器新建一個(gè) "Scripts" 文件夾并且添加  "workeruploadchunk.js" 和   "workerprocessfile.js" 到該文件夾。我們還需要下載jQuery 2.x 和  SparkMD5 庫(kù)并添加到"Scripts"文件夾。 最后還需要添加 "Default.html" 頁(yè)面。

 創(chuàng)建Node.js后臺(tái)

首先我們需要用Node.js的"require()"函數(shù)來(lái)導(dǎo)入在后臺(tái)上傳G級(jí)文件的模塊。注意我也導(dǎo)入了"path"以及"crypto" 模塊。"path"模塊提供了生成上傳文件塊的文件名的方法。"crypto" 模塊提供了生成上傳文件的MD5校驗(yàn)和的方法。

// The required modules  
var express = require('express');  
var formidable = require('formidable');  
var fs = require('fs-extra');  
var path = require('path'); 
var crypto = require('crypto');

下一行代碼就是見(jiàn)證奇跡的時(shí)刻。

復(fù)制代碼 代碼如下:
<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>

這行代碼是用來(lái)創(chuàng)建express應(yīng)用的。express應(yīng)用是一個(gè)封裝了Node.js底層功能的中間件。如果你還記得那個(gè)由Blank Node.js Web應(yīng)用模板創(chuàng)建的"Hello World" 程序,你會(huì)發(fā)現(xiàn)我導(dǎo)入了"http"模塊,然后調(diào)用了"http.CreateServer()"方法創(chuàng)建了 "Hello World" web應(yīng)用。我們剛剛創(chuàng)建的express應(yīng)用內(nèi)建了所有的功能。

現(xiàn)在我們已經(jīng)創(chuàng)建了一個(gè)express應(yīng)用,我們讓它呈現(xiàn)之前創(chuàng)建的"Default.html",然后讓?xiě)?yīng)用等待連接。

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

express應(yīng)用有app.VERB()方法,它提供了路由的功能。我們將使用app.post()方法來(lái)處理"UploadChunk" 請(qǐng)求。在app.post()方法里我們做的第一件事是檢查我們是否在處理POST請(qǐng)求。接下去檢查Content-Type是否是mutipart/form-data,然后檢查上傳的文件塊大小不能大于51MB。

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

 一旦我們成功通過(guò)了所有的檢查,我們將把上傳的文件塊作為一個(gè)單獨(dú)分開(kāi)的文件并將它按順序數(shù)字命名。下面最重要的代碼是調(diào)用fs.ensureDirSync()方法,它使用來(lái)檢查臨時(shí)目錄是否存在。如果目錄不存在則創(chuàng)建一個(gè)。注意我們使用的是該方法的同步版本。

// Get the extension from the file name 
var extension =path.extname(request.param('filename'));  
 
// Get the base file name 
var baseFilename =path.basename(request.param('filename'), extension);  
 
// Create the temporary file name for the chunk 
var tempfilename =baseFilename + '.'+  
request.param('chunkNumber').toString().padLeft('0', 16) + extension + ".tmp";  
 
 
// Create the temporary directory to store the file chunk 
// The temporary directory will be based on the file name 
var tempdir =uploadpath + request.param('directoryname')+ '/' + baseFilename;  
 
// The path to save the file chunk 
var localfilepath =tempdir + '/'+ tempfilename;  
 
if (fs.ensureDirSync(tempdir)) {  
 console.log('Created directory ' +tempdir); 
}

正如我之前提出的,我們可以通過(guò)兩種方式上傳文件到后端服務(wù)器。第一種方式是在web瀏覽器中使用FormData,然后把文件塊作為二進(jìn)制數(shù)據(jù)發(fā)送,另一種方式是把文件塊轉(zhuǎn)換成base64編碼的字符串,然后創(chuàng)建一個(gè)手工的multipart/form-data encoded請(qǐng)求,然后發(fā)送到后端服務(wù)器。  

所以我們需要檢查一下是否在上傳的是一個(gè)手工multipart/form-data encoded請(qǐng)求,通過(guò)檢查"CelerFT-Encoded"頭部信息,如果這個(gè)頭部存在,我們創(chuàng)建一個(gè)buffer并使用request的ondata時(shí)間把數(shù)據(jù)拷貝到buffer中。

在request的onend事件中通過(guò)將buffer呈現(xiàn)為字符串并按CRLF分開(kāi),從而從 multipart/form-data encoded請(qǐng)求中提取base64字符串。base64編碼的文件塊可以在數(shù)組的第四個(gè)索引中找到。

通過(guò)創(chuàng)建一個(gè)新的buffer來(lái)將base64編碼的數(shù)據(jù)重現(xiàn)轉(zhuǎn)換為二進(jìn)制。隨后調(diào)用fs.outputFileSync()方法將buffer寫(xiě)入文件中。

// Check if we have uploaded a hand crafted multipart/form-data request 
// If we have done so then the data is sent as a base64 string 
// and we need to extract the base64 string and save it 
if (request.headers['celerft-encoded']=== 'base64') {  
 
 var fileSlice = newBuffer(+request.headers['content-length']);  
 var bufferOffset = 0;  
 
 // Get the data from the request 
 request.on('data', function (chunk) {  
  chunk.copy(fileSlice , bufferOffset);  
  bufferOffset += chunk.length;  
 }).on('end', function() {  
  // Convert the data from base64 string to binary 
  // base64 data in 4th index of the array 
  var base64data = fileSlice.toString().split('\r\n');  
  var fileData = newBuffer(base64data[4].toString(), 'base64');  
 
  fs.outputFileSync(localfilepath,fileData);  
  console.log('Saved file to ' +localfilepath);  
 
  // Send back a sucessful response with the file name 
  response.status(200).send(localfilepath);  
  response.end();  
 }); 
}

二進(jìn)制文件塊的上傳是通過(guò)formidable模塊來(lái)處理的。我們使用formidable.IncomingForm()方法得到multipart/form-data encoded請(qǐng)求。formidable模塊將把上傳的文件塊保存為一個(gè)單獨(dú)的文件并保存到臨時(shí)目錄。我們需要做的是在formidable的onend事件中將上傳的文件塊保存為里一個(gè)名字。

else {  
 // The data is uploaded as binary data.  
 // We will use formidable to extract the data and save it  
 var form = new formidable.IncomingForm();  
 form.keepExtensions = true;  
 form.uploadDir = tempdir;  
 
 // Parse the form and save the file chunks to the  
 // default location  
 form.parse(request, function (err, fields, files) {  
  if (err){  
   response.status(500).send(err);  
   return;  
  }  
 
 //console.log({ fields: fields, files: files });  
 });  
 
 // Use the filebegin event to save the file with the naming convention  
 /*form.on('fileBegin', function (name, file) { 
 file.path = localfilepath; 
});*/  
 
form.on('error', function (err) {  
  if (err){  
   response.status(500).send(err);  
   return;  
  }  
 });  
 
 // After the files have been saved to the temporary name  
 // move them to the to teh correct file name  
 form.on('end', function (fields,files) {  
  // Temporary location of our uploaded file    
  var temp_path = this.openedFiles[0].path;  
 
  fs.move(temp_path , localfilepath,function (err){  
 
   if (err) {  
    response.status(500).send(err);  
    return;  
   }  
   else {  
    // Send back a sucessful response with the file name  
    response.status(200).send(localfilepath);  
    response.end();  
   }  
  });  
 });  
 
// Send back a sucessful response with the file name  
//response.status(200).send(localfilepath);  
//response.end();  
} 
}

app.get()方法使用來(lái)處理"MergeAll"請(qǐng)求的。這個(gè)方法實(shí)現(xiàn)了之前描述過(guò)的功能。

// Request to merge all of the file chunks into one file 
app.get('*/api/CelerFTFileUpload/MergeAll*', function(request,response) {  
 
 if (request.method === 'GET') {  
 
  // Get the extension from the file name 
  var extension =path.extname(request.param('filename'));  
 
  // Get the base file name 
  var baseFilename =path.basename(request.param('filename'), extension);  
 
  var localFilePath =uploadpath + request.param('directoryname')+ '/' + baseFilename;  
 
  // Check if all of the file chunks have be uploaded 
  // Note we only wnat the files with a *.tmp extension 
  var files =getfilesWithExtensionName(localFilePath, 'tmp')  
  /*if (err) { 
   response.status(500).send(err); 
   return; 
  }*/ 
 
  if (files.length !=request.param('numberOfChunks')){  
   response.status(400).send('Number of file chunks less than total count');  
   return;  
  }  
 
  var filename =localFilePath + '/'+ baseFilename +extension;  
  var outputFile =fs.createWriteStream(filename);  
 
  // Done writing the file 
  // Move it to top level directory 
  // and create MD5 hash 
  outputFile.on('finish', function (){  
   console.log('file has been written');  
   // New name for the file 
   var newfilename = uploadpath +request.param('directoryname')+ '/' + baseFilename 
   + extension;  
 
   // Check if file exists at top level if it does delete it 
   //if (fs.ensureFileSync(newfilename)) { 
   fs.removeSync(newfilename);  
   //} 
 
   // Move the file 
   fs.move(filename, newfilename ,function (err) {  
    if (err) {  
     response.status(500).send(err);  
     return;  
    }  
    else {  
     // Delete the temporary directory 
     fs.removeSync(localFilePath);  
     varhash = crypto.createHash('md5'),  
      hashstream = fs.createReadStream(newfilename);  
 
     hashstream.on('data', function (data) {  
      hash.update(data)  
     });  
 
     hashstream.on('end', function (){  
      var md5results =hash.digest('hex');  
      // Send back a sucessful response with the file name 
      response.status(200).send('Sucessfully merged file ' + filename + ", "  
      + md5results.toUpperCase());  
      response.end();  
     });  
    }  
   });  
  });  
 
  // Loop through the file chunks and write them to the file 
  // files[index] retunrs the name of the file. 
  // we need to add put in the full path to the file 
  for (var index infiles) {  
   console.log(files[index]);  
   var data = fs.readFileSync(localFilePath +'/' +files[index]);  
   outputFile.write(data);  
   fs.removeSync(localFilePath + '/' + files[index]);  
  }  
  outputFile.end();  
 } 
 
}) ;

注意Node.js并沒(méi)有提供String.padLeft()方法,這是通過(guò)擴(kuò)展String實(shí)現(xiàn)的。

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

其中一件事是,發(fā)表上篇文章后我繼續(xù)研究是為了通過(guò)域名碎片實(shí)現(xiàn)并行上傳到CeleFT功能。域名碎片的原理是訪問(wèn)一個(gè)web站點(diǎn)時(shí),讓web瀏覽器建立更多的超過(guò)正常允許范圍的并發(fā)連接。 域名碎片可以通過(guò)使用不同的域名(如web1.example.com,web2.example.com)或者不同的端口號(hào)(如8000, 8001)托管web站點(diǎn)的方式實(shí)現(xiàn)。

示例中,我們使用不同端口號(hào)托管web站點(diǎn)的方式。

我們使用 iisnode 把 Node.js集成到 IIS( Microsoft Internet Information Services)實(shí)現(xiàn)這一點(diǎn)。 下載兼容你操作系統(tǒng)的版本 iisnode (x86) 或者  iisnode (x64)。 下載 IIS URL重寫(xiě)包。

一旦安裝完成(假定windows版Node.js已安裝),到IIS管理器中創(chuàng)建6個(gè)新網(wǎng)站。將第一個(gè)網(wǎng)站命名為CelerFTJS并且將偵聽(tīng)端口配置為8000。

2015623103842932.png (546×529)

圖片7在IIS管理器中創(chuàng)建一個(gè)新網(wǎng)站

然后創(chuàng)建其他的網(wǎng)站。我為每一個(gè)網(wǎng)站都創(chuàng)建了一個(gè)應(yīng)用池,并且給應(yīng)用池“LocalSystem”級(jí)別的權(quán)限。所有網(wǎng)站的本地路徑是C:\inetpub\wwwroot\CelerFTNodeJS。

2015623103905433.png (628×395)

圖片8 文件夾層級(jí)

我在Release模式下編譯了Node.js應(yīng)用,然后我拷貝了server.js文件、Script文件夾以及node_modules文件夾到那個(gè)目錄下。
 要讓包含 iisnode 的Node.js的應(yīng)用工作,我們需要?jiǎng)?chuàng)建一個(gè)web.config文件,并在其中添加如下得內(nèi)容。

 

<defaultDocument> 
 <files> 
  <add value="server.js" /> 
 </files> 
 </defaultDocument> 
 
 <handlers> 
 <!-- indicates that the server.js file is a node.js application to be handled by the  
 iisnode module -->  
 <add name="iisnode" path="*.js" verb="*" modules="iisnode" /> 
 </handlers> 
 
 <rewrite> 
 <rules> 
  <rule name="CelerFTJS"> 
  <match url="/*" /> 
  <action type="Rewrite" url="server.js" /> 
  </rule> 
 
  <!-- Don't interfere with requests for node-inspector debugging -->  
  <rule name="NodeInspector" patternSyntax="ECMAScript" stopProcessing="true"> 
  <match url="^server.js\/debug[\/]?" /> 
  </rule> 
 </rules> 
 </rewrite>

web.config中各項(xiàng)的意思是讓iisnode處理所有得*.js文件,由server.js 處理任何匹配"/*"的URL。 

2015623103925540.png (628×210)

 如果你正確的做完了所有的工作,你就可以通過(guò)http://localhost:8000瀏覽網(wǎng)站,并進(jìn)入CelerFT "Default.html"頁(yè)面。

下面的web.config項(xiàng)可以改善 iisnode中Node.js的性能。

復(fù)制代碼 代碼如下:
<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>

 并行上傳

為了使用域名碎片來(lái)實(shí)現(xiàn)并行上傳,我不得不給Node.js應(yīng)用做些修改。我第一個(gè)要修改的是讓Node.js應(yīng)用支持跨域資源共享。我不得不這樣做是因?yàn)槭褂糜蛩槠瑢?shí)際上是讓一個(gè)請(qǐng)求分到不同的域并且同源策略會(huì)限制我的這個(gè)請(qǐng)求。

好消息是XMLttPRequest 標(biāo)準(zhǔn)2規(guī)范允許我這么做,如果網(wǎng)站已經(jīng)把跨域資源共享打開(kāi),更好的是我不用為了實(shí)現(xiàn)這個(gè)而變更在"workeruploadchunk.js"里的上傳方法。

// 使用跨域資源共享 // Taken from http://bannockburn.io/2013/09/cross-origin-resource-sharing-cors-with-a-node-js-express-js-and-sencha-touch-app/ 
var enableCORS = function(request,response, next){  
 response.header('Access-Control-Allow-Origin', '*');  
 response.header('Access-Control-Allow-Methods', 'GET,POST,OPTIONS');  
 response.header('Access-Control-Allow-Headers', 'Content-Type, Authorization, Content- 
     Length, X-Requested-With' ) ; 
 
 
 // 攔截OPTIONS方法
 if ('OPTIONS' ==request.method){  
  response.send(204);  
 }  
 else {  
  next();  
 }  
} ;  
 
// 在表達(dá)式中使用跨域資源共享
app. use ( enableCORS ) ;

 為了使server.js文件中得CORS可用,我創(chuàng)建了一個(gè)函數(shù),該函數(shù)會(huì)創(chuàng)建必要的頭以表明Node.js應(yīng)用支持CORS。另一件事是我還需要表明CORS支持兩種請(qǐng)求,他們是:

    簡(jiǎn)單請(qǐng)求:

             1、只用GET,HEAD或POST。如果使用POST向服務(wù)器發(fā)送數(shù)據(jù),那么發(fā)送給服務(wù)器的HTTP POST請(qǐng)求的Content-Type應(yīng)是application/x-www-form-urlencoded, multipart/form-data, 或 text/plain其中的一個(gè)。

          2、HTTP請(qǐng)求中不要設(shè)置自定義的頭(例如X-Modified等)

    預(yù)檢請(qǐng)求:

           1、使用GET,HEAD或POST以外的方法。假設(shè)使用POST發(fā)送請(qǐng)求,那么Content-Type不能是application/x-www-form-urlencoded, multipart/form-data, or text/plain,例如假設(shè)POST請(qǐng)求向服務(wù)器發(fā)送了XML有效載荷使用了application/xml or text/xml,那么這個(gè)請(qǐng)求就是預(yù)檢的。

            2、在請(qǐng)求中設(shè)置自定義頭(比如請(qǐng)求使用X-PINGOTHER頭)。

在我們的例子中,我們用的是簡(jiǎn)單請(qǐng)求,所以我們不需要做其他得工作以使例子能夠工作。

在  "workeruploadchunk.js" 文件中,我向  self.onmessage 事件添加了對(duì)進(jìn)行并行文件數(shù)據(jù)塊上傳的支持. 

// We are going to upload to a backend that supports parallel uploads. 
// Parallel uploads is supported by publishng the web site on different ports 
// The backen must implement CORS for this to work 
else if(workerdata.chunk!= null&& workerdata.paralleluploads ==true){  
 if (urlnumber >= 6) {  
  urlnumber = 0;  
 }  
 
 if (urlcount >= 6) {  
  urlcount = 0;  
 }  
 
 if (urlcount == 0) {  
  uploadurl = workerdata.currentlocation +webapiUrl + urlnumber;  
 }  
 else {  
  // Increment the port numbers, e.g 8000, 8001, 8002, 8003, 8004, 8005 
  uploadurl = workerdata.currentlocation.slice(0, -1) + urlcount +webapiUrl +  
  urlnumber;  
 }  
 
 upload(workerdata.chunk,workerdata.filename,workerdata.chunkCount, uploadurl,  
 workerdata.asyncstate);  
 urlcount++;  
 urlnumber++; 
 }

在 Default.html 頁(yè)面我對(duì)當(dāng)前的URL進(jìn)行了保存,因?yàn)槲覝?zhǔn)備把這些信息發(fā)送給文件上傳的工作程序. 只所以這樣做是因?yàn)? 

  •     我想要利用這個(gè)信息增加端口數(shù)量
  •     做了 CORS 請(qǐng)求,我需要把完整的 URL 發(fā)送給 XMLHttpRequest 對(duì)象.

 

復(fù)制代碼 代碼如下:
<div class="MsoNoSpacing" style="background: #FFFF99;">
<span style="font-family: "Lucida Console"; font-size: 8.0pt;">// Save current protocol and host for parallel uploads</span></div>
<div class="MsoNoSpacing" style="background: #FFFF99;">
"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>
<div class="MsoNoSpacing" style="background: #FFFF99;">
"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>
<div class="MsoNoSpacing" style="background: #FFFF99;">
"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;">"http://"</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>

<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;">
</span>

<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>

<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;">
</span>

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

"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>

<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>

var paralleluploads =false; 
if ($('#select_parallelupload').prop('checked')) { 
        paralleluploads = true; 

 
uploadworkers[data.id].postMessage({ 'chunk': data.blob, 'filename':data.filename, 
'directory': $("#select_directory").val(), 'chunkCount':data.chunkCount, 
'asyncstate':data.asyncstate,'paralleluploads':paralleluploads, 'currentlocation': 
currentLocation, 'id': data.id }); 
break;

最后修改了 CelerFT 接口來(lái)支持并行上傳.

2015623104005997.png (628×344)

帶有并行上傳的CelerFT

這個(gè)項(xiàng)目的代碼可以再我的 github 資源庫(kù)上找到

相關(guān)文章

  • 詳解離線安裝npm包的幾種方法

    詳解離線安裝npm包的幾種方法

    這篇文章主要介紹了詳解離線安裝npm包的幾種方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-11-11
  • NodeJS制作爬蟲(chóng)全過(guò)程(續(xù))

    NodeJS制作爬蟲(chóng)全過(guò)程(續(xù))

    本文是接上篇NodeJS制作爬蟲(chóng)全過(guò)程,是最上文的一個(gè)補(bǔ)充以及優(yōu)化,給需要的小伙伴參考下
    2014-12-12
  • npm全局環(huán)境變量配置詳解

    npm全局環(huán)境變量配置詳解

    這篇文章主要介紹了npm全局環(huán)境變量配置詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-12-12
  • nodejs+websocket實(shí)時(shí)聊天系統(tǒng)改進(jìn)版

    nodejs+websocket實(shí)時(shí)聊天系統(tǒng)改進(jìn)版

    這篇文章主要介紹了nodejs+websocket實(shí)時(shí)聊天系統(tǒng)的改進(jìn)版,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-05-05
  • nodeJS模塊簡(jiǎn)單用法示例

    nodeJS模塊簡(jiǎn)單用法示例

    這篇文章主要介紹了nodeJS模塊簡(jiǎn)單用法,結(jié)合實(shí)例形式簡(jiǎn)單分析了nodejs模塊定義、引入、注冊(cè)、啟動(dòng)等相關(guān)操作技巧,需要的朋友可以參考下
    2018-04-04
  • Elasticsearch插件及nodejs的安裝配置

    Elasticsearch插件及nodejs的安裝配置

    這篇文章主要為大家介紹了Elasticsearch插件及nodejs的安裝配置,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-04-04
  • 基于游標(biāo)的分頁(yè)接口實(shí)現(xiàn)代碼示例

    基于游標(biāo)的分頁(yè)接口實(shí)現(xiàn)代碼示例

    這篇文章主要給大家介紹了關(guān)于基于游標(biāo)的分頁(yè)接口實(shí)現(xiàn)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2018-11-11
  • node.js 抓取代理ip實(shí)例代碼

    node.js 抓取代理ip實(shí)例代碼

    這篇文章主要介紹了node.js 抓取代理ip實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下
    2017-04-04
  • vscode 調(diào)試 node.js的方法步驟

    vscode 調(diào)試 node.js的方法步驟

    這篇文章主要介紹了vscode 調(diào)試 node.js的方法步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-09-09
  • nodejs不用electron實(shí)現(xiàn)打開(kāi)文件資源管理器并選擇文件

    nodejs不用electron實(shí)現(xiàn)打開(kāi)文件資源管理器并選擇文件

    最近在開(kāi)發(fā)一些小腳本,用 nodejs 實(shí)現(xiàn),其中很多功能需要選擇一個(gè)/多個(gè)文件,或者是選擇一個(gè)文件夾,這種情況下網(wǎng)上給出的解決方案都是 electron,但是我一個(gè)小腳本用 electron 屬實(shí)有點(diǎn)夸張了,后來(lái)轉(zhuǎn)念一想可以通過(guò) powershell 來(lái)實(shí)現(xiàn)類(lèi)似的功能,需要的朋友可以參考下
    2024-01-01

最新評(píng)論