-
Notifications
You must be signed in to change notification settings - Fork 0
/
273b44bb.html
1 lines (1 loc) · 63.4 KB
/
273b44bb.html
1
<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width,initial-scale=1,viewport-fit=cover"><title>express,multer,jQuery前端后端上传单个文件 | 梦洁小站-属于你我的小天地</title><meta name="author" content="梦洁"><meta name="copyright" content="梦洁"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="先安装基本的模块$ npm init -y $ npm install express –save $ npm install multer –save 附上multer的github当中别人汉化的API文档github地址 我这使用的nodemon,如果那么使用node运行,修改记得重启 开始创建基本express(上传图片)当前目录下创建fileup.js文件 12345678910111"><meta property="og:type" content="article"><meta property="og:title" content="express,multer,jQuery前端后端上传单个文件"><meta property="og:url" content="https://www.dreamlove.top/273b44bb.html"><meta property="og:site_name" content="梦洁小站-属于你我的小天地"><meta property="og:description" content="先安装基本的模块$ npm init -y $ npm install express –save $ npm install multer –save 附上multer的github当中别人汉化的API文档github地址 我这使用的nodemon,如果那么使用node运行,修改记得重启 开始创建基本express(上传图片)当前目录下创建fileup.js文件 12345678910111"><meta property="og:locale" content="zh_CN"><meta property="og:image" content="https://dreamos.oss-cn-beijing.aliyuncs.com/gitblog/124124124.png"><meta property="article:published_time" content="2022-03-29T22:18:47.000Z"><meta property="article:modified_time" content="2022-03-29T22:18:47.000Z"><meta property="article:author" content="梦洁"><meta property="article:tag" content="NodeJs"><meta name="twitter:card" content="summary"><meta name="twitter:image" content="https://dreamos.oss-cn-beijing.aliyuncs.com/gitblog/124124124.png"><link rel="shortcut icon" href="https://oss.dreamlove.top/i/2024/02/02/nw24wu.png"><link rel="canonical" href="https://www.dreamlove.top/273b44bb.html"><link rel="preconnect" href="https://lib.baomitu.com"><link rel="preconnect" href="//busuanzi.ibruce.info"><link rel="stylesheet" href="/css/index.css?v=4.13.0"><link rel="stylesheet" href="https://lib.baomitu.com/font-awesome/6.5.1/css/all.min.css"><link rel="stylesheet" href="https://lib.baomitu.com/fancyapps-ui/5.0.33/fancybox/fancybox.min.css" media="print" onload='this.media="all"'><script>const GLOBAL_CONFIG={root:"/",algolia:{appId:"G7A2D1SYPY",apiKey:"49056313a9be02852c7dbedfcb772831",indexName:"blog",hits:{per_page:20},languages:{input_placeholder:"搜索文章",hits_empty:"找不到您查询的内容:${query}",hits_stats:"找到 ${hits} 条结果,用时 ${time} 毫秒"}},localSearch:void 0,translate:void 0,noticeOutdate:void 0,highlight:{plugin:"highlight.js",highlightCopy:!0,highlightLang:!0,highlightHeightLimit:!1},copy:{success:"复制成功",error:"复制错误",noSupport:"浏览器不支持"},relativeDate:{homepage:!1,post:!1},runtime:"天",dateSuffix:{just:"刚刚",min:"分钟前",hour:"小时前",day:"天前",month:"个月前"},copyright:void 0,lightbox:"fancybox",Snackbar:void 0,infinitegrid:{js:"https://lib.baomitu.com/egjs-infinitegrid/4.11.1/infinitegrid.min.js",buttonText:"加载更多"},isPhotoFigcaption:!1,islazyload:!1,isAnchor:!1,percent:{toc:!0,rightside:!1},autoDarkmode:!1}</script><script id="config-diff">var GLOBAL_CONFIG_SITE={title:"express,multer,jQuery前端后端上传单个文件",isPost:!0,isHome:!1,isHighlightShrink:!1,isToc:!0,postUpdate:"2022-03-29 22:18:47"}</script><script>(e=>{e.saveToLocal={set:(e,t,o)=>{if(0===o)return;const a={value:t,expiry:Date.now()+864e5*o};localStorage.setItem(e,JSON.stringify(a))},get:e=>{const t=localStorage.getItem(e);if(!t)return;const o=JSON.parse(t);if(!(Date.now()>o.expiry))return o.value;localStorage.removeItem(e)}},e.getScript=(e,t={})=>new Promise((o,a)=>{const n=document.createElement("script");n.src=e,n.async=!0,n.onerror=a,n.onload=n.onreadystatechange=function(){const e=this.readyState;e&&"loaded"!==e&&"complete"!==e||(n.onload=n.onreadystatechange=null,o())},Object.keys(t).forEach(e=>{n.setAttribute(e,t[e])}),document.head.appendChild(n)}),e.getCSS=(e,t=!1)=>new Promise((o,a)=>{const n=document.createElement("link");n.rel="stylesheet",n.href=e,t&&(n.id=t),n.onerror=a,n.onload=n.onreadystatechange=function(){const e=this.readyState;e&&"loaded"!==e&&"complete"!==e||(n.onload=n.onreadystatechange=null,o())},document.head.appendChild(n)}),e.activateDarkMode=()=>{document.documentElement.setAttribute("data-theme","dark"),null!==document.querySelector('meta[name="theme-color"]')&&document.querySelector('meta[name="theme-color"]').setAttribute("content","#0d0d0d")},e.activateLightMode=()=>{document.documentElement.setAttribute("data-theme","light"),null!==document.querySelector('meta[name="theme-color"]')&&document.querySelector('meta[name="theme-color"]').setAttribute("content","#ffffff")};const t=saveToLocal.get("theme");"dark"===t?activateDarkMode():"light"===t&&activateLightMode();const o=saveToLocal.get("aside-status");void 0!==o&&("hide"===o?document.documentElement.classList.add("hide-aside"):document.documentElement.classList.remove("hide-aside"));/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)&&document.documentElement.classList.add("apple")})(window)</script><meta name="generator" content="Hexo 7.1.1"></head><body><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="https://oss.dreamlove.top/i/2024/02/02/nw24wu.png" onerror='onerror=null,src="/img/404.png"' alt="avatar"></div><div class="sidebar-site-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">170</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">94</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">70</div></a></div><hr class="custom-hr"><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 友情和链接</span></a></div><div class="menus_item"><a class="site-page" href="/navigation/"><i class="fa-fw fas fa-link"></i><span> 链接导航</span></a></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fas fa-list"></i><span> 私有化导航</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" target="_blank" rel="noopener" href="https://mark.123916.xyz/"><i class="fa-fw fas fa-database"></i><span> 导航</span></a></li><li><a class="site-page child" target="_blank" rel="noopener" href="https://cloud.reassurehome.com/"><i class="fa-fw fas fa-hippo"></i><span> Alist(私有)</span></a></li></ul></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fas fa-list"></i><span> 工具</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" target="_blank" rel="noopener" href="https://short.123916.xyz/027a2"><i class="fa-fw fas fa-paperclip"></i><span> clash免费订阅地址</span></a></li><li><a class="site-page child" target="_blank" rel="noopener" href="https://short.123916.xyz/admin/"><i class="fa-fw fas fa-paperclip"></i><span> 短链接生成</span></a></li></ul></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fas fa-list"></i><span> 好玩的</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" target="_blank" rel="noopener" href="https://make.girls.moe/#/"><i class="fa-fw fas fa-database"></i><span> 动漫头像制作</span></a></li><li><a class="site-page child" target="_blank" rel="noopener" href="http://dfqshy.ysepan.com/"><i class="fa-fw fas fa-database"></i><span> FC游戏大全网盘</span></a></li><li><a class="site-page child" target="_blank" rel="noopener" href="https://www.yikm.net/"><i class="fa-fw fas fa-database"></i><span> 小霸王</span></a></li><li><a class="site-page child" target="_blank" rel="noopener" href="https://www.return8090.com/"><i class="fa-fw fas fa-database"></i><span> 小霸王(广告多)</span></a></li><li><a class="site-page child" target="_blank" rel="noopener" href="http://farm.dreamlove.top"><i class="fa-fw fas fa-database"></i><span> QQ农场经典版</span></a></li><li><a class="site-page child" target="_blank" rel="noopener" href="https://codepip.com/"><i class="fa-fw fas fa-database"></i><span> CssGame</span></a></li><li><a class="site-page child" target="_blank" rel="noopener" href="https://photo.123916.xyz/"><i class="fa-fw fas fa-database"></i><span> 有趣的照片</span></a></li></ul></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fas fa-list"></i><span> 其他</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></li><li><a class="site-page child" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></li><li><a class="site-page child" target="_blank" rel="noopener" href="https://buy.dreamlove.top/"><i class="fa-fw fas fa-heart"></i><span> 购物</span></a></li></ul></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image:url(https://dreamos.oss-cn-beijing.aliyuncs.com/gitblog/124124124.png)"><nav id="nav"><span id="blog-info"><a href="/" title="梦洁小站-属于你我的小天地"><span class="site-name">梦洁小站-属于你我的小天地</span></a></span><div id="menus"><div id="search-button"><a class="site-page social-icon search" href="javascript:void(0);"><i class="fas fa-search fa-fw"></i><span> 搜索</span></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 友情和链接</span></a></div><div class="menus_item"><a class="site-page" href="/navigation/"><i class="fa-fw fas fa-link"></i><span> 链接导航</span></a></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fas fa-list"></i><span> 私有化导航</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" target="_blank" rel="noopener" href="https://mark.123916.xyz/"><i class="fa-fw fas fa-database"></i><span> 导航</span></a></li><li><a class="site-page child" target="_blank" rel="noopener" href="https://cloud.reassurehome.com/"><i class="fa-fw fas fa-hippo"></i><span> Alist(私有)</span></a></li></ul></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fas fa-list"></i><span> 工具</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" target="_blank" rel="noopener" href="https://short.123916.xyz/027a2"><i class="fa-fw fas fa-paperclip"></i><span> clash免费订阅地址</span></a></li><li><a class="site-page child" target="_blank" rel="noopener" href="https://short.123916.xyz/admin/"><i class="fa-fw fas fa-paperclip"></i><span> 短链接生成</span></a></li></ul></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fas fa-list"></i><span> 好玩的</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" target="_blank" rel="noopener" href="https://make.girls.moe/#/"><i class="fa-fw fas fa-database"></i><span> 动漫头像制作</span></a></li><li><a class="site-page child" target="_blank" rel="noopener" href="http://dfqshy.ysepan.com/"><i class="fa-fw fas fa-database"></i><span> FC游戏大全网盘</span></a></li><li><a class="site-page child" target="_blank" rel="noopener" href="https://www.yikm.net/"><i class="fa-fw fas fa-database"></i><span> 小霸王</span></a></li><li><a class="site-page child" target="_blank" rel="noopener" href="https://www.return8090.com/"><i class="fa-fw fas fa-database"></i><span> 小霸王(广告多)</span></a></li><li><a class="site-page child" target="_blank" rel="noopener" href="http://farm.dreamlove.top"><i class="fa-fw fas fa-database"></i><span> QQ农场经典版</span></a></li><li><a class="site-page child" target="_blank" rel="noopener" href="https://codepip.com/"><i class="fa-fw fas fa-database"></i><span> CssGame</span></a></li><li><a class="site-page child" target="_blank" rel="noopener" href="https://photo.123916.xyz/"><i class="fa-fw fas fa-database"></i><span> 有趣的照片</span></a></li></ul></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fas fa-list"></i><span> 其他</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></li><li><a class="site-page child" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></li><li><a class="site-page child" target="_blank" rel="noopener" href="https://buy.dreamlove.top/"><i class="fa-fw fas fa-heart"></i><span> 购物</span></a></li></ul></div></div><div id="toggle-menu"><a class="site-page" href="javascript:void(0);"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">express,multer,jQuery前端后端上传单个文件</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2022-03-29T22:18:47.000Z" title="发表于 2022-03-29 22:18:47">2022-03-29</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2022-03-29T22:18:47.000Z" title="更新于 2022-03-29 22:18:47">2022-03-29</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/javscript/">javscript</a><i class="fas fa-angle-right post-meta-separator"></i><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/javscript/jQuery/">jQuery</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-wordcount"><i class="far fa-file-word fa-fw post-meta-icon"></i><span class="post-meta-label">字数总计:</span><span class="word-count">1.8k</span><span class="post-meta-separator">|</span><i class="far fa-clock fa-fw post-meta-icon"></i><span class="post-meta-label">阅读时长:</span><span>7分钟</span></span><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" data-flag-title="express,multer,jQuery前端后端上传单个文件"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"><i class="fa-solid fa-spinner fa-spin"></i></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h3 id="先安装基本的模块"><a href="#先安装基本的模块" class="headerlink" title="先安装基本的模块"></a>先安装基本的模块</h3><p>$ npm init -y</p><p>$ npm install express –save</p><p>$ npm install multer –save</p><h4 id="附上multer的github当中别人汉化的API文档"><a href="#附上multer的github当中别人汉化的API文档" class="headerlink" title="附上multer的github当中别人汉化的API文档"></a>附上multer的github当中别人汉化的API文档</h4><p><a target="_blank" rel="noopener" href="https://github.com/expressjs/multer/blob/master/doc/README-zh-cn.md">github地址</a></p><p class="note note-info">我这使用的nodemon,如果那么使用node运行,修改记得重启</p><h3 id="开始创建基本express-上传图片"><a href="#开始创建基本express-上传图片" class="headerlink" title="开始创建基本express(上传图片)"></a>开始创建基本express(上传图片)</h3><p>当前目录下创建fileup.js文件</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> express = <span class="built_in">require</span>(<span class="string">"express"</span>);</span><br><span class="line"><span class="keyword">const</span> multer = <span class="built_in">require</span>(<span class="string">"multer"</span>);</span><br><span class="line"><span class="keyword">var</span> app = <span class="title function_">express</span>();<span class="comment">//创建express实例</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//这里是为了后期没有跨域问题设置的静态资源目录</span></span><br><span class="line"><span class="comment">//__dirname为NodeJS全局变量: 返回运行当前js的文件夹的绝对路径</span></span><br><span class="line">app.<span class="title function_">use</span>(<span class="string">"/"</span>,express.<span class="title function_">static</span>(__dirname));</span><br><span class="line"></span><br><span class="line"><span class="comment">//后期ajax提交地址就为:http://localhost:3000/file 端口号可在下方自行设置</span></span><br><span class="line">app.<span class="title function_">post</span>(<span class="string">"/file"</span>,<span class="function">(<span class="params">req, res</span>) =></span> {</span><br><span class="line"> <span class="comment">//前期测试接口发送数据,没有问题再进行下一步</span></span><br><span class="line"> res.<span class="title function_">send</span>(<span class="string">"file ok"</span>);</span><br><span class="line">})</span><br><span class="line"></span><br><span class="line"><span class="comment">//设置在本地端口3000</span></span><br><span class="line">app.<span class="title function_">listen</span>(<span class="string">"3000"</span>, <span class="function">() =></span> {</span><br><span class="line"> <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">"-------------server start-------------------"</span>);</span><br><span class="line">})</span><br></pre></td></tr></table></figure><h4 id="测试是否正常"><a href="#测试是否正常" class="headerlink" title="测试是否正常"></a>测试是否正常</h4><p class="note note-info">这里使用postman测试</p><img src="https://dreamos.oss-cn-beijing.aliyuncs.com/gitblog/20220329212333.png"><p class="note note-info">测试没有问题再继续,不然后期很难改</p><h3 id="前端部分"><a href="#前端部分" class="headerlink" title="前端部分"></a>前端部分</h3><p>当前目录下创建up.html</p><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta"><!DOCTYPE <span class="keyword">html</span>></span></span><br><span class="line"><span class="tag"><<span class="name">html</span> <span class="attr">lang</span>=<span class="string">"en"</span>></span></span><br><span class="line"></span><br><span class="line"><span class="tag"><<span class="name">head</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">meta</span> <span class="attr">charset</span>=<span class="string">"UTF-8"</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">meta</span> <span class="attr">http-equiv</span>=<span class="string">"X-UA-Compatible"</span> <span class="attr">content</span>=<span class="string">"IE=edge"</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">meta</span> <span class="attr">name</span>=<span class="string">"viewport"</span> <span class="attr">content</span>=<span class="string">"width=device-width, initial-scale=1.0"</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">title</span>></span>Document<span class="tag"></<span class="name">title</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">script</span> <span class="attr">src</span>=<span class="string">"https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js"</span>></span><span class="tag"></<span class="name">script</span>></span></span><br><span class="line"><span class="tag"></<span class="name">head</span>></span></span><br><span class="line"></span><br><span class="line"><span class="tag"><<span class="name">body</span>></span></span><br><span class="line"> </span><br><span class="line"> <span class="tag"><<span class="name">input</span> <span class="attr">type</span>=<span class="string">"file"</span> <span class="attr">id</span>=<span class="string">"fileInput"</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">button</span> <span class="attr">id</span>=<span class="string">"btn"</span>></span>上传文件<span class="tag"></<span class="name">button</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">script</span>></span><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript"> <span class="comment">//这里不用jQuery获取是后期需要用到DOM元素,用jQuery也可以,用get或则[]就可以转为DOM元素</span></span></span><br><span class="line"><span class="language-javascript"> <span class="keyword">var</span> fileInput = <span class="variable language_">document</span>.<span class="title function_">getElementById</span>(<span class="string">"fileInput"</span>);</span></span><br><span class="line"><span class="language-javascript"> <span class="keyword">var</span> $btn = $(<span class="string">"#btn"</span>);</span></span><br><span class="line"><span class="language-javascript"> $btn.<span class="title function_">click</span>(<span class="keyword">function</span> (<span class="params"></span>) {</span></span><br><span class="line"><span class="language-javascript"> <span class="comment">//创建表单</span></span></span><br><span class="line"><span class="language-javascript"> <span class="comment">//这里new FormData不传入参数,因为这里只是上传一个图片,不存在其他数据,如果要传,传入form标签对应的DOM元素</span></span></span><br><span class="line"><span class="language-javascript"> <span class="keyword">var</span> formData = <span class="keyword">new</span> <span class="title class_">FormData</span>();</span></span><br><span class="line"><span class="language-javascript"> <span class="comment">//传入文件的key和value</span></span></span><br><span class="line"><span class="language-javascript"> <span class="comment">//fileInput.files[0]获取第一个文件,为什么是数组的我猜是因为开发js的为了和多文件上传一样用</span></span></span><br><span class="line"><span class="language-javascript"> formData.<span class="title function_">append</span>(<span class="string">"fileusers"</span>, fileInput.<span class="property">files</span>[<span class="number">0</span>]); </span></span><br><span class="line"><span class="language-javascript"> <span class="comment">//注意formData.append和formData.set区别,</span></span></span><br><span class="line"><span class="language-javascript"> $.<span class="title function_">ajax</span>({</span></span><br><span class="line"><span class="language-javascript"> <span class="string">"type"</span>: <span class="string">"post"</span>,</span></span><br><span class="line"><span class="language-javascript"> <span class="string">"url"</span>: <span class="string">"http://localhost:3000/file"</span>,</span></span><br><span class="line"><span class="language-javascript"> <span class="comment">//contentType:true;默认值,值为application/x-www-form-urlencoded</span></span></span><br><span class="line"><span class="language-javascript"> <span class="comment">//contentType:false;值为multipart/form-data</span></span></span><br><span class="line"><span class="language-javascript"> <span class="comment">//重要!!!!!!!!!!!!!!!!!!!!!!!!</span></span></span><br><span class="line"><span class="language-javascript"> <span class="string">"contentType"</span>: <span class="literal">false</span>,</span></span><br><span class="line"><span class="language-javascript"> <span class="comment">//重要!!!!!!!!!!!!!!!!!!!!!!!!</span></span></span><br><span class="line"><span class="language-javascript"> <span class="string">"processData"</span>: <span class="literal">false</span>,</span></span><br><span class="line"><span class="language-javascript"> <span class="string">"data"</span>: formData,</span></span><br><span class="line"><span class="language-javascript"> <span class="string">"success"</span>: <span class="keyword">function</span> (<span class="params">data</span>) {</span></span><br><span class="line"><span class="language-javascript"> <span class="variable language_">console</span>.<span class="title function_">log</span>(data);</span></span><br><span class="line"><span class="language-javascript"> },</span></span><br><span class="line"><span class="language-javascript"> <span class="string">"error"</span>: <span class="keyword">function</span> (<span class="params">error</span>) {</span></span><br><span class="line"><span class="language-javascript"> <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">"错误了"</span>, error);</span></span><br><span class="line"><span class="language-javascript"> }</span></span><br><span class="line"><span class="language-javascript"> });</span></span><br><span class="line"><span class="language-javascript"> });</span></span><br><span class="line"><span class="language-javascript"> </span><span class="tag"></<span class="name">script</span>></span></span><br><span class="line"><span class="tag"></<span class="name">body</span>></span></span><br><span class="line"></span><br><span class="line"><span class="tag"></<span class="name">html</span>></span></span><br></pre></td></tr></table></figure><h4 id="前端需要注意的"><a href="#前端需要注意的" class="headerlink" title="前端需要注意的"></a>前端需要注意的</h4><ol><li><p>注意formData.append和formData.set区别</p><ol><li>formData.append是如果存在值则会添加到后面,就像[{…},{…}]一样</li><li>formData.set是会覆盖前面的,只保留一个</li></ol></li><li><p><strong>jQuery上传数据的时候,记得设置下面二点</strong></p><ol><li>“processData”: false</li><li>“contentType”: false</li></ol></li><li><p>formData.append(“<strong>fileusers</strong>“, fileInput.files[0]); 当中<strong>fileusers</strong>在这里的作用</p><ol><li>后端multer需要通过用户定义的key,在这里也就是<strong>fileusers</strong>来设置数据</li></ol></li><li><p>new FormData();如果需要传入参数,是传入form标签对应的DOM元素</p></li></ol><h3 id="multer使用"><a href="#multer使用" class="headerlink" title="multer使用"></a>multer使用</h3><ul><li>大体分为而部分<ul><li>根据multer({})创建一个变量(对文件的一些设置和过滤),比如说 var <strong>fileOption</strong> = multer({})</li><li>依据这个变量创建单例fileOption.single(key)或者fileOption.array(key,maxcount)</li><li>使用这个单例作为过滤器来对文件进行过滤和操作(也就是中间件)</li></ul></li></ul><h4 id="multer-创建一个变量当中一些可以设置的-具体可以参考官网"><a href="#multer-创建一个变量当中一些可以设置的-具体可以参考官网" class="headerlink" title="multer({})创建一个变量当中一些可以设置的(具体可以参考官网)"></a>multer({})创建一个变量当中一些可以设置的(具体可以参考官网)</h4><ol><li><p><strong>limits</strong>{对象}(一个对象,指定一些数据大小的限制)</p><table><thead><tr><th>Key</th><th>Description</th><th>Default</th></tr></thead><tbody><tr><td><code>fieldNameSize</code></td><td>field 名字最大长度</td><td>100 bytes</td></tr><tr><td><code>fieldSize</code></td><td>field 值的最大长度</td><td>1MB</td></tr><tr><td><code>fields</code></td><td>非文件 field 的最大数量</td><td>无限</td></tr><tr><td><code>fileSize</code></td><td>在 multipart 表单中,文件最大长度 (字节单位)</td><td>无限</td></tr><tr><td><code>files</code></td><td>在 multipart 表单中,文件最大数量</td><td>无限</td></tr><tr><td><code>parts</code></td><td>在 multipart 表单中,part 传输的最大数量(fields + files)</td><td>无限</td></tr><tr><td><code>headerPairs</code></td><td>在 multipart 表单中,键值对最大组数</td><td>2000</td></tr></tbody></table></li><li><p><strong>fileFilter</strong>{函数}设置一个函数来控制什么文件可以上传以及什么文件应该跳过</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">function</span> <span class="title function_">fileFilter</span> (req, file, cb) {</span><br><span class="line"></span><br><span class="line"> <span class="comment">// 这个函数应该调用 `cb` 用boolean值来</span></span><br><span class="line"> <span class="comment">// 指示是否应接受该文件</span></span><br><span class="line"></span><br><span class="line"> <span class="comment">// 拒绝这个文件,使用`false`,像这样:</span></span><br><span class="line"> <span class="title function_">cb</span>(<span class="literal">null</span>, <span class="literal">false</span>)</span><br><span class="line"></span><br><span class="line"> <span class="comment">// 接受这个文件,使用`true`,像这样:</span></span><br><span class="line"> <span class="title function_">cb</span>(<span class="literal">null</span>, <span class="literal">true</span>)</span><br><span class="line"></span><br><span class="line"> <span class="comment">// 如果有问题,你可以总是这样发送一个错误:</span></span><br><span class="line"> <span class="title function_">cb</span>(<span class="keyword">new</span> <span class="title class_">Error</span>(<span class="string">'I don\'t have a clue!'</span>))</span><br><span class="line"></span><br><span class="line">}</span><br></pre></td></tr></table></figure></li><li><p><strong>storage</strong>{返回值} 为运行multer.diskStorage({设置参数})的返回值</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> storage = multer.<span class="title function_">diskStorage</span>({</span><br><span class="line"> <span class="comment">//定义文件存储位置</span></span><br><span class="line"> <span class="attr">destination</span>: <span class="keyword">function</span> (<span class="params">req, file, cb</span>) {</span><br><span class="line"> <span class="comment">//回调函数file当中 </span></span><br><span class="line"> <span class="comment">/* file对象的内容{</span></span><br><span class="line"><span class="comment"> fieldname: 'fileusers',//通过formData.append(key,value)添加的key值</span></span><br><span class="line"><span class="comment"> originalname: '_1464815392_.bmp',//文件名</span></span><br><span class="line"><span class="comment"> encoding: '7bit',//编码</span></span><br><span class="line"><span class="comment"> mimetype: 'image/bmp' //mimeType类型</span></span><br><span class="line"><span class="comment"> } */</span></span><br><span class="line"> <span class="comment">//cb(null,存储路径)</span></span><br><span class="line"> <span class="title function_">cb</span>(<span class="literal">null</span>, <span class="string">'./myfile'</span>);<span class="comment">//可能是以fileup.js在哪里运行为基准的参考的</span></span><br><span class="line"> },</span><br><span class="line"> <span class="comment">//重定义上传的文件</span></span><br><span class="line"> <span class="attr">filename</span>: <span class="keyword">function</span> (<span class="params">req, file, cb</span>) {</span><br><span class="line"> <span class="comment">//cb(null,新文件名称)</span></span><br><span class="line"> <span class="title function_">cb</span>(<span class="literal">null</span>, file.<span class="property">fieldname</span> + <span class="string">'-'</span> + <span class="title class_">Date</span>.<span class="title function_">now</span>())</span><br><span class="line"> }</span><br><span class="line">})</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> upload = <span class="title function_">multer</span>({ <span class="attr">storage</span>: storage })</span><br></pre></td></tr></table></figure></li></ol><h4 id="multer代码"><a href="#multer代码" class="headerlink" title="multer代码"></a>multer代码</h4><p>当前目录下创建fileup.js文件和myfile文件夹</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> express = <span class="built_in">require</span>(<span class="string">"express"</span>);</span><br><span class="line"><span class="keyword">const</span> multer = <span class="built_in">require</span>(<span class="string">"multer"</span>);</span><br><span class="line"><span class="keyword">var</span> app = <span class="title function_">express</span>(); <span class="comment">//创建express实例</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//这里是为了后期没有跨域问题设置的静态资源目录</span></span><br><span class="line"><span class="comment">//__dirname为NodeJS全局变量: 返回运行当前js的文件夹的绝对路径</span></span><br><span class="line">app.<span class="title function_">use</span>(<span class="string">"/"</span>, express.<span class="title function_">static</span>(__dirname));</span><br><span class="line"></span><br><span class="line"><span class="comment">//根据multer({})创建一个变量(对文件的一些设置和过滤)</span></span><br><span class="line"><span class="keyword">var</span> fileOption = <span class="title function_">multer</span>({</span><br><span class="line"> <span class="comment">//上传限制</span></span><br><span class="line"> <span class="attr">limits</span>: {</span><br><span class="line"> <span class="attr">fileSize</span>: <span class="number">5</span> * <span class="number">1024</span> * <span class="number">1024</span> * <span class="number">1024</span>,</span><br><span class="line"> <span class="attr">files</span>: <span class="number">1</span></span><br><span class="line"> },</span><br><span class="line"> <span class="comment">//存储位置</span></span><br><span class="line"> <span class="attr">storage</span>: multer.<span class="title function_">diskStorage</span>({</span><br><span class="line"> <span class="attr">destination</span>: <span class="keyword">function</span> (<span class="params">req, file, cb</span>) {</span><br><span class="line"> <span class="title function_">cb</span>(<span class="literal">null</span>, <span class="string">'./myfile'</span>);</span><br><span class="line"> },</span><br><span class="line"> <span class="comment">//重定义上传的文件</span></span><br><span class="line"> <span class="attr">filename</span>: <span class="keyword">function</span> (<span class="params">req, file, cb</span>) {</span><br><span class="line"> <span class="comment">//cb(null,新文件名称)</span></span><br><span class="line"> <span class="title function_">cb</span>(<span class="literal">null</span>, file.<span class="property">fieldname</span> + <span class="string">'-'</span> + <span class="title class_">Date</span>.<span class="title function_">now</span>()+<span class="string">"-"</span>+file.<span class="property">originalname</span>);</span><br><span class="line"> }</span><br><span class="line"> }),</span><br><span class="line"> <span class="comment">//过滤</span></span><br><span class="line"> <span class="attr">fileFilter</span>: <span class="keyword">function</span> (<span class="params">req, file, cb</span>) {</span><br><span class="line"> <span class="comment">//这里就不写了~,如果添加了这个对象,一定要设置cb(null,true);不然数据过不去</span></span><br><span class="line"> <span class="title function_">cb</span>(<span class="literal">null</span>,<span class="literal">true</span>);</span><br><span class="line"> }</span><br><span class="line">});</span><br><span class="line"></span><br><span class="line"><span class="comment">//依据这个变量创建单例multer.single(key)或者multer.array(key,maxcount)</span></span><br><span class="line"><span class="keyword">var</span> fileOptionFunction=fileOption.<span class="title function_">single</span>(<span class="string">"fileusers"</span>);<span class="comment">//传入通过formData.append(key,value)添加的key值</span></span><br><span class="line"><span class="comment">//后期ajax提交地址就为:http://localhost:3000/file 端口号可在下方自行设置</span></span><br><span class="line">app.<span class="title function_">post</span>(<span class="string">"/file"</span>, <span class="function">(<span class="params">req, res</span>) =></span> {</span><br><span class="line"> <span class="comment">//这里传入的是app.post当中的req,和res</span></span><br><span class="line"> <span class="title function_">fileOptionFunction</span>(req,res,<span class="function">(<span class="params">error</span>)=></span>{</span><br><span class="line"> <span class="keyword">if</span> (error) {</span><br><span class="line"> <span class="keyword">return</span> res.<span class="title function_">send</span>({<span class="attr">err</span>:-<span class="number">1</span>,<span class="attr">msg</span>:<span class="string">'上传图片不能大于5M'</span>})</span><br><span class="line"> }</span><br><span class="line"> res.<span class="title function_">send</span>({<span class="attr">error</span>:<span class="number">0</span>,<span class="attr">msg</span>:<span class="string">"文件上传成功"</span>})</span><br><span class="line"> })</span><br><span class="line">})</span><br><span class="line"></span><br><span class="line"><span class="comment">//设置在本地端口3000</span></span><br><span class="line">app.<span class="title function_">listen</span>(<span class="string">"3000"</span>, <span class="function">() =></span> {</span><br><span class="line"> <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">"-------------server start-------------------"</span>);</span><br><span class="line">})</span><br></pre></td></tr></table></figure><p><img src="https://dreamos.oss-cn-beijing.aliyuncs.com/gitblog/20220329220433.png"></p><h4 id="multer部分需要注意的"><a href="#multer部分需要注意的" class="headerlink" title="multer部分需要注意的"></a>multer部分需要注意的</h4><ol><li>如果使用app.post(“/file”, uploadOption.single(“fileusers”),(req, res) => { });好像无法处理文件过大时候的异常</li><li>如果使用上面fileup.js文件当中的方式,可以在回调当中处理文件过大时候的异常</li><li>如果设置了文件过滤,那么遇到不符合的文件扩展名,req.file的值会为undefined</li><li>如果设置了fileFilter文件过滤,一定要设置cb(null,true)(通过),或者cb(null,false);(不通过),不然留空的话数据会一直处于”pending”状态</li></ol><h3 id="具体文件目录结构和参考完整代码下载"><a href="#具体文件目录结构和参考完整代码下载" class="headerlink" title="具体文件目录结构和参考完整代码下载"></a>具体文件目录结构和参考完整代码下载</h3><p>启动后浏览器输入<a target="_blank" rel="noopener" href="http://localhost:3000/up.html">http://localhost:3000/up.html</a></p><h4 id="目录文件结构"><a href="#目录文件结构" class="headerlink" title="目录文件结构"></a>目录文件结构</h4><p><img src="https://dreamos.oss-cn-beijing.aliyuncs.com/gitblog/20220329220518.png"></p><h4 id="参考完整代码下载"><a href="#参考完整代码下载" class="headerlink" title="参考完整代码下载"></a>参考完整代码下载</h4><p><a target="_blank" rel="noopener" href="https://github.com/superBiuBiuMan/demonForUpFile">express+multer+jQuery前端后端上传单个文件演示</a></p></article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta"><i class="fas fa-circle-user fa-fw"></i>文章作者: </span><span class="post-copyright-info"><a href="https://www.dreamlove.top">梦洁</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta"><i class="fas fa-square-arrow-up-right fa-fw"></i>文章链接: </span><span class="post-copyright-info"><a href="https://www.dreamlove.top/273b44bb.html">https://www.dreamlove.top/273b44bb.html</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta"><i class="fas fa-circle-exclamation fa-fw"></i>版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外,均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://www.dreamlove.top" target="_blank">梦洁小站-属于你我的小天地</a>!</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/NodeJs/">NodeJs</a></div><div class="post_share"><div class="social-share" data-image="https://dreamos.oss-cn-beijing.aliyuncs.com/gitblog/124124124.png" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://lib.baomitu.com/butterfly-extsrc/1.1.3/sharejs/dist/css/share.min.css" media="print" onload='this.media="all"'><script src="https://lib.baomitu.com/butterfly-extsrc/1.1.3/sharejs/dist/js/social-share.min.js" defer></script></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/7c53a2bd.html" title="防抖节流的使用和封装成函数"><img class="cover" src="https://dreamos.oss-cn-beijing.aliyuncs.com/gitblog/舔狗日记.png" onerror='onerror=null,src="/img/404.png"' alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">防抖节流的使用和封装成函数</div></div></a></div><div class="next-post pull-right"><a href="/c6b5dfff.html" title="js当中的图片懒加载,懒加载的一些概念和相关知识点"><img class="cover" src="https://dreamos.oss-cn-beijing.aliyuncs.com/gitblog/20220228215037.png" onerror='onerror=null,src="/img/404.png"' alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">js当中的图片懒加载,懒加载的一些概念和相关知识点</div></div></a></div></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span>相关推荐</span></div><div class="relatedPosts-list"><div><a href="/933f6d7f.html" title="nvm安装版本后设置默认镜像地址和nvm list available出现空白解决办法"><img class="cover" src="https://dreamos.oss-cn-beijing.aliyuncs.com/gitblog/202403181444406.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2024-01-14</div><div class="title">nvm安装版本后设置默认镜像地址和nvm list available出现空白解决办法</div></div></a></div><div><a href="/efae7552.html" title="我来图书馆小程序加密后抓包分析反编译抢位置"><img class="cover" src="https://dreamos.oss-cn-beijing.aliyuncs.com/gitblog/20220228215037.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-04-23</div><div class="title">我来图书馆小程序加密后抓包分析反编译抢位置</div></div></a></div><div><a href="/b691d3ea.html" title="我来图书馆小程序抓包抢位置"><img class="cover" src="https://dreamos.oss-cn-beijing.aliyuncs.com/gitblog/20220228215037.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-04-06</div><div class="title">我来图书馆小程序抓包抢位置</div></div></a></div><div><a href="/96254c31.html" title="江西财经大学智慧江财登录分析"><img class="cover" src="https://dreamos.oss-cn-beijing.aliyuncs.com/gitblog/202206280827701.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-06-27</div><div class="title">江西财经大学智慧江财登录分析</div></div></a></div><div><a href="/5c543e29.html" title="问卷星问卷抓包分析"><img class="cover" src="https://dreamos.oss-cn-beijing.aliyuncs.com/gitblog/202206280826561.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-06-26</div><div class="title">问卷星问卷抓包分析</div></div></a></div><div><a href="/d9f65d5.html" title="使用nodejs导出md/Markdown文档当中的图片到本地并替换原始图片链接为本地图片链接"><img class="cover" src="https://dreamos.oss-cn-beijing.aliyuncs.com/gitblog/202205281118457.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-05-28</div><div class="title">使用nodejs导出md/Markdown文档当中的图片到本地并替换原始图片链接为本地图片链接</div></div></a></div></div></div><hr class="custom-hr"><div id="post-comment"><div class="comment-head"><div class="comment-headline"><i class="fas fa-comments fa-fw"></i><span> 评论</span></div></div><div class="comment-wrap"><div><div id="artalk-wrap"></div></div></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="https://oss.dreamlove.top/i/2024/02/02/nw24wu.png" onerror='this.onerror=null,this.src="/img/404.png"' alt="avatar"></div><div class="author-info__name">梦洁</div><div class="author-info__description">小小的字,有大大的梦想~分享我的前端学习过程,经历,错误,和一些其他折腾过程</div></div><div class="card-info-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">170</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">94</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">70</div></a></div><a id="card-info-btn" target="_blank" rel="noopener" href="https://github.com/superBiuBiuMan"><i class="fab fa-github"></i><span>关注下我(* ̄▽ ̄*)</span></a><div class="card-info-social-icons is-center"><a class="social-icon" href="mailto:[email protected]" target="_blank" title="Email"><i class="fas fa-envelope" style="color:#4a7dbe"></i></a></div></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn fa-shake"></i><span>公告</span></div><div class="announcement_content">不断更新中,有问题请留言回复(会通过邮箱提醒~)</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span><span class="toc-percentage"></span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%85%88%E5%AE%89%E8%A3%85%E5%9F%BA%E6%9C%AC%E7%9A%84%E6%A8%A1%E5%9D%97"><span class="toc-number">1.</span> <span class="toc-text">先安装基本的模块</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E9%99%84%E4%B8%8Amulter%E7%9A%84github%E5%BD%93%E4%B8%AD%E5%88%AB%E4%BA%BA%E6%B1%89%E5%8C%96%E7%9A%84API%E6%96%87%E6%A1%A3"><span class="toc-number">1.1.</span> <span class="toc-text">附上multer的github当中别人汉化的API文档</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%BC%80%E5%A7%8B%E5%88%9B%E5%BB%BA%E5%9F%BA%E6%9C%ACexpress-%E4%B8%8A%E4%BC%A0%E5%9B%BE%E7%89%87"><span class="toc-number">2.</span> <span class="toc-text">开始创建基本express(上传图片)</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%B5%8B%E8%AF%95%E6%98%AF%E5%90%A6%E6%AD%A3%E5%B8%B8"><span class="toc-number">2.1.</span> <span class="toc-text">测试是否正常</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%89%8D%E7%AB%AF%E9%83%A8%E5%88%86"><span class="toc-number">3.</span> <span class="toc-text">前端部分</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%89%8D%E7%AB%AF%E9%9C%80%E8%A6%81%E6%B3%A8%E6%84%8F%E7%9A%84"><span class="toc-number">3.1.</span> <span class="toc-text">前端需要注意的</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#multer%E4%BD%BF%E7%94%A8"><span class="toc-number">4.</span> <span class="toc-text">multer使用</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#multer-%E5%88%9B%E5%BB%BA%E4%B8%80%E4%B8%AA%E5%8F%98%E9%87%8F%E5%BD%93%E4%B8%AD%E4%B8%80%E4%BA%9B%E5%8F%AF%E4%BB%A5%E8%AE%BE%E7%BD%AE%E7%9A%84-%E5%85%B7%E4%BD%93%E5%8F%AF%E4%BB%A5%E5%8F%82%E8%80%83%E5%AE%98%E7%BD%91"><span class="toc-number">4.1.</span> <span class="toc-text">multer({})创建一个变量当中一些可以设置的(具体可以参考官网)</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#multer%E4%BB%A3%E7%A0%81"><span class="toc-number">4.2.</span> <span class="toc-text">multer代码</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#multer%E9%83%A8%E5%88%86%E9%9C%80%E8%A6%81%E6%B3%A8%E6%84%8F%E7%9A%84"><span class="toc-number">4.3.</span> <span class="toc-text">multer部分需要注意的</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%85%B7%E4%BD%93%E6%96%87%E4%BB%B6%E7%9B%AE%E5%BD%95%E7%BB%93%E6%9E%84%E5%92%8C%E5%8F%82%E8%80%83%E5%AE%8C%E6%95%B4%E4%BB%A3%E7%A0%81%E4%B8%8B%E8%BD%BD"><span class="toc-number">5.</span> <span class="toc-text">具体文件目录结构和参考完整代码下载</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E7%9B%AE%E5%BD%95%E6%96%87%E4%BB%B6%E7%BB%93%E6%9E%84"><span class="toc-number">5.1.</span> <span class="toc-text">目录文件结构</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%8F%82%E8%80%83%E5%AE%8C%E6%95%B4%E4%BB%A3%E7%A0%81%E4%B8%8B%E8%BD%BD"><span class="toc-number">5.2.</span> <span class="toc-text">参考完整代码下载</span></a></li></ol></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/bbed5cd2.html" title="大家好我是夯大力,这是我的影视,大力金刚等系列"><img src="https://dreamos.oss-cn-beijing.aliyuncs.com/gitblog/202410091600476.png" onerror='this.onerror=null,this.src="/img/404.png"' alt="大家好我是夯大力,这是我的影视,大力金刚等系列"></a><div class="content"><a class="title" href="/bbed5cd2.html" title="大家好我是夯大力,这是我的影视,大力金刚等系列">大家好我是夯大力,这是我的影视,大力金刚等系列</a><time datetime="2024-10-21T08:12:21.000Z" title="发表于 2024-10-21 08:12:21">2024-10-21</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/91ff580f.html" title="Pycharm搭配miniConda(Anaconda的轻量版本)建立虚拟环境管理不同项目不同依赖">Pycharm搭配miniConda(Anaconda的轻量版本)建立虚拟环境管理不同项目不同依赖</a><time datetime="2024-10-18T21:22:02.000Z" title="发表于 2024-10-18 21:22:02">2024-10-18</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/50575b3e.html" title="独角数卡,打开商品列表出现Undefined variable form的解决办法"><img src="https://dreamos.oss-cn-beijing.aliyuncs.com/gitblog/202408241535616.png" onerror='this.onerror=null,this.src="/img/404.png"' alt="独角数卡,打开商品列表出现Undefined variable form的解决办法"></a><div class="content"><a class="title" href="/50575b3e.html" title="独角数卡,打开商品列表出现Undefined variable form的解决办法">独角数卡,打开商品列表出现Undefined variable form的解决办法</a><time datetime="2024-08-24T15:31:38.000Z" title="发表于 2024-08-24 15:31:38">2024-08-24</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/6ace453b.html" title="前端常用npm库大全-vue,react,通用(持续更新)"><img src="https://dreamos.oss-cn-beijing.aliyuncs.com/gitblog/202408241528979.png" onerror='this.onerror=null,this.src="/img/404.png"' alt="前端常用npm库大全-vue,react,通用(持续更新)"></a><div class="content"><a class="title" href="/6ace453b.html" title="前端常用npm库大全-vue,react,通用(持续更新)">前端常用npm库大全-vue,react,通用(持续更新)</a><time datetime="2024-08-24T15:24:59.000Z" title="发表于 2024-08-24 15:24:59">2024-08-24</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/f4cb1979.html" title="基于vue-pdf-embed的二开PDF预览的通用组件"><img src="https://dreamos.oss-cn-beijing.aliyuncs.com/gitblog/202408241539662.png" onerror='this.onerror=null,this.src="/img/404.png"' alt="基于vue-pdf-embed的二开PDF预览的通用组件"></a><div class="content"><a class="title" href="/f4cb1979.html" title="基于vue-pdf-embed的二开PDF预览的通用组件">基于vue-pdf-embed的二开PDF预览的通用组件</a><time datetime="2024-08-11T08:53:00.000Z" title="发表于 2024-08-11 08:53:00">2024-08-11</time></div></div></div></div></div></div></main><footer id="footer" style="background-image:url(https://dreamos.oss-cn-beijing.aliyuncs.com/gitblog/124124124.png)"><div id="footer-wrap"><div class="copyright">©2021 - 2024 By 梦洁</div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside-config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><a id="to_comment" href="#post-comment" title="直达评论"><i class="fas fa-comments"></i></a><button id="go-up" type="button" title="回到顶部"><span class="scroll-percent"></span><i class="fas fa-arrow-up"></i></button></div></div><div><script src="/js/utils.js?v=4.13.0"></script><script src="/js/main.js?v=4.13.0"></script><script src="https://lib.baomitu.com/fancyapps-ui/5.0.33/fancybox/fancybox.umd.min.js"></script><div class="js-pjax"><script>(()=>{let t=null;const e=()=>{if(t=Artalk.init(Object.assign({el:"#artalk-wrap",server:"https://artalk.dreamlove.top",site:"梦洁小站",pageKey:location.pathname,darkMode:"dark"===document.documentElement.getAttribute("data-theme")},null)),"null"===GLOBAL_CONFIG.lightbox)return;t.on("list-loaded",()=>{t.ctx.get("list").getCommentNodes().forEach(t=>{const e=t.getRender().$content;btf.loadLightbox(e.querySelectorAll("img:not([atk-emoticon])"))})});btf.addGlobalFn("pjax",()=>{t.destroy()},"destroyArtalk")},a=async()=>{"object"==typeof Artalk||(await getCSS("https://lib.baomitu.com/artalk/2.8.2/Artalk.min.css"),await getScript("https://lib.baomitu.com/artalk/2.8.2/Artalk.min.js")),e()};btf.addGlobalFn("themeChange",e=>{const a=document.getElementById("artalk-wrap");if(!a||!a.children.length)return;const l="dark"===e;t.setDarkMode(l)},"artalk"),a()})()</script></div><script id="canvas_nest" defer color="0,0,255" opacity="0.7" zindex="-1" count="99" mobile="false" src="https://lib.baomitu.com/butterfly-extsrc/1.1.3/canvas-nest.min.js"></script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script><div id="algolia-search"><div class="search-dialog"><nav class="search-nav"><span class="search-dialog-title">搜索</span><button class="search-close-button"><i class="fas fa-times"></i></button></nav><div class="search-wrap"><div id="algolia-search-input"></div><hr><div id="algolia-search-results"><div id="algolia-hits"></div><div id="algolia-pagination"></div><div id="algolia-info"><div class="algolia-stats"></div><div class="algolia-poweredBy"></div></div></div></div></div><div id="search-mask"></div><script src="https://lib.baomitu.com/algoliasearch/4.22.1/algoliasearch-lite.umd.min.js"></script><script src="https://lib.baomitu.com/instantsearch.js/4.65.0/instantsearch.production.min.js"></script><script src="/js/search/algolia.js?v=4.13.0"></script></div></div></body></html>