javascript - 使用 Node.js 和 multer 上传压缩文件并将其保存到 MySql 数据库时出现问题

标签 javascript mysql node.js filesystems multer

我正在使用 Node.js 和 multer 上传压缩文件并将其保存到 MySql 数据库。我已经成功创建了一个登录功能,用户可以通过该功能注册、登录、维护 session key ,并且所有内容都保存到 MySql DB 中。

当我尝试上传 zip 文件并将其发布到我的路线并向数据库查询时,它不起作用,并表示上传对象未定义。

编辑: 压缩文件上传并保存到我在下面添加的 multer 函数的存储路径中定义的/upload 文件夹,因此到目前为止它正在工作。我相信问题可能出在上传表单上方 JS 代码中的 post 请求中,和/或异步函数中。

编辑:

//这是我收到的错误,但是我不认为数据库查询本身有问题,我相信这是异步函数中定义的 req.body 对象的问题。

  code: 'ER_PARSE_ERROR',
  errno: 1064,
  sqlMessage:
   'You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near \'\'undefined\', \'undefined\', \'undefined\'\' at line 1',
  sqlState: '42000',
  index: 0,
  sql:
   'INSERT into user_v_file (user_vile,vile_name,vile_desc) VALUES \'undefined\', \'undefined\', \'undefined\'' }

//这是上传表单和post请求:

<script>
$(document).ready(function() {
  $("#upload").click(function() {
    $("#form-upload").toggle();
    });
    $('#uploadVr').click(function(e){
        $.post("http://localhost:3000/upload",{
                    user_vile : $("#uservile").val(),
                    vile_name : $("#vilename").val(),
                    vile_desc : $("#viledesc").val()
                    },function(data){
                if(!data.error) {
                    window.location.href = "/home";
                } else {
                    alert(data.message);
                }
            });
        })
});
</script>
</head>
<body>
    <button type="button" id="upload" value="Upload">Upload</button>
    <button onclick="window.location.href='/home.html'" type="button" id="explore" value="Explore">Explore</button>

    <form id="form-upload" action="/upload"  method="POST"  enctype="multipart/form-data" style="display:none">
        Upload folder: <input type="file" id="uservile" name="vr_files">
        VR name: <input type="text" id="vilename">
        VR description: <input type="text" id="viledesc">
        <input type="submit" id="uploadVr " value="Uploadfiles"/>
    </form>
</body>
// this is async function
const upload = async (req, res) => {
  try {
    const {vile_name, vile_desc} = req.body;
    const {user_vile} = req.file;
    const resp = await db.upload(user_vile, vile_name, vile_desc); //should it be vr_file..?
    req.session.key = resp;
    res.send({error: false, message: "Vile added"});

  } catch (e) {
    res.send({error: true, message: "Vile upload failed"})
    console.log(e);
  }
  console.log(req.body);
  };
//this is the multer function to define upload storage path 
var storagePath = multer.diskStorage({
    destination: function (req, file, cb) {
      cb(null, './files/uploads')
    },
    filename: function (req, file, cb) {
      cb(null, file.fieldname + '-' + Date.now())
    }
  })
var upload = multer({storage: storagePath})
// this is the route 

router.post('/upload', upload.single('user_vile'), main.upload);

//this is the DB query


const upload = async (user_vile, vile_name, vile_desc) => {
  const results = await testDb.query(`INSERT into user_v_file (user_vile,vile_name,vile_desc) VALUES '${user_vile}', '${vile_name}', '${vile_desc}'`);
  return results
};

最佳答案

所有输入都需要有一个名称,但是多个输入(即以 undefined 到达服务器的输入)没有名称:

 <input type="text" id="vilename" name="vile_name" />

是的,您确实有一个 jQuery 处理程序尝试拦截表单 post,并使用正确的格式发出 AJAX post 请求,但是该 AJAX 请求永远不会像您一样运行不取消表单提交。为此,请在点击处理程序中调用 e.preventDefault()

请务必防止这些 SQL Injections 。您当前的代码确实造成了安全风险。

关于javascript - 使用 Node.js 和 multer 上传压缩文件并将其保存到 MySql 数据库时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57574888/

相关文章:

node.js - 如何在 DynamoDb 描述表中使用 waitUntilTableNotExists

javascript - 尝试在 JavaScript Canvas 上沿鼠标指针方向旋转图像?

javascript - 对象参数的默认值

javascript - 使用 Express 上传图像

javascript - jQuery 每个循环不更新变量中的 HTML

java - 如何使用 Android 应用程序访问 MySQL 数据库?

php - 从时间戳填充多维数组

mysql - 通过计算从 SELECT 更新

javascript - 将模块暴露给外部删除别名

javascript - 数组推送在数组 forEach 中不起作用 - Javascript