简体   繁体   English

如何使用MEAN堆栈将上传的图像存储到其他域?

[英]How do I store an uploaded image to my other domain using the MEAN stack?

I've been trying to save simple profile images to my MongoDB database for days now with limited success. 几天来,我一直试图将简单的配置文件图像保存到我的MongoDB数据库中,但收效有限。 It seems to be quite a pain. 似乎很痛苦。 I can get it to store the image but not retrieve it with a path. 我可以获取它来存储图像,但不能使用路径检索它。 I have read that it's a good idea to store your image somewhere else (in my case a registered domain) and only store the url to the image in your database. 我已经读到,将图像存储在其他位置(在我的情况下是注册域),并且仅将图像的URL存储在数据库中是个好主意。 How do I achieve this using the MEAN stack? 如何使用MEAN堆栈实现此目标? Is it even possible? 可能吗?

Otherwise are there any good, potentially free, services that are available? 否则,有没有可用的优质服务(可能免费)?

Example: 例:

router.post('/upload/:profile_id', function (req, res) {

//post to a folder on my external domain

});

You can store images or any binary files easily with firebase. 您可以使用Firebase轻松存储图像或任何二进制文件。

You can set your storage up by: 您可以通过以下方式设置存储空间:

// 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();

This is an example on how to upload images: 这是有关如何上传图像的示例:

// 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;
});

and finally.. download the image: 最后..下载图片:

// 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;
  }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM