2017-09-14 86 views
0

我是nodejs和firebase的新手,但我需要上傳文件。我看到this教程,但無法獲得太多。我完全困惑。在這個教程中,是傳遞選擇文件的函數? 代碼:如何在nodejs中上傳文件

const keyFilename = "./my-private-api-key-file.json"; //replace this with api key file 
const projectId = "my-project-id-should-go-here" //replace with your project id 
const bucketName = `${projectId}.appspot.com`; 

const mime = require('mime'); 
const gcs = require('@google-cloud/storage')({ 
    projectId, 
    keyFilename 
}); 

const bucket = gcs.bucket(bucketName); 

const filePath = `./package.json`; 
const uploadTo = `subfolder/package.json`; 
const fileMime = mime.lookup(filePath); 

bucket.upload(filePath, { 
    destination: uploadTo, 
    public: true, 
    metadata: { 
     contentType: fileMime, 
     cacheControl: "public, max-age=300" 
    } 
}, function (err, file) { 
    if (err) { 
     console.log(err); 
     return; 
    } 
    console.log(createPublicFileURL(uploadTo)); 
}); 

function createPublicFileURL(storageName) { 
    return `http://storage.googleapis.com/${bucketName}/${encodeURIComponent(storageName)}`; 
} 

我要上傳文件時,用戶選擇了一個文件。任何人都可以提供我一些啓動?謝謝。

回答

0

您所遵循的教程的目的是爲了在文件上傳到服務器之後處理文件。這對於服務(如Heroku或OpenShift,如本教程中提到的)適用於不會永久保存上載內容的臨時文件系統。

您應該嘗試遵循this tutorial而不是,它解釋瞭如何使用AJAX構建前端用戶界面。一旦你這樣做了,那麼你可能需要按照你的問題中鏈接的教程來獲得這些文件永久存儲。

希望這有助於。

0

使用多方庫它幫助了我很多在我當前nodejs項目