javascript - 将 Mongoose/Multer 文件上传的工作 Modal/FORM 更改为 MULTIPART/FORM - Modal 保持打开状态

标签 javascript html node.js mongoose multer

我有一个有效的模态/表单(缩短):

html:

<div id="addNew" class="modal">
<form class="modal-content animate">
    <div class="container">
    <table id="t01">
      <tbody>
        <tr>
          <td>Date</td>
          <td>Time</td>
        </tr>
        <tr>
          <td>
          <input id="id_date" type="date" name="date" required>
          </td>
          <td>
          <input id="id_time" type="time" name="time" required>
          </td>
        </tr>
      </tbody>
    </table>
      <br>
      <button type="submit" class="buttons" onclick="submit_new()">Safe</button>
      <button type="button" onclick="document.getElementById('addNew').style.display='none'" class="cancelbtn">Cancel</button>
    </div>
  </form>
</div>

它正在调用一个将内容放入 mongodb 的函数:

功能:

function submit_new() {
    $.ajax({
        type: "POST",
        url: '/submit_new',
        data: {
            date:$('#id_date').val(),
            time:$('#id_time').val(),
            },
        });
    };

应用程序:

app.post('/submit_new', function (req, res) {
          const postBody = req.body;
          Faenge.create({date: postBody.date, time: postBody.time }), function (err, res) {
            if (err)  {
                throw err;
            }
                console.log("1 document inserted");
                Faenge.close()
            }
    });

这没有任何问题。点击提交后,模式会关闭,并且值会被放入集合中。

我需要使用 multer 添加图像上传。当我点击提交按钮时,上传、MongoDB 条目等正在工作,没有问题,但模态框永远保持打开状态。即使我使用取消关闭模式,它也会关闭,但如果我重新打开它,这些值仍然存在。

html:

<div id="addNew" class="modal">
<form class="modal-content animate" id="test-data" method="post" enctype="multipart/form-data">
<div class="container">
<table id="t01">
<tbody>
<tr>
<td>Date</td>
<td>Time</td>
</tr>
<tr>
<td>
<input id="id_date" type="date" name="date" required>
</td>
<td>
<input id="id_time" type="time" name="time" required>
</td>
</tr>
</tbody>
</table>
</tr>
<br>
<tr>
<input id="id_picture" type="file" name="Bild" accept="image/*" multiple="multiple" />  
</form>
<br>
<br>
<button type="submit" class="buttons">Speichern</button>
<button type="button" onclick="document.getElementById('addNew').style.display='none'" class="cancelbtn">Abbrechen</button>
</div>
</form>

功能:

$(document).ready(function() {
$("form#test-data").submit(function(e) {
        e.preventDefault();    
        var formData = new FormData(this);
        $.ajax({
            url: '/submit_new',
            type: 'POST',
            data: formData,
            success: function (data) {
                alert(data);
            },
            cache: false,
            contentType: false,
            processData: false
        });
    });
return false;
}); 

应用程序:

app.post('/submit_new', upload.single('Bild') , function (req, res, next) {
            if(req.file){
                filepath = req.file.path;
            } else {
                filepath = "";
            }
          const postBody = req.body;
          Faenge.create({
              date: postBody.date, 
              time: postBody.time, 
              filepath: filepath,
              }), function (err, res) {
              if (err)  {
                throw err;
              }else{
                console.log("1 document inserted");
                Faenge.close()
            }}          
    });

请问有人可以帮忙吗?提前致谢,

问候

最佳答案

我自己找到的,并补充道:

onclick="document.getElementById('addNew').style.display='none'"

到提交按钮....

关于javascript - 将 Mongoose/Multer 文件上传的工作 Modal/FORM 更改为 MULTIPART/FORM - Modal 保持打开状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52964336/

相关文章:

javascript - 通过 REST 使用 Web 服务,并使用完整的 JavaScript 进行身份验证

c# - 模型从 ASP.NET Web API 中的 URI 绑定(bind)到字典

node.js - npm 错误!代码 ELIFECYCLE(起始问题)

javascript - 为什么在 Promise.all 中使用 fetch 时会收到 Promise { <pending> }?

javascript - 仅从表 'like github does it' 中选择/突出显示列?

javascript - AngularJs : Callback or promise in a service using conditions

javascript - 添加和删​​除类以单击动态按钮

html - CSS画廊帮助UI开发

JavaScript .click()在for循环中只执行一次

node.js - 将我的模块包含在 node_modules 中是一种很好的做法,可以使要求搜索变得容易,如果不是为什么不呢?