我創建了一個 dB,用于在 MySQL 和 NodeJS 中存盤影像的 URL 和名稱并做出反應,我有一個帶節點和反應的登錄功能。我想要的是當用戶上傳影像時,我想將其存盤在用戶下,以便只有他才能查看。你能指導我如何實作這一目標嗎
uj5u.com熱心網友回復:
實作此目的的方法之一是管理權限。跟蹤誰正在上傳它。
Uploads
--------
id
path
filename
uid (user_id)
使用它來獲取影像詳細資訊。為了提高安全性,請將檔案名設定為隨機字符或管理服務器檔案權限。
uj5u.com熱心網友回復:
MySql:我假設您在 MySql 中有user表和uploads表。在您的uploads表中,添加created_by應該是表中列的外鍵參考的user_id列user。
React:在呼叫 NodeJs 以獲取影像時,user_id從 React發送引數。
Nodejs:然后NodeJs應該只從當前用戶通過查詢MySQL創建的Uploads表中獲取影像
select <YOUR COLUMNS> from UPLOADS where created_by=user_id
uj5u.com熱心網友回復:
因此,如果您要將上傳檔案的資料發布到服務器,例如作為多部分,那么只需將資料添加到 ajax 請求中。
<input id="upload_file" name="upload_file"/>
// create form data holder
var fd = new FormData();
// add images, text or any data you would like here.
// this will access the upload_files file input element, get the total files selected
let total_files = document.getElementById('upload_file').files.length;
// this will loop through each selected file and add it to the FormData which will be submitted.
for(let i=0;i<total_files;i ){
fd.append("upload_file[]", document.getElementById('upload_file').files);
}
// added the users details.
let userID = 1;
let userName = "John Doe"
fd.append("userID", userId);
fd.append("userName", userName);
// make hhtp post request and return the json response.
let api_url = "/api/url/goes/here";
let response = fetch(api_url, {
headers: {
"Content-Type": "multipart/form-data",
"Accept": "application/json",
"X-Requested-With": "XMLHttpRequest",
},
method: 'POST',
credentials: "same-origin",
body: fd,
})
.then((response)=>response.json())
.then((responseJson)=>{return responseJson});
// now you can access the response data via the response variable.
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/322070.html
