如何在meteor中使用upyun又拍云或者七牛上传/下载文件
已经卡在这好多天。。。看了好几遍文档,sdk,完全没有头绪。。。
请高手指教
jpg改rar
10 years, 1 month ago
Answers
之前做过一个七牛的。简单说下思路:
依赖的包有:
- iron:router
- meteorhacks:npm
首先
meteor add meteorhacks:npm
用来引入node 依赖,安装完之后,目录下会多了一个packages.json
在里边写入如下内容:
{
"qiniu": "6.1.8",
"formidable": "1.0.17"
}
这时,会自动安装这两个node包。就可以在服务端使用了。
在server目录下:新建upload.coffee,写入如下内容:
#上传文件路由
Router.route '/upload',
name: 'uploadImg'
where: 'server',
method: 'POST'
action: ->
this.response.write(JSON.stringify(this.request.result), 'utf8')
this.response.end()
uploadFileToQiniu = (req, res, next) ->
formidable = Meteor.npmRequire('formidable')
qiniu = Meteor.npmRequire('qiniu')
form = new formidable.IncomingForm()
uploadToken = getUploadToken()
if req.method == 'POST'
form.parse(req, (err, fields, files)->
now = new Date()
saveTo = "/uploads/#{now.getTime()}-#{files.upload_file.name}"
qiniu.io.putFile(uploadToken, saveTo, files.upload_file.path, null, (err, ret)->
if (!err)
result =
msg: '上传成功, 处理返回值'
success: true
file_path: qiniu_domain + saveTo
else
result =
success: false
file_path: ''
msg: '上传失败'
console.log err
req.result = result
next()
)
)
else
next()
return
Router.onBeforeAction(uploadFileToQiniu, {
only: ['uploadImg']
})
在server目录下,新建一个qiniu.coffee,把配置和
getUploadToken()
方法写在里边
qiniu = Meteor.npmRequire('qiniu')
qiniu.conf.ACCESS_KEY = 'your access_key'
qiniu.conf.SECRET_KEY = 'your secret_key'
@qiniu_domain = 'your domain'
default_bucket = 'your bucketname'
@getUploadToken = (bucketname)->
bucketname = default_bucket unless bucketname
putPolicy = new qiniu.rs.PutPolicy(bucketname)
#putPolicy.callbackUrl = callbackUrl
#putPolicy.callbackBody = callbackBody
#putPolicy.returnUrl = returnUrl
#putPolicy.returnBody = returnBody
#putPolicy.asyncOps = asyncOps
#putPolicy.expires = expires
return putPolicy.token()
盒子里的秘密
answered 10 years, 1 month ago