簡體   English   中英

NodeJS expressJS上載穆特爾和護照,並將文件保存到MongoDB

[英]NodeJS expressJS upload and save file to MongoDB with multer and passport

我已經看過許多類似的問題,但仍無法解決我的問題。

我有一個注冊發帖請求,該請求是通過護照截取的,但是我還需要上傳個人資料照片才能將其保存到數據庫中。

routes.js

app.post('/signup', passport.authenticate('local-signup', {
  successRedirect: '/home',
  failureRedirect: '/signup',
  failureFlash: true
}));

護照

passport.use('local-signup', new LocalStrategy({
  passReqToCallback: true
}, function(req, username, password, done) {

  // logic and add new user
}));

注冊表格(html)

<form class="signup-container" action="/signup" method="post">
  <input class="signup-container-input" placeholder="Username"  name="username" />
  <input class="signup-container-input" placeholder="Email ID" name="email" />
  <input class="signup-container-input" type="password" placeholder="Password" name="password" />
  <input class="signup-container-input" type="password" placeholder="Re-enter password" name="reEnterPassword" />
  <input class="signup-container-input" placeholder="Full name" name="name" />
  <div class="signup-container-image">
    <img class="signup-container-image-image" id="profile-photo-img" src="http://www.ivonly.com/assets/images/user-image.jpg" />
    <div class="signup-container-image-uploader">
      <span id="profile-photo-label">Choose photo</span>
      <input class="upload" type="file" name="profilePhoto" id="profile-photo" onchange="getPhoto(this);" />
    </div>
  </div>
  <div class="signup-footer">
    <button type="submit" class="signup-footer-signup">Sign up</button>
    <div class="signup-footer-login">
            Already a user?
      <a href="/login">Login</a>
    </div>
  </div>
</form>

我希望將圖像保存到我的MongoDB集合中。 我已經閱讀了有關Multer和GridFS的信息,但是有人可以解釋我應該如何使用它嗎?

看看

我會建議你什么? 當用戶瀏覽表單上的圖像時,請立即上載此文件並從服務器獲取圖像名稱,然后在表單中填寫圖像字段,這是最好的解決方案,因此您只能將圖像名稱保存到mongodb中然后再訪問它。

暫無
暫無

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

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