我想要1-从我的文件系统中选择一个图像并将其上传到服务器/本地2-使用node js服务取回其url。我成功地完成了步骤1,现在我希望获得图像url,而不是res.end格式的成功消息这里是我的代码
app.post("/api/Upload", function(req, res) {
upload(req, res, function(err) {
if (err) {
return res.end("Something went wrong!");
}
return res.end("File uploaded sucessfully!.");
});
});我在用multer上传图片。
发布于 2018-02-10 06:17:45
您可以这样做,使用AWS S3,它将返回上传的图像的url
const AWS = require('aws-sdk')
AWS.config.update({
accessKeyId: <AWS_ACCESS_KEY>,
secretAccessKey: <AWS_SECRET>
})
const uploadImage = file => {
const replaceFile = file.data_uri.replace(/^data:image\/\w+;base64,/, '')
const buf = new Buffer(replaceFile, 'base64')
const s3 = new AWS.S3()
s3.upload({
Bucket: <YOUR_BUCKET>,
Key: <NAME_TO_SAVE>,
Body: buf,
ACL: 'public-read'
}, (err, data) => {
if (err) throw err;
return data.Location; // this is the URL
})
}此外,您还可以查看此快速生成器,其中包含将图像上传到AWS S3 https://www.npmjs.com/package/speedbe的路径
发布于 2018-02-08 21:12:58
我假设您将图像保存在服务器文件系统上,而不是像亚马逊S3或谷歌云存储这样的存储解决方案,您可以在上传后获取url。
由于您将其存储在文件系统中,因此可以使用唯一标识符重命名该文件,如uuid或其他名称。
然后,您可以创建一个GET路由,并在查询或路径参数中请求该ID,然后读取具有该ID作为名称的文件并将其发送回去。
https://stackoverflow.com/questions/48686145
复制相似问题