Node.js Multer Ajax - 页面刷新

时间:2016-11-16 22:17:40

标签: jquery ajax node.js multer

我正在尝试使用Multer和Ajax技术在Node中上传文件。

节点服务器端代码

var express = require('express');
var app = express();
var bodyParser =    require("body-parser");

var multer  =   require('multer');

var storage =   multer.diskStorage({
  destination: function (req, file, callback) {
    callback(null, '../shop/photos');
  },
  filename: function (req, file, callback) {
    callback(null, file.originalname + '.jpg');
  }
});
var upload = multer({ storage : storage}).single('userPhoto');                

app.post('/postphoto', upload, function (req, res, next) {

 res.end("asd");
});

表达ejs:

      <form id="uploadForm"
          enctype="multipart/form-data"
          action="/postphoto"
          method="post"
          >
      <input type="file" name="userPhoto" />
      <input type="submit" value="Upload Image" name="submit">
      <input type='text' id='random' name='random'><br>
      <span id = "status"></span>
      </form>
</div>

  <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
  <script src="http://cdnjs.cloudflare.com/ajax/libs/jquery.form/3.51/jquery.form.min.js"></script>
  <script>
  $(document).ready(function() {
     $('#uploadForm').submit(function() {
        $("#status").empty().text("File is uploading...");
        $(this).ajaxSubmit({
            error: function(xhr) {
          status('Error: ' + xhr.status);
            },
            success: function(response) {
            $("#status").empty().text(response);
            }
    });
    return false;
    }); 

保存文件,此部分正常工作。问题是邮政路线正在打开一个带有评论“asd”的新页面。貌似/ postphoto没有回到Ajax。你知道怎么解决这个问题吗?

0 个答案:

没有答案