问题描述
我正在尝试将文件上传到我的服务器,并使用connect-multiparty
中间件从发布请求中提取文件.但是,当我在服务器上收到请求时,req.files
和req.body
对象为空(不是null,但是node-inspector
显示它们是Object
,其中没有任何内容.)
I am trying to upload files to my server and extract them from the post request using the connect-multiparty
middleware. However, when I receive the request on the server, the req.files
and req.body
objects are empty (not null, but node-inspector
shows that they are Object
s with nothing in them.
这是我正在使用的代码:
Here is the code that I'm working with:
server.js:
server.js:
var express = require( "express" );
var app = express();
var server = require( "http" ).Server( app );
var fs = require( "fs" );
var multipart = require('connect-multiparty');
app.use( express.static( "public" ) );
app.use( multipart() );
app.post( "/httpUpload", function( req, res ) {
console.log( "Received post request" );
}
index.html:
index.html:
<form action="/httpUpload" method="post" enctype="multipart/form-data">
<input type="file" id="uploadFileInput">
<div class="row">
<div class="col-md-6">
<input type="submit">
</div>
</div>
</form>
尝试使用multer
,connect-busboy
和body-parser
时,我得到了类似的结果.如果该解决方案对我有用,我会喜欢的,但它没有: http://howtonode. org/really-simple-file-uploads
I've gotten similar results trying to use multer
, connect-busboy
, and body-parser
. I would have loved if this solution worked for me, but it didn't: http://howtonode.org/really-simple-file-uploads
所以……在我所有失败的尝试中,唯一的共同主题是我. ; o)有什么想法我做错了吗?
So ... the only common theme in all of my failed attempts is me. ;o) Any ideas what I'm doing wrong?
推荐答案
嗯……这并不是我的问题的答案,但是当我更改代码以删除表单并通过jQuery的ajax
方法中,req.files
对象中包含我的数据. (耸耸肩)这是我的js中使其生效的代码:
Well ... this isn't quite an answer to my question, but when I changed my code to remove the form and send the post request via jQuery's ajax
method, the req.files
object had my data in it. (shrug) Here was the code in my js that made it work:
$.ajax( {
url: "/httpUpload",
type: "POST",
processData: false, // important
contentType: false, // important
dataType : "json",
data: formData
} );
这是使我走上正确道路的帖子: 不带表单的文件上传
This is the post that put me on the right path: File Upload without Form
这篇关于在express.js中使用connect-multiparty的正确方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架