上传一个文件并传递一个附加的参数与multer [英] Uploading a file and passing a additional parameter with multer

查看:1119
本文介绍了上传一个文件并传递一个附加的参数与multer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 jQuery表单插件 multer 将文件上传到我的服务器。这工作得很好,但我想传递一个额外的参数,这将决定在哪里确切的文件将被保存。

我有以下的代码,我想扩展为表现如下:
$ b

HTML



 < form id =uploadForm
enctype =multipart / form-data
action =/ files
method =post>
< input type =fileid =userFilename =userFile/>
< input type =texthidden id =savePathname =savePathvalue =path/>
< / form>



客户端JS



 < $ c $ upload.cfm.submit(function(){
$(this).ajaxSubmit({

error:function(xhr){
console.log('Error :'+ xhr.status);
},

成功:函数(响应){
console.log('Success:'+ response);
}
});

return false;
});



Node.js路由



  app.post(APIpath +file,function(req,res){
var storage = multer.diskStorage({
destination:absoluteServePath +/+ config.filePath ,
filename:function(req,file,cb){
cb(null,file.originalname);
}
});
var upload = multer( (存储:存储})。任何();
$ b $上传(请求,水库,功能(错误){$ b $如果(错误){
console.log(错误);
return res.end(Error uploading file。);
}
res.end(File has been uploaded);
});
} );

注意:我知道我没有检查mimetypes或清理用户文件,但它现在主要是次要的。

解决方案

问题在于 multer 首先保存文件,然后只写入 req 表单的其余部分 - 比如隐藏字段。

试试这个:

  app.post(APIpath +file,function(req,res){ 
var storage = multer.diskStorage({
destination:tmpUploadsPath
});
var upload = multer({storage:storage})。any();

upload(req,res,function(err){
if(err){
console.log(err);
return res.end(Error uploading file。 );
} else {
console.log(req.body);
req.files.forEach(function(f){
console.log(f);
//并将文件移动到最终目的地n ...
});
res.end(File has been uploaded);
}
});
});


I am using the jQuery Form Plugin and multer to upload files to my server. This works just fine, but I am trying to pass an additional parameter, which will determine where exactly the file will be saved.

I have following code, which I would like to extend to behave as stated:

HTML

<form id="uploadForm"
      enctype="multipart/form-data"
      action="/files"
      method="post">
    <input type="file" id="userFile" name="userFile"/>
    <input type="text" hidden id="savePath" name="savePath" value="path"/>
</form>

Client JS

uploadForm.submit(function() {
    $(this).ajaxSubmit({

        error: function(xhr) {
            console.log('Error: ' + xhr.status);
        },

        success: function(response) {
            console.log('Success: ' + response);
        }
    });

    return false;
});

Node.js Route

app.post(APIpath + "file",function(req,res){
    var storage = multer.diskStorage({
        destination: absoluteServePath+ "/" + config.filePath,
        filename: function (req, file, cb) {
            cb(null, file.originalname);
        }
    });
    var upload = multer({ storage : storage}).any();

    upload(req,res,function(err) {
        if(err) {
            console.log(err);
            return res.end("Error uploading file.");
        }
        res.end("File has been uploaded");
    });
});

Note: I know I am not checking for mimetypes or sanitizing the user files, but it is mainly secondary for me right now.

解决方案

The problem is that multer that saves files first, and only then writes to req the remaining part of the form - such as hidden fields.

Try this:

app.post(APIpath + "file",function(req,res){
    var storage = multer.diskStorage({
        destination: tmpUploadsPath
    });
    var upload = multer({ storage : storage}).any();

    upload(req,res,function(err) {
        if(err) {
            console.log(err);
            return res.end("Error uploading file.");
        } else {
           console.log(req.body);
           req.files.forEach( function(f) {
             console.log(f);
             // and move file to final destination...
           });
           res.end("File has been uploaded");
        }
    });
});

这篇关于上传一个文件并传递一个附加的参数与multer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆