All posts by 龙生
解决Node.js调用fs.renameSync报错的问题(Error: EXDEV, cross-device link not permitted)
今天开始学习Node.js,在写一个文件上传的功能时候,调用fs.renameSync方法错误 出错代码所在如下:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
1 function upload(response,request){ 2 console.log("upload called"); 3 var form = new formidable.IncomingForm(); 4 console.log("about to parse"); 5 form.parse(request, function(error, fields, files) { 6 console.log("parsing done"); 7 fs.renameSync(files.upload.path, "./tmp/test.jpg"); 8 response.writeHead(200, {"Content-Type": "text/html"}); 9 response.write("received image:<br/>"); 10 response.write("<img src='/show' />"); 11 response.end(); 12 }); 13 } |
大致分析后,预计是因为跨磁盘分区移动或操作文件会有权限问题。 下面提供两种解决办法: 方法一: 主要利用fs的createReadStream、createWriteSream和unlinkSync方法 具体代码如下:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
1 function upload(response,request){ 2 console.log("upload called"); 3 var form = new formidable.IncomingForm(); 4 console.log("about to parse"); 5 form.parse(request, function(error, fields, files) { 6 console.log("parsing done"); 7 // fs.renameSync(files.upload.path, "./tmp/test.jpg"); 8 <strong>var readStream=fs.createReadStream(files.upload.path); 9 var writeStream=fs.createWriteStream("./tmp/test.jpg"); 10 readStream.pipe(writeStream); 11 readStream.on('end',function(){ 12 fs.unlinkSync(files.upload.path); 13 }); </strong>14 response.writeHead(200, {"Content-Type": "text/html"}); 15 response.write("received image:<br/>"); 16 response.write("<img src='/show' />"); 17 response.end(); 18 }); 19 } |
PS:我用的node版本是0.10.69,如果使用的是0.6以下的版本,可以使用util.pump 相应代码只需将上面的代码中readStream.on处改成:(注意引入util模块)
|
1 2 3 |
util.pump(readStream,writeStream, function() { fs.unlinkSync('files.upload.path'); }); |
方法二: 这种就简洁很多了 添加一个 form.uploadDir=’tmp' 即可(写一个临时路径)
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
1 function upload(response,request){ 2 console.log("upload called"); 3 var form = new formidable.IncomingForm(); 4 <strong> form.uploadDir='tmp'; </strong> 5 6 console.log("about to parse"); 7 form.parse(request, function(error, fields, files) { 8 console.log("parsing done"); 9 fs.renameSync(files.upload.path, "./tmp/test.jpg"); 10 response.writeHead(200, {"Content-Type": "text/html"}); 11 response.write("received image:<br/>"); 12 response.write("<img src='/show' />"); 13 response.end(); 14 }); 15 } 16 |
解决问题后,就可以很开心的继续我的Node学习了,感觉路还很长啊 PS:附上两个有关Node文件上传的帖子,个人觉得挺不错的,来源都是cnode nodejs-post文件上传原理详解 node-formidable详解 from:https://www.cnblogs.com/glczero/archive/2014/08/23/3932024.html
View Details未找到与约束ContractName,无法打开项目的解决方案
如果VS2013,在打开解决方案时,报如下错误: “未找到与约束 ContractName Microsoft.Internal.VisualStudio.PlatformUI.ISolutionAttachedCollectionService RequiredTypeIdentity Microsoft.Internal.VisualStudio.PlatformUI.ISolutionAttachedCollectionService 匹配的导出” 导致项目无法打开以及VS无法关闭。 解决方法: 1.关闭VS; 2.去C:/Users/<your users name>/AppData/Local/Microsoft/VisualStudio/12.0/ComponentModelCache文件夹下删除所有文件及文件夹; 3.重新打开VS即可。 如果是vs2012的话 方法一: 可以尝试删除最近更新的windows补丁更新,主要是关于.net Framework的。 如果方法一行不通,可以尝试方法二,或直接用方法二解决。 方法二: 安装微软的windows补丁 KB2781514(官网:https://www.microsoft.com/zh-cn/download/details.aspx?id=36020) ,补丁主要解决“在 .NET Framework 4.5 更新之后,Visual Studio 用户可能无法打开或创建 C++ 或 JavaScript 文件或项目。” from:https://www.cnblogs.com/ChineseMoonGod/p/5687521.html
View Details从一份配置清单详解Nginx服务器配置
在前面《Nginx服务器开箱体验》 一文中我们从开箱到体验,感受了一下Nginx服务器的魅力。Nginx是轻量级的高性能Web服务器,提供了诸如HTTP代理和反向代理、负载均衡、缓存等一系列重要特性,因而在实践之中使用广泛,笔者也在学习和实践之中。
在本文中,我们继续延续前文,从前文给出的一份示例配置清单开始,详解一下Nginx服务器的各种配置指令的作用和用法。
PowerDesigner中NAME和COMMENT的互相转换,需要执行语句
使用说明: 在【Tools】-【Execute Commands】-【Edit/Run Script】 下。输入下面你要选择的语句即可,也可以保存起来,以便下次使用,后缀为.vbs。 需要注意的问题是:运行语句时必须在Module模式下,如果是导出报表时执行会出现错误提示。 1.Name转到Comment注释字段。一般情况下只填写NAME,COMMENT可以运行语句自动生成。 将该语句保存为name2comment.vbs
|
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 |
Option Explicit ValidationMode = True InteractiveMode = im_Batch Dim mdl ' the current model ' get the current active model Set mdl = ActiveModel If (mdl Is Nothing) Then MsgBox "There is no current Model " ElseIf Not mdl.IsKindOf(PdPDM.cls_Model) Then MsgBox "The current model is not an Physical Data model. " Else ProcessFolder mdl End If ' This routine copy name into comment for each table, each column and each view ' of the current folder Private sub ProcessFolder(folder) Dim Tab 'running table for each Tab in folder.tables if not tab.isShortcut then if trim(tab.comment)="" then'如果有表的注释,则不改变它.如果没有表注释.则把name添加到注释里面. tab.comment = tab.name end if Dim col ' running column for each col in tab.columns if trim(col.comment)="" then '如果col的comment为空,则填入name,如果已有注释,则不添加;这样可以避免已有注释丢失. col.comment= col.name end if next end if next Dim view 'running view for each view in folder.Views if not view.isShortcut and trim(view.comment)="" then view.comment = view.name end if next ' go into the sub-packages Dim f ' running folder For Each f In folder.Packages if not f.IsShortcut then ProcessFolder f end if Next end sub |
2.将Comment内容保存到NAME中,comment2name.vbs 实习互换。语句为:
|
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 |
Option Explicit ValidationMode = True InteractiveMode = im_Batch Dim mdl ' the current model ' get the current active model Set mdl = ActiveModel If (mdl Is Nothing) Then MsgBox "There is no current Model " ElseIf Not mdl.IsKindOf(PdPDM.cls_Model) Then MsgBox "The current model is not an Physical Data model. " Else ProcessFolder mdl End If Private sub ProcessFolder(folder) On Error Resume Next Dim Tab 'running table for each Tab in folder.tables if not tab.isShortcut then tab.name = tab.comment Dim col ' running column for each col in tab.columns if col.comment="" then else col.name= col.comment end if next end if next Dim view 'running view for each view in folder.Views if not view.isShortcut then view.name = view.comment end if next ' go into the sub-packages Dim f ' running folder For Each f In folder.Packages if not f.IsShortcut then ProcessFolder f end if Next end sub |
from:https://www.cnblogs.com/netsql/archive/2010/05/24/1742734.html
View DetailsPowerDesigner从Sqlserver中反转为带注释的字典及快捷键操作
PowerDesigner的操作经常忘记,所以把常用的功能记录下来备忘。 1、修改反转过来的字段 PowerDesigner从数据库反转的时候,默认不带注释,需要先进行修改。 输入如下脚本: 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 {OWNER, TABLE, S, COLUMN, DTTPCODE, LENGTH, SIZE, PREC, COMPUTE, NOTNULL, IDENTITY, DOMAIN, DEFAULT, COMMENT, ExtIdentityIncrement, ExtIdentitySeed} select u.name, o.name, c.colid, c.name, case when (s.usertype < 100) then s.name else x.name end, c.prec, c.length, c.scale, z.text , case (c.status & 8) when 8 then 'NULL' else 'NOTNULL' end, case (c.status & 128) when 128 then 'identity' else " end, case when (s.usertype < 100) then " else s.name end, v.text, CONVERT(varchar, ISNULL(p.[value], ")) AS text, case (c.status & 128) when 128 then ident_incr(u.name + '.' + o.name) else null end, case (c.status & 128) when 128 then ident_seed(u.name + '.' + o.name) else null end from dbo.sysusers u join dbo.sysobjects o on (o.uid = u.uid and o.type in ('U', 'S', 'V')) […]
View DetailsMysql 忘记root密码的完美解决方法
一、更改my.cnf配置文件 1.用命令编辑/etc/my.cnf配置文件,即:vim /etc/my.cnf 或者 vi /etc/my.cnf 2.在[mysqld]下添加skip-grant-tables,然后保存并退出 3.重启mysql服务:service mysqld restart 二、更改root用户名 1.重启以后,执行mysql命令进入mysql命令行 2.修改root用户密码 1 2 3 MySQL> UPDATE mysql.user SET Password=PASSWORD('新密码') where USER=’root'; MySQL> flush privileges; MySQL> exit 3.把/etc/my.cnf中的skip-grant-tables注释掉,然后重启mysql,即:service mysqld restart 好了,下面就可以用root新的密码登录了! 以上所述是小编给大家介绍的Mysql 忘记root密码处理办法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持! from:https://www.jb51.net/article/100925.htm
View Details代码规范审查 -Sonar环境搭建
Sonar概述 Sonar 是一个用于代码质量管理的开放平台,可以进行代码质量的持续跟踪审查,支持的语言包含C#、java、PHP、C等。可以通过UI一睹Sonar的强大之处。 Sonar安装 Sonar是一个基于java的开源平台,环境安装包含JDK安装、数据库安装、Sonar Server安装、Sonar Runner安装。 一、JDK安装 下载java SDK ,下载地址:http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html 安装java sdk,直接双击exe进行运行 验证java安装是否成功,打开命令行,运行java –version, 如果显示如下图,代表安装成功! 二、数据库安装 Sonar支持数据库类型:Sql server、MySQL、Oracle,此处以MySql为例 Mysql数据库下载地址:https://www.mysql.com/downloads/, 具体安装步骤省略可自行学习。 配置Sonar数据库(创建Sonar数据库、用户等) CREATE DATABASE sonar CHARACTER SET utf8 COLLATE utf8_general_ci; CREATE USER 'sonar' IDENTIFIED BY 'sonar'; GRANT ALL ON sonar.* TO 'sonar’@’%' IDENTIFIED BY 'sonar'; GRANT ALL ON sonar.* TO 'sonar’@’localhost' IDENTIFIED BY 'sonar'; FLUSH PRIVILEGES; 三、Sonar server及scanner配置 下载sonar(只需下载解压,无需安装) 下载地址:https://www.sonarqube.org/downloads/ 下载sonar scanner(只需下载解压,无需安装) 下载地址:https://docs.sonarqube.org/display/SCAN/Analyzing+with+SonarQube+Scanner 添加snoar和snoar scanner到环境变量 添加SONAR_HOME、SONAR_RUNNER_HOME环境变量,并将SONAR_RUNNER_HOME加入PATH环境变量参考如下: SONAR_HOME:C:\sonar\sonarqube-6.2 SONAR_RUNNER_HOME:C:\sonar\Sonar-scanner-2.8 PATH:%JAVA_HOME%\bin;%JAVA_HOME%\jre\bin;%SONAR_RUNNER_HOME%/bin;%MAVEN_HOME%\bin; 编辑sonar配置文件,修改如下: 编辑sonar\sonarqube-6.2\conf\sonar.properties文件,配置数据库设置,默认已经提供各类数据库的支持,这里使用mysql,因此取消mysql模块的注释,修改内容如下: 编辑SonarRunner配置文件,修改如下: 编辑Sonar-scanner-2.8\conf\conf\sonar-runner.properties文件,配置数据库设置,默认已经提供各类数据库的支持,这里使用mysql,因此取消mysql模块的注释,修改内容如下: 启动sonarQube服务,从sonarqube-6.2\bin\windows-x86-64目录下双击启动StartSonar.bat,启动成功显示如下: 访问http:\localhost:9000检测是否启动成功 安装中文包 […]
View DetailsWin10自带的虚拟机Hyper-V安装Centos7
1、设置开启Hyper-V应用程序 在搜索功能里输入 Hyper-V 然后点击选中的部分 2、全部选中框中的部分,然后重新启动电脑 3、在搜索功能里输入Hyper-V 打开 4、点击新建--> 下一步--> 5、修改名称 和虚拟机存储位置 6、这里一定要选择“第一代”,不然无法正常启动到安装界面 7、分配内存根据自己情况来设置 8、网络配置(网络设置的 专用虚拟机交换机为无线网卡 设置 见下图 步骤:) 专用虚拟机交换机为无线网卡 设置 步骤 9、连接虚拟硬盘 10、安装选项 选中镜像位置 11、设置信息如下:通过摘要查看 (最后点击完成后自动创建虚拟机) 12、选择创建好的虚拟机 先启动-->连接 13、安装Centos7 选中图中部分 进入安装界面 14、选择语言 最后选择英文哦 15、点击 Done 选择自动分区 16、开始安装 : 1. 设置root密码 2.创建用户 17、root密码设置 18、等待重启 然后用root用户登陆 密码是刚才设置的 18、网络配置设置 修改配置文件 网络桥接右键 桥接(桥接完成之后记得通过属性修改网桥的IP地址 手动设置) from:https://blog.csdn.net/xuanalex/article/details/79943206
View Detailslaravel 查询构建器(连贯操作)
注:laravel 查询返回行的都是 php 的 stdClass 对象实例,不是数组!!!! 1)查询多行(get)
|
1 |
DB::table('table_name')->get(); |
带偏移和限制的查询(skip take 或 offset limit)(两种用法是一样的)
|
1 2 3 4 5 |
//skip take DB::table('table_name')->skip(10)->take(10)->get(); //offset limit DB::table('table_name')->offset(20)->limit(10)->get(); |
2)查询一行(first)
|
1 |
DB::table('table_name')->first(); |
|
1 |
DB::table('table_name')->find(1); |
PS:find方法也可以查多行,它可以这样玩 DB::table('table_name')->find([1,2,3,4]); 3)直接查询一个字段(value)
|
1 |
DB::table('table_name')->where('name','Tiac')->value('email'); |
4)查询一列(pluck)
|
1 |
DB::table('table_name')->where('brand_id','100')->pluck('goods_id'); |
5)块组结果集(chunk) 使用情景: 假设我们需要查询 1 百万的数据,并对其做处理,一次性查询 1 百万并统一处理势必会对数据产生不小的压力,消耗大量的服务器资源,有没有一种更做优的处理方法? 将 1 百万的查询分成 1000 次 1000 条记录的查询的块查询怎样?? 这个 chunk的作用,chunk方法接口一个闭包函数做回调处理
|
1 2 3 4 5 |
DB::table('users')->orderBy('id')->chunk(1000, function($users) { foreach ($users as $user) { // } }); |
PS:上面只是举例,chunk 貌似不能和 limit 一起用,chunk 默认情况下一次性处理整张表的数据,不过我可以通过自己加判断用 return false 跳出 chunk 操作 6)聚合函数(count max min avg sum 等等)
|
1 |
DB::table('table_name')->count(); |
PS:其他聚合函数的使用方法一样的,不一一举例了 7)where 条件字句 这个比较多,懒得写了,想看的直接到 laravel 学院看吧,传送门:http://laravelacademy.org/post/6140.html#ipt_kb_toc_6140_8 (where, orWhere, whereNUll, whereIn, whereNotIn, whereBetween, whereNotBetween, whereDate, whereYear, whereMonth, whereDay 等等) 8)orderBy 排序字句
|
1 |
DB::table('table_name')->orderBy('id','desc')->get(); |
PS:如果想要返回随机的查询结果集,可以使用 inRandomOrder 子句 […]
View Detailslaravel5.0 groupBy中count的写法
|
1 2 3 4 |
$user_info = DB::table('usermetas') ->select('browser', DB::raw('count(*) as total')) ->groupBy('browser') ->get(); |
from:https://stackoverflow.com/questions/18533080/laravel-eloquent-groupby-and-also-return-count-of-each-group
View Details