如何使用 MEAN 堆栈将上传的图像存储到我的其他域
How do I store an uploaded image to my other domain using the MEAN stack?
几天来我一直在尝试将简单的个人资料图像保存到我的MongoDB数据库中,但收效甚微。这似乎很痛苦。我可以让它存储图像,但不能用路径检索它。我已经读到将您的图像存储在其他地方(在我的情况下是注册域)并且仅将图像的 url 存储在您的数据库中是个好主意。如何使用 MEAN 堆栈实现此目的?甚至可能吗?
否则,是否有任何好的,可能免费的服务可用?
例:
router.post('/upload/:profile_id', function (req, res) {
//post to a folder on my external domain
});
您可以使用Firebase轻松存储图像或任何二进制文件。
您可以通过以下方式设置存储空间:
// Set the configuration for your app
// TODO: Replace with your project's config object
var config = {
apiKey: '<your-api-key>',
authDomain: '<your-auth-domain>',
databaseURL: '<your-database-url>',
storageBucket: '<your-storage-bucket>'
};
firebase.initializeApp(config);
// Get a reference to the storage service, which is used to create references in your storage bucket
var storageRef = firebase.storage().ref();
这是有关如何上传图像的示例:
// File or Blob, assume the file is called rivers.jpg
var file = ...
// Upload the file to the path 'images/rivers.jpg'
// We can use the 'name' property on the File API to get our file name
var uploadTask = storageRef.child('images/' + file.name).put(file);
// Register three observers:
// 1. 'state_changed' observer, called any time the state changes
// 2. Error observer, called on failure
// 3. Completion observer, called on successful completion
uploadTask.on('state_changed', function(snapshot){
// Observe state change events such as progress, pause, and resume
// See below for more detail
}, function(error) {
// Handle unsuccessful uploads
}, function() {
// Handle successful uploads on complete
// For instance, get the download URL: https://firebasestorage.googleapis.com/...
var downloadURL = uploadTask.snapshot.downloadURL;
});
最后.. 下载图片:
// Create a reference to the file we want to download
var imageRef = storageRef.child('images/example.jpg');
// Get the download URL
imageRef.getDownloadURL().then(function(url) {
// Insert url into an <img> tag to "download"
}).catch(function(error) {
switch (error.code) {
case 'storage/object_not_found':
// File doesn't exist
break;
case 'storage/unauthorized':
// User doesn't have permission to access the object
break;
case 'storage/canceled':
// User canceled the upload
break;
...
case 'storage/unknown':
// Unknown error occurred, inspect the server response
break;
}
});
相关文章:
- 当图像转换为本地存储的DataURL时,EXIF被删除
- 在字符串中查找所有图像 src URL 并存储在数组中
- 在ListView中添加JSON数组中存储的图像-ReactNative
- 以HTML文件中的编码形式脱机存储图像
- Codeigniter:通过Ajax上传图像并存储在数据库中
- 存储和检索图像数据URL
- 将图像文件存储在猫鼬模式中的二进制数据中,并以html形式显示图像
- 将图像标题存储在selenium IDE中
- Pinterest 扩展如何存储(临时)网页中的图像,然后在 iframe 中访问它们
- 在 Windows 现代(地铁)UI 应用中存储图像文件和音频文件的方法是什么?
- 有没有一种存储图像变量的短方法
- 如何正确存储图像的描述信息
- 如何使用Backand和Ionic存储图像
- 有可能阻止Cordova's相机插件,用于在设备上存储图像
- 在网络应用程序上存储图像的最佳方式是什么
- 存储图像点击并回显链接
- 在没有JQuery的数据库中存储图像
- Pdf.js查看器不正确地存储图像数据
- 使用node.js存储图像会产生错误
- 如何在HBASE中插入图片?我需要在hbase中存储图像(hbase是我项目中的DB)