e2

滴滴侠,fai抖

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
  0 随笔 :: 734 文章 :: 0 评论 :: 50万 阅读
本文介绍了在express.js中使用connect-multiparty的正确方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将文件上传到我的服务器,并使用connect-multiparty中间件从发布请求中提取文件.但是,当我在服务器上收到请求时,req.filesreq.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 Objects 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>

尝试使用multerconnect-busboybody-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屋!

posted on   纯黑Se丶  阅读(232)  评论(0编辑  收藏  举报
(评论功能已被禁用)
编辑推荐:
· 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 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架
点击右上角即可分享
微信分享提示