图像未上传到node.js中定义的文件夹

时间:2019-01-23 14:02:32

标签: html node.js rest

我有一个小的HTML模板,我想要的是将图像从一个位置上传到另一个位置。选择并单击上传按钮后,响应显示“成功上传”,但是图像不在目标文件夹中。请查看下面的代码。

HTML

<form id="frmUploader" enctype="multipart/form-data" action="book/api/Upload/" method="post">
    <input type="file" name="imgUploader" multiple />
    <input type="button" name="submit" id="btnSubmit" value="Upload" onclick="upload()"/>
</form>

index.js

function upload() {
    axios.post(baseUrlLocal+'/book/api/Upload', {headers: headers})
    .then(response => {
        console.log(response.data.success)
        if (response.data.success) {
            console.log("hi: "+response)
        // $.notify("New Book is Added to your Library", "success");

    }
        })
        .catch(function (error) {
            console.log(error)
            // $.notify("Please check all the details","warn");
        });
}`

book.js

var Storage = multer.diskStorage({
destination: function(req, file, callback) {
    callback(null, "../public/images/");
},
filename: function(req, file, callback) {
    callback(null, file.fieldname + "_" + Date.now() + "_" + 
file.originalname);
}
 });

var upload = multer({
storage: Storage
 }).array("imgUploader", 3);



router.get("/image", function(req, res) {
res.sendFile(__dirname + "/index.html");
});
`router.post("/api/Upload", function(req, res) {
upload(req, res, function(err) {
    if (err) {
        return res.end("Something went wrong!");
    }
    return res.end("File uploaded sucessfully!.");
});
 });`

0 个答案:

没有答案