簡體   English   中英

將文件上傳到服務器的發布表單,請求中僅顯示文件名?

[英]Post Form with file upload to server, only file name showing in request?

我將Node.js用於服務器,並嘗試通過表單將文件上傳到服務器。 and cannot find the field in most suggested approaches: 我檢查了其他職位,其中大部分建議采取下列措施,但我打印完整的並不能找到大多數建議方案領域:

形成:

<form method="POST" action="/imageupload">
    <input type="file" name="image" id="image" accept="image/jpeg, image/png"/> <br/>
    <input type="submit" value="Search" name="search" id="search" />
</form>

在服務器中:

app.post('/imageupload', function (req, res){
    //var imgPath = req.files.image;
    console.log(req);
});

這是我在要求中發現的與其他帖子所建議的不同的內容:

 headers: 
  { host: 'localhost:8081',
 connection: 'keep-alive',
 'content-length': '33',
 'cache-control': 'max-age=0',
 accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
 origin: 'http://localhost:8081',
 'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36',
 'content-type': 'application/x-www-form-urlencoded',
 dnt: '1',
 referer: 'http://localhost:8081/',
 'accept-encoding': 'gzip, deflate',
 'accept-language': 'en-US,en;q=0.8,ja;q=0.6,zh-CN;q=0.4,zh-TW;q=0.2',
 cookie: 'JSESSIONID=89875bd29370ecf4ae585822f40c; treeForm_tree-hi=treeForm:tree:updateCenterPlugIN; connect.sid=s%3AJJfmZcGvlE388fwSVSNeoQ6Y0hV55Wur.VQdPuxk0W%2BHHt2zmKLlBxqdUFFXJumtGOOcQVcoeJSI' },

body: { file: 'sampleimg.jpg', search: 'Search' },

我找不到任何指示文件的字段,訪問它會導致服務器錯誤:

TypeError: Cannot read property 'image' of undefined

所以我不確定出了什么問題。 有人可以建議嗎?

將表格更改為

<form method="POST" action="/imageupload" enctype="multipart/form-data"> 

添加enctype 正如文檔所說,當我們上傳文件時,這是必需的。

body-parser僅處理JSON。 要處理multipart ,建議您使用connect-busboy 它處理多部分表單數據。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM