@大侠,如题
七牛sDk老多没回调,得自己封。
weblog.coding.io,其中上传头像是用七牛
https://github.com/hpgt/node-upload.git 一个不完整有效的列子,可以上传到七牛,只不过上传成功后回调还没写,可以自己摸索一下
@hezedu 没办法啊。不然用哪个啊
@hpgt 我的返回: { code: -1, error: ‘Error: ENOENT, open ‘C:\fakepath\2.png’’ }
服务端: var qs = require(“querystring”); var webot = require(‘weixin-robot’); var qiniu = require(‘qiniu’); qiniu.conf.ACCESS_KEY = ‘lJ5tagD530-rgDG6OkI3SZwkC7Xv5ByfHWfr1Bv5’; qiniu.conf.ACCESS_KEY = ‘J-nK8ZcNrRs4Nw7UVsI7N_ELPN7is2krQ8pqySTR’; var bucket = ‘carly32fileupload’; var putPolicy = new qiniu.rs.PutPolicy(bucket); putPolicy.callbackUrl = ‘http://carly.notes18.com:4000’; putPolicy.callbackBody = “name=$(key)”; var token = putPolicy.token(); exports.uploadFile = function(req, res, next){ var $url = url.parse(req.url).query; $url = qs.parse($url); var localFile = $url[“localFile”]; var key = $url[“key”]; var extra = new qiniu.io.PutExtra(); var uptoken = token; qiniu.io.putFile(uptoken, key, localFile, extra, function(err, ret) { if(!err) { // 上传成功, 处理返回值 res.json({ key:ret.key, hash:ret.hash }); // ret.key & ret.hash } else { // 上传失败, 处理返回代码 console.log(err); // http://developer.qiniu.com/docs/v6/api/reference/codes.html } }); }
客户端: <input type=“file” class=“form-control” name=“img” value=“上传图片” id=“upload”> $(’#upload’).change(function(e){ var localFile = $(this).val(); console.log(localFile); var key = ‘’; $.post(’/upload/img?localFile=’+localFile+’&key=’+key,{localFile:localFile, key:key}, function(data){}); });
最后返回的是: { code: -1,
error: 'Error: ENOENT, open ‘C:\fakepath\2.png’’ }
@carly1987 地址应该给错了,我在coding上部署的环境的确可以上传https://coding.net/hpgt/node-upload.git
七牛的话 ,用 qn 这个库,看看文档就懂用了,别用官方那个。