【发布时间】:2015-11-01 00:39:54
【问题描述】:
使用cloudinary_npm 提供的节点集成,当我尝试上传时收到以下消息:
{ error: { message: 'Invalid Signature t7233823748278473838erfndsjy8234. String to sign - \'timestamp=1439054775\'.', http_code: 401 } }
我检索然后将我的图像传递给后端,如下所示:
$scope.previewFile = function() {
var file = document.querySelector('input[type=file]').files[0];
var reader = new FileReader();
if (file) {
reader.readAsDataURL(file);
} else {
preview.src = "";
}
reader.onloadend = function () {
base64img = reader.result;
preview.src = base64img;
console.log(base64img);
};
};
$scope.submitPic = function() {
$http.post('http://localhost:3000/story/pic', {img: base64img})
.success(function(data){
preview.src = "";
})
.error(function(err){
console.log(err);
});
};
然后在后面,我有以下配置和路线,都直接来自文档:
var cloudinary = require("cloudinary");
var CLOUD_API_SECRET = require("../constants.js");
cloudinary.config({
cloud_name: 'some_cloud',
api_key: '63789865675995',
api_secret: CLOUD_API_SECRET
});
router.post('/pic', function(req, res, next) {
var img = req.body.img;
cloudinary.uploader.upload(img, function(result) {
});
res.status(200).send('ok');
});
有人知道我做错了什么吗?我已经解决了几个小时。我走到了尽头。
【问题讨论】:
-
在
submitPic,你确定base64img 有值吗? (为什么要发送 base64 而不是文件?) -
是的,当它到达后端时,我可以看到它在终端中以 base64 的形式出现,我忘了编辑它,但我确实继续,后来又将它转换为二进制所以我可以将其保存为 jpg 格式的文件。除此之外,我仍然不知道如何连接到 cloudinary。至于我为什么要发送base64,好吧,因为我不知道任何其他方式,老实说。使用 html5 文件阅读器,这就是我在文档中可以找到的所有内容,这将允许我将生成的文件输入用作数据 uri,这是我预览所需的。
-
很可能
require("../constants.js")不返回字符串,因此CLOUD_API_SECRET设置不正确。为了使该行工作,constants.js应该有一行,形式为:module.exports = "my_secret"; -
请在另一个 SO 问题中查看我的回答 - stackoverflow.com/questions/31897661/…
标签: upload signature cloudinary