簡體   English   中英

如何使用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;
  }
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM