CNode

分享一个node结合uploadify的异步上传例子。

Ffyddaben发布于13 年前最后回复12 年前5 回复9021 浏览0 收藏

服务器端配置: app.js==> app.use(express.bodyParser({ keepExtensions: true, uploadDir: __dirname + '/public/upload'})); index.js==>

`exports.upload = function(req, res){

     var fileDesc=req.files;

 var imgname=fileDesc.Filedata.name;
 
     var path=fileDesc.Filedata.path;//这里是本地路径D://upload/..jpg
 
     var index=path.indexOf(constant.uploadFile);//配置的放置文件的文件夹名字
 
     var name=path.substring(index);
 
 var imgurl=constant.DomainUrl+":"+constant.DomainPort+"/"+name;
 
 res.send(imgurl);//返回服务器端地址

};`

前台配置: test.js==>

$(function() {
	$('#file_upload').uploadify({
		 'swf'     : '/images/uploadify.swf',
		 'formData':{
	            'userid':'用户id',
	            'username':'用户名',
	            'rnd':'加密密文'
          },
          'cancelImg ':'/images/uploadify-cancel.png',
          'fileTypeExts' : '*.gif; *.jpg; *.png',
          'simUploadLimit ':'2',
		  'uploader' : '/fileupload',
		  'onUploadSuccess' : function(file, data, response) {
		          var img="<img src=\""+data+"\"/>";
		        
                              $("#img_box").append(img);
		        }
	});
});

html页面:

<link rel="stylesheet" type="text/css" href="/stylesheets/uploadify.css">
<style type="text/css">
    .input_frame{
    	height: 70px;
    }
    .img{
    	width: 550px;
    	position: absolute;
    	margin-top: 50px;
    }
    .img img{
    	width: 100px;
    	height: 100px;
        margin-left: 10px;
        z-index: -99;
    }
</style>

<div class="input_frame">
   <pre><input type="file" name="file_upload" id="file_upload" /></pre>
      </div>
</script>
查看回复

回复 (5)

Y
yyp544784013#1·13 年前

求例子源码

Q
qyy213#2·12 年前

楼主代码运行不通啊

Q
qyy213#3·12 年前

路由是exports.upload 但是提交的是 'uploader' : '/fileupload',啊? 这怎么解释的通呢

I
i5ting#4·12 年前

这是3的代码。。。。。

F
fyddaben#5·12 年前

奥。我的app.js里面配置的上传路径,是fileupload

参与回复
登录后即可参与回复。登录