Node.js 节点js获取图像url
我想 1-从我的文件系统中选择一个映像并将其上载到服务器/本地 2-使用节点js服务获取其url。我成功地完成了步骤1,现在我想获取图像url,而不是res.end中的成功消息 这是我的密码Node.js 节点js获取图像url,node.js,multer,Node.js,Multer,我想 1-从我的文件系统中选择一个映像并将其上载到服务器/本地 2-使用节点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!"); } re
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上传图像。我假设您将图像保存在服务器文件系统上,而不是像AWS S3或Google Cloud Storage这样的存储解决方案,您可以在上传后获得url 由于您将其存储在文件系统中,因此可以使用唯一标识符(如uuid或其他)重命名该文件
然后,您可以在查询或路径参数中创建一个GET路由并请求该ID,然后读取该ID作为名称的文件并将其发送回。我假设您正在服务器文件系统上保存图像,而不是像AWS S3或Google Cloud Storage这样的存储解决方案,在上传后您可以在其中获取url 由于您将其存储在文件系统中,因此可以使用唯一标识符(如uuid或其他)重命名该文件
然后,您可以创建一个GET路由并在查询或路径参数中请求该ID,然后读取以该ID为名称的文件并将其发送回。您可以使用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
})
}
const AWS=require('AWS-sdk')
AWS.config.update({
accessKeyId:您可以使用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
})
}
const AWS=require('AWS-sdk')
AWS.config.update({
accessKeyId:我使用返回res.send(req.file.path);
在我的post方法中我使用返回res.send(req.file.path);
在我的post方法中