You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
1 line
6.2 KiB
JavaScript
1 line
6.2 KiB
JavaScript
"use strict";(self.webpackChunkfurion=self.webpackChunkfurion||[]).push([[1736],{1744:(e,t,n)=>{n.r(t),n.d(t,{assets:()=>s,contentTitle:()=>i,default:()=>u,frontMatter:()=>a,metadata:()=>l,toc:()=>d});var o=n(5893),r=n(1151);n(4996);const a={slug:"fileupload-download",title:"2. \u6587\u4ef6\u4e0a\u4f20\u4e0b\u8f7d",author:"dotNET China",author_title:"\u8ba9 .NET \u5f00\u53d1\u66f4\u7b80\u5355\uff0c\u66f4\u901a\u7528\uff0c\u66f4\u6d41\u884c\u3002",author_url:"https://gitee.com/dotnetchina",author_image_url:"https://i.loli.net/2021/01/19/M8q5a3OTZWUKicl.png",tags:["furion","furos",".net",".netcore",".net5","upload","download"]},i=void 0,l={permalink:"/blog/fileupload-download",editUrl:"https://gitee.com/dotnetchina/Furion/tree/v4/handbook/blog/2021-02-02-fileupload-download.mdx",source:"@site/blog/2021-02-02-fileupload-download.mdx",title:"2. \u6587\u4ef6\u4e0a\u4f20\u4e0b\u8f7d",description:"\u6587\u4ef6\u4e0b\u8f7d",date:"2021-02-02T00:00:00.000Z",formattedDate:"February 2, 2021",tags:[{label:"furion",permalink:"/blog/tags/furion"},{label:"furos",permalink:"/blog/tags/furos"},{label:".net",permalink:"/blog/tags/net"},{label:".netcore",permalink:"/blog/tags/netcore"},{label:".net5",permalink:"/blog/tags/net-5"},{label:"upload",permalink:"/blog/tags/upload"},{label:"download",permalink:"/blog/tags/download"}],readingTime:1.435,hasTruncateMarker:!0,authors:[{name:"dotNET China",title:"\u8ba9 .NET \u5f00\u53d1\u66f4\u7b80\u5355\uff0c\u66f4\u901a\u7528\uff0c\u66f4\u6d41\u884c\u3002",url:"https://gitee.com/dotnetchina",imageURL:"https://i.loli.net/2021/01/19/M8q5a3OTZWUKicl.png"}],frontMatter:{slug:"fileupload-download",title:"2. \u6587\u4ef6\u4e0a\u4f20\u4e0b\u8f7d",author:"dotNET China",author_title:"\u8ba9 .NET \u5f00\u53d1\u66f4\u7b80\u5355\uff0c\u66f4\u901a\u7528\uff0c\u66f4\u6d41\u884c\u3002",author_url:"https://gitee.com/dotnetchina",author_image_url:"https://i.loli.net/2021/01/19/M8q5a3OTZWUKicl.png",tags:["furion","furos",".net",".netcore",".net5","upload","download"]},unlisted:!1,prevItem:{title:"3. .NET 6 Preview 5 \u5c1d\u9c9c",permalink:"/blog/net6-preview5"},nextItem:{title:"1. HttpContext \u5e94\u7528",permalink:"/blog/httpcontext"}},s={authorsImageUrls:[void 0]},d=[{value:"\u6587\u4ef6\u4e0b\u8f7d",id:"\u6587\u4ef6\u4e0b\u8f7d",level:2},{value:"\u6587\u4ef6\u4e0a\u4f20",id:"\u6587\u4ef6\u4e0a\u4f20",level:2}];function c(e){const t={admonition:"admonition",code:"code",h2:"h2",p:"p",pre:"pre",...(0,r.a)(),...e.components};return(0,o.jsxs)(o.Fragment,{children:[(0,o.jsx)(t.h2,{id:"\u6587\u4ef6\u4e0b\u8f7d",children:"\u6587\u4ef6\u4e0b\u8f7d"}),"\n",(0,o.jsx)(t.pre,{children:(0,o.jsx)(t.code,{className:"language-cs",metastring:"showLineNumbers ",children:'[HttpGet, NonUnify]\r\npublic IActionResult FileDownload(string path, string fileName)\r\n{\r\n string filePath = "\u8fd9\u91cc\u83b7\u53d6\u5b8c\u6574\u7684\u6587\u4ef6\u4e0b\u8f7d\u8def\u5f84";\r\n return new FileStreamResult(new FileStream(filePath, FileMode.Open), "application/octet-stream") { FileDownloadName = fileName };\r\n}\n'})}),"\n",(0,o.jsxs)(t.admonition,{title:"\u5173\u4e8e\u524d\u7aef\u83b7\u53d6\u6587\u4ef6\u540d",type:"note",children:[(0,o.jsx)(t.p,{children:"\u5982\u679c\u524d\u7aef\u83b7\u53d6\u4e0d\u5230\u6587\u4ef6\u5939\uff0c\u53ef\u6dfb\u52a0\u4ee5\u4e0b\u914d\u7f6e\uff1a"}),(0,o.jsx)(t.pre,{children:(0,o.jsx)(t.code,{className:"language-cs",metastring:"showLineNumbers ",children:'_httpContextAccessor.HttpContext.Response.Headers.Add("Content-Disposition", $"attachment; filename={\u6587\u4ef6\u540d}");\r\n_httpContextAccessor.HttpContext.Response.Headers.Add("Access-Control-Expose-Headers", "Content-Disposition");\n'})})]}),"\n",(0,o.jsx)(t.h2,{id:"\u6587\u4ef6\u4e0a\u4f20",children:"\u6587\u4ef6\u4e0a\u4f20"}),"\n",(0,o.jsx)(t.pre,{children:(0,o.jsx)(t.code,{className:"language-cs",metastring:"showLineNumbers ",children:'[HttpPost, NonUnify]\r\npublic async Task<IActionResult> UploadFileAsync(List<IFormFile> files)\r\n{\r\n // \u4fdd\u5b58\u5230\u7f51\u7ad9\u6839\u76ee\u5f55\u4e0b\u7684 uploads \u76ee\u5f55\r\n var savePath = Path.Combine(App.HostEnvironment.ContentRootPath, "uploads");\r\n if(!Directory.Exists(savePath)) Directory.CreateDirectory(savePath);\r\n\r\n long size = files.Sum(f => f.Length);\r\n\r\n foreach (var formFile in files)\r\n {\r\n if (formFile.Length > 0)\r\n {\r\n // \u907f\u514d\u6587\u4ef6\u540d\u91cd\u590d\uff0c\u91c7\u7528 GUID \u751f\u6210\r\n var filePath = Path.Combine(savePath, Guid.NewGuid().ToString("N") + Path.GetExtension(formFile.FileName)); // \u53ef\u4ee5\u66ff\u4ee3\u4e3a\u4f60\u9700\u8981\u5b58\u50a8\u7684\u771f\u5b9e\u8def\u5f84\r\n\r\n using (var stream = System.IO.File.Create(filePath))\r\n {\r\n await formFile.CopyToAsync(stream);\r\n }\r\n }\r\n }\r\n\r\n // \u5728\u52a8\u6001 API \u76f4\u63a5\u8fd4\u56de\u5bf9\u8c61\u5373\u53ef\uff0c\u65e0\u9700 OK \u548c IActionResult\r\n return Ok(new { count = files.Count, size });\r\n}\n'})}),"\n",(0,o.jsxs)(t.admonition,{title:"\u5173\u4e8e\u4f7f\u7528axios\u4e0a\u4f20\u6587\u4ef6,\u65b9\u6cd5\u83b7\u53d6\u5230\u53c2\u6570files.Count=0",type:"note",children:[(0,o.jsx)(t.p,{children:"axios\u8bf7\u6c42\u914d\u7f6e"}),(0,o.jsx)(t.pre,{children:(0,o.jsx)(t.code,{className:"language-cs",metastring:"showLineNumbers ",children:' let formData = new FormData();\r\n formData.append("files", this.file); //files\u9700\u4e0e\u65b9\u6cd5\u91cc\u7684\u53c2\u6570files\u540d\u79f0\u4e00\u6837\r\n let config = {\r\n headers: {\r\n "Content-Type": "multipart/form-data",\r\n },\r\n };\r\n axios.post(this.uploadURL, formData, config).then((res) => {//\u9700\u5f15\u5165axios\r\n console.log(res);\r\n });\n'})})]})]})}function u(e={}){const{wrapper:t}={...(0,r.a)(),...e.components};return t?(0,o.jsx)(t,{...e,children:(0,o.jsx)(c,{...e})}):c(e)}},1151:(e,t,n)=>{n.d(t,{Z:()=>l,a:()=>i});var o=n(7294);const r={},a=o.createContext(r);function i(e){const t=o.useContext(a);return o.useMemo((function(){return"function"==typeof e?e(t):{...t,...e}}),[t,e])}function l(e){let t;return t=e.disableParentContext?"function"==typeof e.components?e.components(r):e.components||r:i(e.components),o.createElement(a.Provider,{value:t},e.children)}}}]); |