设为首页收藏本站

LUPA开源社区

 找回密码
 注册
文章 帖子 博客
LUPA开源社区 首页 业界资讯 技术文摘 查看内容

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

2014-11-4 11:34| 发布者: joejoe0332| 查看: 4457| 评论: 0|原作者: LeoXu, 开源中国榨菜, LeoG0816, daxiang, Cath, DYOS|来自: oschina

摘要: 近年来, Javascript越来越流行,这似乎是由于 Javascript 生态系统正在帮助提高生产率、减少入门所需的时间。 在我的 第一篇文章中,我介绍了使用 ASP.NET Web 后端 API 实现 G级文件上传,发表完这篇文章后,我决 ...


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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// 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并没有提供String.padLeft()方法,这是通过扩展String实现的。

1
2
3
4
5
6
7
8
9
10
11
// 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;  
}   ;

一些其它事情

  其中一件事是,发表上篇文章后我继续研究是为了通过域名碎片实现并行上传到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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<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>



酷毙

雷人

鲜花

鸡蛋

漂亮
  • 快毕业了,没工作经验,
    找份工作好难啊?
    赶紧去人才芯片公司磨练吧!!

最新评论

关于LUPA|人才芯片工程|人才招聘|LUPA认证|LUPA教育|LUPA开源社区 ( 浙B2-20090187 浙公网安备 33010602006705号   

返回顶部