2016-07-27 64 views
0

我写了一个代码,允许用户上传图像到服务器。我的代码如下:检索上传文件的图像路径

我的后端在Node JS。我想要的是检索图像文件的文件路径,以便我可以上传它。这是从Node JS方面完成的。

我写的代码不起作用。它在检索图像文件的路径时崩溃。

<div class="form-group"> 
    <input id="input-700" name="kartik-input-700[]" type="file" multiple class="file-loading" name="fileupload"> 

</div> 

JAVASCRIPT

<script type="text/javascript"> 

    $(document).ready(function(){ 

$("#input-700").fileinput({ 
    uploadUrl: "/filllll/uploadIm", // server upload action 
    allowedFileExtensions: ['jpg', 'png'], 
    uploadAsync: true, 
    maxFileCount: 1 
}); 
var fullPath = $("#input-700").val(); 
alert(fullPath); 

    }); 
</script> 

节点JS

router.post('/uploadIm', function (req, res, next) { 
    console.log('IThe path of the file' + res.fieldname); 

}); 
+0

您可以使用像“multer”这样的npm模块来处理文件上载。它通过在req中添加一个文件字段使得文件上传更加容易。 – nrgwsth

回答

0

快递没有这个开箱。您需要处理multipart/form-data。最简单的方法 - 使用中间件。例如,multer

var express = require('express') 
var multer = require('multer') 
var upload = multer({ dest: 'uploads/' }) 

var app = express() 

app.post('/profile', upload.single('avatar'), function (req, res, next) { 
    // req.file is the `avatar` file 
    // req.body will hold the text fields, if there were any 
}) 
+0

你也可以向我展示相应的HTML和JQuery函数。我是一个新手。 :S – Illep

+0

很多关于这个主题的互联网上的教程:)例如:https://codeforgeek.com/2014/11/ajax-file-upload-node-js/ –