【问题标题】:upload image path to mysql along with the user name who uploads the image [closed]上传图片到mysql的路径以及上传图片的用户名[关闭]
【发布时间】:2021-12-05 05:30:21
【问题描述】:

我创建了一个 dB,用于在 MySQL 和 NodeJS 中存储图像的 URL 和名称并做出反应,我有一个节点登录功能并做出反应。我想要的是当用户上传图片时,我想将其存储在用户名下,这样只有他才能查看。你能指导我如何实现这一目标

【问题讨论】:

  • 请与问题一起分享代码sn-p。
  • 只需发送带有标头的用户数据,这样标头就会包含用户名,简单的破解
  • @DeanVanGreunen 你能详细解释一下你的答案吗?我是新手

标签: mysql node.js reactjs


【解决方案1】:

因此,如果您将上传文件的数据发布到服务器,例如作为多部分,那么只需将数据添加到 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.

【讨论】:

【解决方案2】:

MySql: 我假设您在 MySql 中有 user 表和 uploads 表。在您的uploads 表中,添加created_by 列,该列应该是user 表中user_id 列的外键引用。

反应: 在调用 NodeJs 获取图像时,从 React 发送 user_id 参数。

Nodejs: 然后 NodeJs 应该只从 Uploads 表中获取由当前用户通过使用以下查询查询 MySQL 创建的图像

select <YOUR COLUMNS> from UPLOADS where created_by=user_id

【讨论】:

【解决方案3】:

实现此目的的方法之一是管理权限。跟踪上传者。

Uploads
--------
id
path
filename
uid (user_id)

使用它来获取图像详细信息。为了提高安全性,请将文件名设置为随机字符或管理服务器文件权限。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-07-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-30
  • 2020-03-21
  • 1970-01-01
相关资源
最近更新 更多