如何讓用戶上傳 img 并將其存儲(chǔ)在公共文件夾中?
我正在制作簡(jiǎn)單的應(yīng)用程序,使用戶(餐廳經(jīng)理)能夠插入新的食物及其圖像。
我讓用戶用表單上傳一個(gè)img,我需要將其保存在Nuxt項(xiàng)目的public文件夾中。
食物信息通過 $fecth 和 POST 方法插入,然后從 POST 中提取并插入到數(shù)據(jù)庫中
INSERT INTO menu (food_name, price, course ) VALUES (?, ?, ?)`, [plateName, platePrice, course]
FORM: <input class="form-control" type="file" v-on:change="uploadImg()" id="formFile"> <label for="formFile" class="form-label">Add Image</label>
methods { uploadImg(){ ??? }, addFood() { $fetch("/api/insert/", { method: "POST", body: { plateName: this.plateName, platePrice: this.platePrice, course: this.plateSelect, } }) .then(() => window.location.href = "/inserimento") .catch((e) => alert(e)) },
這不是一個(gè)可行的解決方案,因?yàn)樽罱K用戶可能會(huì)出現(xiàn)惡意行為并發(fā)送垃圾郵件查詢。因此,過度填充您的服務(wù)器并使其崩潰或填滿整個(gè)磁盤。
最重要的是,它不會(huì)被優(yōu)化、正常服務(wù)或任何其他事情(沒有捆綁步驟)。
相反,我建議您將其上傳到某個(gè)后端,對(duì)其進(jìn)行優(yōu)化(可以使用 Cloudinary 等服務(wù)來完成)并將其托管在 AWS S3 或類似的設(shè)備上(最好是在 CDN 上)。
這是一個(gè)更具可擴(kuò)展性、更安全且持久的解決方案。