Javascript 在express中上载图像而不重定向:(

Javascript 在express中上载图像而不重定向:(,javascript,express,formidable,Javascript,Express,Formidable,有没有一种方法可以在不重定向到/upload路径的情况下使用 如在线和文档中所示 HTML 快车 var form = new formidable.IncomingForm(); form.parse(req, function(err, fields, files) { console.log(87987) console.log(files) console.log(files.file) // `file` is the name of the <inpu

有没有一种方法可以在不重定向到/upload路径的情况下使用

如在线和文档中所示

HTML


快车

 var form = new formidable.IncomingForm();
form.parse(req, function(err, fields, files) {
  console.log(87987)
  console.log(files)
  console.log(files.file)
    // `file` is the name of the <input> field of type `file`
    var old_path = files.file.path,
        file_size = files.file.size,
        file_ext = files.file.name.split('.').pop(),
        index = old_path.lastIndexOf('/') + 1,
        file_name = old_path.substr(index),
        new_path = path.join(process.env.PWD, 'public/uploads/', file_name + '.' + file_ext);

    fs.readFile(old_path, function(err, data) {
        fs.writeFile(new_path, data, function(err) {
            fs.unlink(old_path, function(err) {
                if (err) {
                    res.status(500);
                    res.json({'success': false});
                } else {
                    res.status(200);
                    res.json({'success': true});
                }
            });
        });
    });
});
var form=new.IncomingForm();
解析(请求、函数(错误、字段、文件){
控制台日志(87987)
console.log(文件)
console.log(files.file)
//`file`是`file类型字段的名称`
var old_path=files.file.path,
file_size=files.file.size,
file_ext=files.file.name.split('..').pop(),
index=old_path.lastIndexOf('/')+1,
文件名=旧路径.substr(索引),
new_path=path.join(process.env.PWD,'public/uploads/',file_name+'.+file_ext);
fs.readFile(旧路径、函数(错误、数据){
fs.writeFile(新路径、数据、函数(错误){
fs.取消链接(旧路径、函数(错误){
如果(错误){
物质状态(500);
res.json({'success':false});
}否则{
物质状态(200);
res.json({'success':true});
}
});
});
});
});
图像上载到文件夹中,但由于form元素中的“action='/upload'”属性,我被重定向到/uploads路径


我希望保持在同一页上,但当我尝试更改“action”值时,我无法将图像发送到服务器

使用
res.redirect
或从表单中删除
action
属性以发布到当前页

如果删除要发布到当前页面的
action
属性,则必须为该页面添加要表达的路线,以处理
post
请求


如果在删除
action
属性时遇到问题,请尝试使用
action=“?”
发布到当前页面。

我尝试删除该操作并使用$http.post,但form.parse中的files参数返回一个空对象。因此,我假设在使用action属性时,action属性手动填充files对象。我还想到了使用res.redirect,因此我可以使用req.headers.host+“/path”。我希望有人知道另一种方法,除非这是最佳方法。谢谢你的回答,在选择此
$http.post
?你是否使用angular?如果你需要使用ajax发布表单,请尝试使用本文建议的
new FormData
 var form = new formidable.IncomingForm();
form.parse(req, function(err, fields, files) {
  console.log(87987)
  console.log(files)
  console.log(files.file)
    // `file` is the name of the <input> field of type `file`
    var old_path = files.file.path,
        file_size = files.file.size,
        file_ext = files.file.name.split('.').pop(),
        index = old_path.lastIndexOf('/') + 1,
        file_name = old_path.substr(index),
        new_path = path.join(process.env.PWD, 'public/uploads/', file_name + '.' + file_ext);

    fs.readFile(old_path, function(err, data) {
        fs.writeFile(new_path, data, function(err) {
            fs.unlink(old_path, function(err) {
                if (err) {
                    res.status(500);
                    res.json({'success': false});
                } else {
                    res.status(200);
                    res.json({'success': true});
                }
            });
        });
    });
});