"use strict";(self.webpackChunkfurion=self.webpackChunkfurion||[]).push([[1215],{5775:(e,n,r)=>{r.r(n),r.d(n,{assets:()=>d,contentTitle:()=>c,default:()=>x,frontMatter:()=>t,metadata:()=>o,toc:()=>a});var s=r(5893),i=r(1151),l=r(510);r(4996);const t={id:"file-provider",title:"31. \u865a\u62df\u6587\u4ef6\u7cfb\u7edf",sidebar_label:"31. \u865a\u62df\u6587\u4ef6\u7cfb\u7edf\uff08\u4e0a\u4f20\u4e0b\u8f7d\uff09"},c=void 0,o={id:"file-provider",title:"31. \u865a\u62df\u6587\u4ef6\u7cfb\u7edf",description:"\ud83d\udcdd \u6a21\u5757\u66f4\u65b0\u65e5\u5fd7",source:"@site/docs/file-provider.mdx",sourceDirName:".",slug:"/file-provider",permalink:"/docs/file-provider",draft:!1,unlisted:!1,editUrl:"https://gitee.com/dotnetchina/Furion/tree/v4/handbook/docs/file-provider.mdx",tags:[],version:"current",lastUpdatedBy:"\u767e\u5c0f\u50e7",lastUpdatedAt:1696858376,formattedLastUpdatedAt:"Oct 9, 2023",frontMatter:{id:"file-provider",title:"31. \u865a\u62df\u6587\u4ef6\u7cfb\u7edf",sidebar_label:"31. \u865a\u62df\u6587\u4ef6\u7cfb\u7edf\uff08\u4e0a\u4f20\u4e0b\u8f7d\uff09"},sidebar:"docs",previous:{title:"30. \u8131\u654f\u5904\u7406",permalink:"/docs/sensitive-detection"},next:{title:"32. \u4f1a\u8bdd\u548c\u72b6\u6001\u7ba1\u7406",permalink:"/docs/sesssion-state"}},d={},a=[{value:"31.1 \u5173\u4e8e\u6587\u4ef6\u7cfb\u7edf",id:"311-\u5173\u4e8e\u6587\u4ef6\u7cfb\u7edf",level:2},{value:"31.1.1 \u6587\u4ef6\u7cfb\u7edf\u7c7b\u578b",id:"3111-\u6587\u4ef6\u7cfb\u7edf\u7c7b\u578b",level:3},{value:"31.2 \u6ce8\u518c\u865a\u62df\u6587\u4ef6\u7cfb\u7edf\u670d\u52a1",id:"312-\u6ce8\u518c\u865a\u62df\u6587\u4ef6\u7cfb\u7edf\u670d\u52a1",level:2},{value:"31.3 \u83b7\u53d6\u6587\u4ef6\u7cfb\u7edf IFileProvider
\u5b9e\u4f8b",id:"313-\u83b7\u53d6\u6587\u4ef6\u7cfb\u7edf-ifileprovider-\u5b9e\u4f8b",level:2},{value:"31.3.1 Func<FileProviderTypes, object, IFileProvider>
\u65b9\u5f0f",id:"3131-funcfileprovidertypes-object-ifileprovider-\u65b9\u5f0f",level:3},{value:"31.3.2 FS
\u9759\u6001\u7c7b\u65b9\u5f0f",id:"3132-fs-\u9759\u6001\u7c7b\u65b9\u5f0f",level:3},{value:"31.4 IFileProvider
\u5e38\u89c1\u64cd\u4f5c",id:"314-ifileprovider-\u5e38\u89c1\u64cd\u4f5c",level:2},{value:"31.4.1 \u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9",id:"3141-\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9",level:3},{value:"31.4.2 \u83b7\u53d6\u6587\u4ef6\u76ee\u5f55\u5185\u5bb9\uff08\u9700\u9012\u5f52\u67e5\u627e\uff09",id:"3142-\u83b7\u53d6\u6587\u4ef6\u76ee\u5f55\u5185\u5bb9\u9700\u9012\u5f52\u67e5\u627e",level:3},{value:"31.4.3 \u76d1\u542c\u6587\u4ef6\u53d8\u5316",id:"3143-\u76d1\u542c\u6587\u4ef6\u53d8\u5316",level:3},{value:"31.5 \u6a21\u5757\u5316\u9759\u6001\u8d44\u6e90\u914d\u7f6e",id:"315-\u6a21\u5757\u5316\u9759\u6001\u8d44\u6e90\u914d\u7f6e",level:2},{value:"31.6 \u6587\u4ef6\u4e0a\u4f20\u4e0b\u8f7d",id:"316-\u6587\u4ef6\u4e0a\u4f20\u4e0b\u8f7d",level:2},{value:"31.6.1 \u6587\u4ef6\u4e0b\u8f7d",id:"3161-\u6587\u4ef6\u4e0b\u8f7d",level:3},{value:"31.6.2 \u6587\u4ef6\u4e0a\u4f20",id:"3162-\u6587\u4ef6\u4e0a\u4f20",level:3},{value:"31.6.3 \u5c06 IFormFile
\u8f6c byte[]
",id:"3163-\u5c06-iformfile-\u8f6c-byte",level:3},{value:"31.6.4 \u5c06 byte[]
\u8f93\u51fa\u4e3a Url
\u5730\u5740",id:"3164-\u5c06-byte-\u8f93\u51fa\u4e3a-url-\u5730\u5740",level:3},{value:"31.6.5 \u914d\u7f6e\u4e0a\u4f20\u6587\u4ef6\u76ee\u5f55",id:"3165-\u914d\u7f6e\u4e0a\u4f20\u6587\u4ef6\u76ee\u5f55",level:3},{value:"31.7 \u8bf7\u6c42\u5927\u5c0f\u63a7\u5236\uff08\u4e0a\u4f20\u6587\u4ef6\u5927\u5c0f\u63a7\u5236\uff09",id:"317-\u8bf7\u6c42\u5927\u5c0f\u63a7\u5236\u4e0a\u4f20\u6587\u4ef6\u5927\u5c0f\u63a7\u5236",level:2},{value:"31.7.1 \u5bf9\u7279\u5b9a\u7684\u63a5\u53e3\u8fdb\u884c\u63a7\u5236",id:"3171-\u5bf9\u7279\u5b9a\u7684\u63a5\u53e3\u8fdb\u884c\u63a7\u5236",level:3},{value:"31.7.2 \u5bf9\u7279\u5b9a\u63a5\u53e3\u53d6\u6d88\u9650\u5236",id:"3172-\u5bf9\u7279\u5b9a\u63a5\u53e3\u53d6\u6d88\u9650\u5236",level:3},{value:"31.7.3 \u901a\u7528\u4e2d\u95f4\u4ef6\u8fdb\u884c\u63a7\u5236",id:"3173-\u901a\u7528\u4e2d\u95f4\u4ef6\u8fdb\u884c\u63a7\u5236",level:3},{value:"31.7.4 \u5168\u5c40\u914d\u7f6e",id:"3174-\u5168\u5c40\u914d\u7f6e",level:3},{value:"31.8 \u7279\u5b9a\u6587\u4ef6\u7c7b\u578b\uff08\u6587\u4ef6\u540e\u7f00\uff09\u5904\u7406",id:"318-\u7279\u5b9a\u6587\u4ef6\u7c7b\u578b\u6587\u4ef6\u540e\u7f00\u5904\u7406",level:2},{value:"31.9 \u53cd\u9988\u4e0e\u5efa\u8bae",id:"319-\u53cd\u9988\u4e0e\u5efa\u8bae",level:2}];function h(e){const n={a:"a",admonition:"admonition",code:"code",h2:"h2",h3:"h3",hr:"hr",li:"li",ol:"ol",p:"p",pre:"pre",strong:"strong",ul:"ul",...(0,i.a)(),...e.components},{Details:r}=n;return r||function(e,n){throw new Error("Expected "+(n?"component":"object")+" `"+e+"` to be defined: you likely forgot to import, pass, or provide it.")}("Details",!0),(0,s.jsxs)(s.Fragment,{children:[(0,s.jsxs)(r,{children:[(0,s.jsx)("summary",{children:"\ud83d\udcdd \u6a21\u5757\u66f4\u65b0\u65e5\u5fd7"}),(0,s.jsx)("div",{children:(0,s.jsx)("div",{children:(0,s.jsxs)(n.ul,{children:["\n",(0,s.jsxs)(n.li,{children:["\n",(0,s.jsx)(n.p,{children:(0,s.jsx)(n.strong,{children:"\u65b0\u7279\u6027"})}),"\n",(0,s.jsxs)(n.ul,{children:["\n",(0,s.jsxs)(n.li,{children:["\xa0",(0,s.jsx)(l.Z,{children:"\u65b0\u589e"})," ",(0,s.jsx)(n.code,{children:".m3u8"})," \u548c ",(0,s.jsx)(n.code,{children:".ts"})," \u6587\u4ef6\u7c7b\u578b ",(0,s.jsx)(n.code,{children:"MIME"})," \u652f\u6301 ",(0,s.jsx)("sup",{children:"4.8.7.5"})," ",(0,s.jsx)("sup",{children:"\u23f1\ufe0f2023.03.07"})," ",(0,s.jsx)(n.a,{href:"https://gitee.com/dotnetchina/Furion/issues/I6KKEM",children:"#I6KKEM"})]}),"\n",(0,s.jsxs)(n.li,{children:["\xa0",(0,s.jsx)(l.Z,{children:"\u65b0\u589e"})," ",(0,s.jsx)(n.code,{children:"*.bcmap"})," \u548c ",(0,s.jsx)(n.code,{children:".properties"})," \u6587\u4ef6\u7c7b\u578b ",(0,s.jsx)(n.code,{children:"MIME"})," \u652f\u6301 ",(0,s.jsx)("sup",{children:"4.8.4.9"})," ",(0,s.jsx)("sup",{children:"\u23f1\ufe0f2023.01.06"})," ",(0,s.jsx)(n.a,{href:"https://gitee.com/dotnetchina/Furion/pulls/694",children:"!694"})]}),"\n"]}),"\n"]}),"\n"]})})})]}),"\n",(0,s.jsx)(n.admonition,{title:"\u7248\u672c\u8bf4\u660e",type:"important",children:(0,s.jsxs)(n.p,{children:["\u4ee5\u4e0b\u5185\u5bb9\u4ec5\u9650 ",(0,s.jsx)(n.code,{children:"Furion 2.5.0 +"})," \u7248\u672c\u4f7f\u7528\u3002"]})}),"\n","\n","\n",(0,s.jsx)(n.h2,{id:"311-\u5173\u4e8e\u6587\u4ef6\u7cfb\u7edf",children:"31.1 \u5173\u4e8e\u6587\u4ef6\u7cfb\u7edf"}),"\n",(0,s.jsxs)(n.p,{children:["\u672c\u7ae0\u6240\u8c13\u7684 ",(0,s.jsx)(n.code,{children:"\u6587\u4ef6\u7cfb\u7edf"})," \u6709\u70b9\u540d\u4e0d\u526f\u5b9e\uff0c\u5176\u5b9e\u6839\u672c\u7b97\u4e0d\u4e0a\u4e00\u4e2a\u7cfb\u7edf\uff0c\u5b83\u4ec5\u4ec5\u662f\u5229\u7528\u4e00\u4e2a\u62bd\u8c61\u5316\u7684 ",(0,s.jsx)(n.code,{children:"IFileProvider"})," \u4ee5\u7edf\u4e00\u7684\u65b9\u5f0f\u63d0\u4f9b\u6240\u9700\u7684\u6587\u4ef6\u800c\u5df2\u3002\u901a\u8fc7\u8be5 ",(0,s.jsx)(n.code,{children:"\u6587\u4ef6\u7cfb\u7edf"})," \u53ef\u4ee5\u8bfb\u53d6\u7269\u7406\u6587\u4ef6\u548c\u5d4c\u5165\u8d44\u6e90\u6587\u4ef6\uff0c\u5305\u62ec\u76ee\u5f55\u7ed3\u679c\u8bfb\u53d6\uff0c\u6587\u4ef6\u5185\u5bb9\u8bfb\u53d6\uff0c\u6587\u4ef6\u5185\u5bb9\u76d1\u542c\u7b49\u7b49\u3002"]}),"\n",(0,s.jsx)(n.h3,{id:"3111-\u6587\u4ef6\u7cfb\u7edf\u7c7b\u578b",children:"31.1.1 \u6587\u4ef6\u7cfb\u7edf\u7c7b\u578b"}),"\n",(0,s.jsxs)(n.p,{children:[(0,s.jsx)(n.code,{children:"Furion"})," \u63d0\u4f9b\u4e86\u4e24\u79cd\u6587\u4ef6\u7cfb\u7edf\u7c7b\u578b\uff1a"]}),"\n",(0,s.jsxs)(n.ul,{children:["\n",(0,s.jsxs)(n.li,{children:[(0,s.jsx)(n.code,{children:"Physical"}),"\uff1a\u7269\u7406\u6587\u4ef6\u7cfb\u7edf\u7c7b\u578b\uff0c\u4e5f\u5c31\u662f\u7269\u7406\u673a\u4e2d\u5b9e\u9645\u5b58\u5728\u7684\u6587\u4ef6"]}),"\n",(0,s.jsxs)(n.li,{children:[(0,s.jsx)(n.code,{children:"Embedded"}),"\uff1a\u5d4c\u5165\u8d44\u6e90\u6587\u4ef6\u7cfb\u7edf\u7c7b\u578b\uff0c\u4e5f\u5c31\u662f\u8d44\u6e90\u6587\u4ef6\u5d4c\u5165\u5230\u4e86\u7a0b\u5e8f\u96c6\u4e2d\uff0c\u5e38\u7528\u4e8e\u6a21\u5757\u5316\u5f00\u53d1"]}),"\n"]}),"\n",(0,s.jsx)(n.h2,{id:"312-\u6ce8\u518c\u865a\u62df\u6587\u4ef6\u7cfb\u7edf\u670d\u52a1",children:"31.2 \u6ce8\u518c\u865a\u62df\u6587\u4ef6\u7cfb\u7edf\u670d\u52a1"}),"\n",(0,s.jsx)(n.pre,{children:(0,s.jsx)(n.code,{className:"language-cs",metastring:"showLineNumbers",children:"services.AddVirtualFileServer();\n"})}),"\n",(0,s.jsxs)(n.h2,{id:"313-\u83b7\u53d6\u6587\u4ef6\u7cfb\u7edf-ifileprovider-\u5b9e\u4f8b",children:["31.3 \u83b7\u53d6\u6587\u4ef6\u7cfb\u7edf ",(0,s.jsx)(n.code,{children:"IFileProvider"})," \u5b9e\u4f8b"]}),"\n",(0,s.jsxs)(n.h3,{id:"3131-funcfileprovidertypes-object-ifileprovider-\u65b9\u5f0f",children:["31.3.1 ",(0,s.jsx)(n.code,{children:"Func"})," \u65b9\u5f0f"]}),"\n",(0,s.jsxs)(n.p,{children:[(0,s.jsx)(n.code,{children:"Furion"})," \u6846\u67b6\u63d0\u4f9b\u4e86 ",(0,s.jsx)(n.code,{children:"Func"})," \u59d4\u6258\u4f9b\u6784\u9020\u51fd\u6570\u6ce8\u5165\u6216\u89e3\u6790\u670d\u52a1\uff0c\u5982\uff1a"]}),"\n",(0,s.jsx)(n.pre,{children:(0,s.jsx)(n.code,{className:"language-cs",metastring:"showLineNumbers {6,8-9,11-12}",children:'public class PersonServices\r\n{\r\n private readonly IFileProvider _physicalFileProvider;\r\n private readonly IFileProvider _embeddedFileProvider;\r\n\r\n public PersonServices(Func fileProviderResolve)\r\n {\r\n // \u89e3\u6790\u7269\u7406\u6587\u4ef6\u7cfb\u7edf\r\n _physicalFileProvider = fileProviderResolve(FileProviderTypes.Physical, @"c:/test");\r\n\r\n // \u89e3\u6790\u5d4c\u5165\u8d44\u6e90\u6587\u4ef6\u7cfb\u7edf\r\n _embeddedFileProvider = fileProviderResolve(FileProviderTypes.Embedded, Assembly.GetEntryAssembly());\r\n }\r\n}\n'})}),"\n",(0,s.jsxs)(n.h3,{id:"3132-fs-\u9759\u6001\u7c7b\u65b9\u5f0f",children:["31.3.2 ",(0,s.jsx)(n.code,{children:"FS"})," \u9759\u6001\u7c7b\u65b9\u5f0f"]}),"\n",(0,s.jsxs)(n.p,{children:[(0,s.jsx)(n.code,{children:"Furion"})," \u6846\u67b6\u4e5f\u63d0\u4f9b\u4e86 ",(0,s.jsx)(n.code,{children:"FS"})," \u9759\u6001\u7c7b\u65b9\u5f0f\u521b\u5efa\uff0c\u5982\uff1a"]}),"\n",(0,s.jsx)(n.pre,{children:(0,s.jsx)(n.code,{className:"language-cs",metastring:"showLineNumbers",children:'// \u89e3\u6790\u7269\u7406\u6587\u4ef6\u7cfb\u7edf\r\nvar physicalFileProvider = FS.GetPhysicalFileProvider(@"c:/test");\r\n\r\n// \u89e3\u6790\u5d4c\u5165\u8d44\u6e90\u6587\u4ef6\u7cfb\u7edf\r\nvar embeddedFileProvider = FS.GetEmbeddedFileProvider(Assembly.GetEntryAssembly());\n'})}),"\n",(0,s.jsxs)(n.h2,{id:"314-ifileprovider-\u5e38\u89c1\u64cd\u4f5c",children:["31.4 ",(0,s.jsx)(n.code,{children:"IFileProvider"})," \u5e38\u89c1\u64cd\u4f5c"]}),"\n",(0,s.jsx)(n.h3,{id:"3141-\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9",children:"31.4.1 \u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9"}),"\n",(0,s.jsx)(n.pre,{children:(0,s.jsx)(n.code,{className:"language-cs",metastring:"showLineNumbers",children:'byte[] buffer;\r\nusing (Stream readStream = _fileProvider.GetFileInfo("\u4f60\u7684\u6587\u4ef6\u8def\u5f84").CreateReadStream())\r\n{\r\n buffer = new byte[readStream.Length];\r\n await readStream.ReadAsync(buffer.AsMemory(0, buffer.Length));\r\n}\r\n\r\n// \u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\r\nvar content = Encoding.UTF8.GetString(buffer);\n'})}),"\n",(0,s.jsx)(n.h3,{id:"3142-\u83b7\u53d6\u6587\u4ef6\u76ee\u5f55\u5185\u5bb9\u9700\u9012\u5f52\u67e5\u627e",children:"31.4.2 \u83b7\u53d6\u6587\u4ef6\u76ee\u5f55\u5185\u5bb9\uff08\u9700\u9012\u5f52\u67e5\u627e\uff09"}),"\n",(0,s.jsx)(n.pre,{children:(0,s.jsx)(n.code,{className:"language-cs",metastring:"showLineNumbers",children:'var rootPath = "\u5f53\u524d\u76ee\u5f55\u8def\u5f84";\r\nvar fileinfos = _fileProvider.GetDirectoryContents(rootPath);\r\nforeach (var fileinfo in fileinfos)\r\n{\r\n if(fileinfo.IsDirectory)\r\n {\r\n // \u8fd9\u91cc\u9012\u5f52\u3002\u3002\u3002\r\n }\r\n}\n'})}),"\n",(0,s.jsx)(n.h3,{id:"3143-\u76d1\u542c\u6587\u4ef6\u53d8\u5316",children:"31.4.3 \u76d1\u542c\u6587\u4ef6\u53d8\u5316"}),"\n",(0,s.jsx)(n.pre,{children:(0,s.jsx)(n.code,{className:"language-cs",metastring:"showLineNumbers",children:'ChangeToken.OnChange(() => _fileProvider.Watch("\u76d1\u542c\u7684\u6587\u4ef6"), () =>\r\n{\r\n // \u8fd9\u91cc\u5199\u4f60\u7684\u903b\u8f91\r\n});\n'})}),"\n",(0,s.jsx)(n.h2,{id:"315-\u6a21\u5757\u5316\u9759\u6001\u8d44\u6e90\u914d\u7f6e",children:"31.5 \u6a21\u5757\u5316\u9759\u6001\u8d44\u6e90\u914d\u7f6e"}),"\n",(0,s.jsxs)(n.p,{children:["\u901a\u5e38\u6211\u4eec\u91c7\u7528\u6a21\u5757\u5316\u5f00\u53d1\uff0c\u9759\u6001\u8d44\u6e90\u90fd\u662f\u5d4c\u5165\u8fdb\u7a0b\u5e8f\u96c6\u4e2d\uff0c\u8fd9\u65f6\u5019\u6211\u4eec\u9700\u8981\u901a\u8fc7\u914d\u7f6e ",(0,s.jsx)(n.code,{children:"UseFileServer"})," \u6307\u5b9a\u6a21\u5757\u9759\u6001\u8d44\u6e90\u8def\u5f84\uff0c\u5982\uff1a"]}),"\n",(0,s.jsx)(n.pre,{children:(0,s.jsx)(n.code,{className:"language-cs",metastring:"showLineNumbers",children:'// \u9ed8\u8ba4\u9759\u6001\u8d44\u6e90\u8c03\u7528\uff0cwwwroot\r\napp.UseStaticFiles();\r\n\r\n// \u914d\u7f6e\u6a21\u5757\u5316\u9759\u6001\u8d44\u6e90\r\napp.UseFileServer(new FileServerOptions\r\n{\r\n FileProvider = new EmbeddedFileProvider(\u6a21\u5757\u7a0b\u5e8f\u96c6),\r\n RequestPath = "/\u6a21\u5757\u540d\u79f0", // \u540e\u7eed\u6240\u6709\u8d44\u6e90\u90fd\u662f\u901a\u8fc7 /\u6a21\u5757\u540d\u79f0/xxx.css \u8c03\u7528\r\n EnableDirectoryBrowsing = true\r\n});\n'})}),"\n",(0,s.jsx)(n.h2,{id:"316-\u6587\u4ef6\u4e0a\u4f20\u4e0b\u8f7d",children:"31.6 \u6587\u4ef6\u4e0a\u4f20\u4e0b\u8f7d"}),"\n",(0,s.jsx)(n.p,{children:"\u5728\u5e94\u7528\u5f00\u53d1\u4e2d\uff0c\u6587\u4ef6\u4e0a\u4f20\u4e0b\u8f7d\u5c5e\u4e8e\u975e\u5e38\u5e38\u7528\u7684\u529f\u80fd\uff0c\u8fd9\u91cc\u8d34\u51fa\u5e38\u89c1\u7684\u6587\u4ef6\u4e0a\u4f20\u4e0b\u8f7d\u793a\u4f8b\u3002"}),"\n",(0,s.jsx)(n.h3,{id:"3161-\u6587\u4ef6\u4e0b\u8f7d",children:"31.6.1 \u6587\u4ef6\u4e0b\u8f7d"}),"\n",(0,s.jsxs)(n.ul,{children:["\n",(0,s.jsx)(n.li,{children:"\u6587\u4ef6\u8def\u5f84\u7684\u65b9\u5f0f"}),"\n"]}),"\n",(0,s.jsx)(n.pre,{children:(0,s.jsx)(n.code,{className:"language-cs",metastring:"showLineNumbers {1,5}",children:'[HttpGet, NonUnify]\r\npublic IActionResult FileDownload()\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")\r\n {\r\n FileDownloadName = fileName // \u914d\u7f6e\u6587\u4ef6\u4e0b\u8f7d\u663e\u793a\u540d\r\n };\r\n}\n'})}),"\n",(0,s.jsxs)(n.ul,{children:["\n",(0,s.jsxs)(n.li,{children:[(0,s.jsx)(n.code,{children:"byte[]"})," \u65b9\u5f0f"]}),"\n"]}),"\n",(0,s.jsx)(n.pre,{children:(0,s.jsx)(n.code,{className:"language-cs",metastring:"showLineNumbers {1,4}",children:'[HttpGet, NonUnify]\r\npublic IActionResult FileDownload()\r\n{\r\n return new FileContentResult(byte\u6570\u7ec4, "application/octet-stream")\r\n {\r\n FileDownloadName = fileName // \u914d\u7f6e\u6587\u4ef6\u4e0b\u8f7d\u663e\u793a\u540d\r\n };\r\n}\n'})}),"\n",(0,s.jsxs)(n.ul,{children:["\n",(0,s.jsxs)(n.li,{children:[(0,s.jsx)(n.code,{children:"stream"})," \u65b9\u5f0f"]}),"\n"]}),"\n",(0,s.jsx)(n.pre,{children:(0,s.jsx)(n.code,{className:"language-cs",metastring:"showLineNumbers {1,4,7-9,11}",children:'[HttpGet, NonUnify]\r\npublic async Task FileDownload()\r\n{\r\n var (stream, _) = await "http://furion.baiqian.ltd/img/rm1.png".GetAsStreamAsync();\r\n\r\n // \u5c06 stream \u8f6c byte[]\r\n byte[] bytes = new byte[stream.Length];\r\n await stream.ReadAsync(bytes);\r\n stream.Seek(0, SeekOrigin.Begin);\r\n\r\n return new FileContentResult(bytes, "application/octet-stream")\r\n {\r\n FileDownloadName = fileName // \u914d\u7f6e\u6587\u4ef6\u4e0b\u8f7d\u663e\u793a\u540d\r\n };\r\n}\n'})}),"\n",(0,s.jsxs)(n.admonition,{title:"\u5173\u4e8e\u524d\u7aef\u83b7\u53d6\u6587\u4ef6\u540d",type:"note",children:[(0,s.jsx)(n.p,{children:"\u5982\u679c\u524d\u7aef\u83b7\u53d6\u4e0d\u5230\u6587\u4ef6\uff0c\u53ef\u6dfb\u52a0\u4ee5\u4e0b\u914d\u7f6e\uff1a"}),(0,s.jsx)(n.pre,{children:(0,s.jsx)(n.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'})}),(0,s.jsx)(n.p,{children:"\u5982\u679c\u4f9d\u7136\u4e0d\u80fd\u89e3\u51b3\u95ee\u9898\u53ef\u5c1d\u8bd5\u6dfb\u52a0\u4ee5\u4e0b\u914d\u7f6e\uff1a"}),(0,s.jsx)(n.pre,{children:(0,s.jsx)(n.code,{className:"language-json",metastring:"showLineNumbers {2,4-5}",children:'{\r\n "CorsAccessorSettings": {\r\n "WithExposedHeaders": [\r\n "Content-Disposition",\r\n "Access-Control-Expose-Headersx-access-token"\r\n ]\r\n }\r\n}\n'})})]}),"\n",(0,s.jsx)(n.h3,{id:"3162-\u6587\u4ef6\u4e0a\u4f20",children:"31.6.2 \u6587\u4ef6\u4e0a\u4f20"}),"\n",(0,s.jsx)(n.admonition,{title:"\u5c0f\u63d0\u9192",type:"tip",children:(0,s.jsxs)(n.p,{children:[(0,s.jsx)(n.code,{children:"IFormFile"})," \u7c7b\u578b\u5bf9\u5e94\u524d\u7aef\u7684 ",(0,s.jsx)(n.code,{children:"Content-Type"})," \u4e3a\uff1a ",(0,s.jsx)(n.code,{children:"multipart/form-data"})]})}),"\n",(0,s.jsxs)(n.ul,{children:["\n",(0,s.jsx)(n.li,{children:(0,s.jsxs)(n.strong,{children:["\u5355\u6587\u4ef6 ",(0,s.jsx)(n.code,{children:"IFormFile"})," \u7c7b\u578b\u53c2\u6570\uff08\u5b58\u50a8\u5230\u786c\u76d8\uff09"]})}),"\n"]}),"\n",(0,s.jsx)(n.pre,{children:(0,s.jsx)(n.code,{className:"language-cs",metastring:"showLineNumbers {1,2,18}",children:'[HttpPost]\r\npublic async Task UploadFileAsync(IFormFile file)\r\n{\r\n // \u5982\uff1a\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 //// \u8fd9\u91cc\u8fd8\u53ef\u4ee5\u83b7\u53d6\u6587\u4ef6\u7684\u4fe1\u606f\r\n // var size = file.Length / 1024.0; // \u6587\u4ef6\u5927\u5c0f KB\r\n // var clientFileName = file.FileName; // \u5ba2\u6237\u7aef\u4e0a\u4f20\u7684\u6587\u4ef6\u540d\r\n // var contentType = file.ContentType; // \u83b7\u53d6\u6587\u4ef6 ContentType \u6216\u89e3\u6790 MIME \u7c7b\u578b\r\n\r\n // \u907f\u514d\u6587\u4ef6\u540d\u91cd\u590d\uff0c\u91c7\u7528 GUID \u751f\u6210\r\n var fileName = Guid.NewGuid().ToString("N") + Path.GetExtension(file.FileName);\r\n var filePath = Path.Combine(savePath, fileName);\r\n\r\n // \u4fdd\u5b58\u5230\u6307\u5b9a\u8def\u5f84\r\n using (var stream = System.IO.File.Create(filePath))\r\n {\r\n await file.CopyToAsync(stream);\r\n }\r\n\r\n // \u8fd4\u56de\u6587\u4ef6\u540d\uff08\u8fd9\u91cc\u53ef\u4ee5\u81ea\u7531\u8fd4\u56de\u66f4\u591a\u4fe1\u606f\uff09\r\n return fileName;\r\n}\n'})}),"\n",(0,s.jsxs)(n.ul,{children:["\n",(0,s.jsx)(n.li,{children:(0,s.jsxs)(n.strong,{children:["\u5355\u6587\u4ef6 ",(0,s.jsx)(n.code,{children:"Base64"})," \u7c7b\u578b\u53c2\u6570\uff08\u5b58\u50a8\u5230\u786c\u76d8\uff09"]})}),"\n"]}),"\n",(0,s.jsx)(n.pre,{children:(0,s.jsx)(n.code,{className:"language-cs",metastring:"showLineNumbers {1,2,9,19}",children:'[HttpPost]\r\npublic async Task UploadFileAsync([FromBody] string fileBase64, string clientFileName)\r\n{\r\n // \u5982\uff1a\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 // \u5c06 base64 \u5b57\u7b26\u4e32\u8f6c byte[]\r\n var bytes = Convert.FromBase64String(fileBase64);\r\n\r\n // \u8fd9\u91cc\u8fd8\u53ef\u4ee5\u83b7\u53d6\u6587\u4ef6\u7684\u4fe1\u606f\r\n // var size = bytes.Length / 1024.0; // \u6587\u4ef6\u5927\u5c0f KB\r\n\r\n // \u907f\u514d\u6587\u4ef6\u540d\u91cd\u590d\uff0c\u91c7\u7528 GUID \u751f\u6210\r\n var fileName = Guid.NewGuid().ToString("N") + Path.GetExtension(clientFileName);\r\n var filePath = Path.Combine(savePath, fileName);\r\n\r\n // \u4fdd\u5b58\u5230\u6307\u5b9a\u8def\u5f84\r\n using (var fs = new FileStream(filePath, FileMode.Create))\r\n {\r\n await fs.WriteAsync(bytes);\r\n }\r\n\r\n // \u8fd4\u56de\u6587\u4ef6\u540d\uff08\u8fd9\u91cc\u53ef\u4ee5\u81ea\u7531\u8fd4\u56de\u66f4\u591a\u4fe1\u606f\uff09\r\n return filename;\r\n}\n'})}),"\n",(0,s.jsx)(n.admonition,{title:"\u7279\u522b\u6ce8\u610f",type:"warning",children:(0,s.jsxs)(n.p,{children:["\u6587\u4ef6 ",(0,s.jsx)(n.code,{children:"Base64"})," \u5b57\u7b26\u4e32\u5982\u679c\u5e26 ",(0,s.jsx)(n.code,{children:"data:text/plain;base64,"})," \u5f00\u5934\u5219\uff0c\u9700\u8981\u624b\u52a8\u53bb\u6389 ",(0,s.jsx)(n.code,{children:","})," \u4e4b\u524d\uff08\u542b\u9017\u53f7\uff09\u7684\u5b57\u7b26\u4e32\u3002"]})}),"\n",(0,s.jsxs)(n.ul,{children:["\n",(0,s.jsx)(n.li,{children:(0,s.jsxs)(n.strong,{children:["\u591a\u6587\u4ef6 ",(0,s.jsx)(n.code,{children:"List"})," \u7c7b\u578b\u53c2\u6570\uff08\u5b58\u50a8\u5230\u786c\u76d8\uff09"]})}),"\n"]}),"\n",(0,s.jsx)(n.admonition,{title:"\u53c2\u6570\u63d0\u793a",type:"tip",children:(0,s.jsxs)(n.p,{children:["\u901a\u5e38\u591a\u6587\u4ef6\u4e0a\u4f20\u7528\u7684\u6700\u591a\u7684\u662f ",(0,s.jsx)(n.code,{children:"List files"})," \u53c2\u6570\uff0c\u4f46 ",(0,s.jsx)(n.code,{children:".NET5+"})," \u66f4\u63a8\u8350\u4f7f\u7528 ",(0,s.jsx)(n.code,{children:"IFormFileCollection files"}),"\u3002"]})}),"\n",(0,s.jsxs)(n.p,{children:["\u4ee3\u7801\u548c ",(0,s.jsx)(n.code,{children:"\u5355\u6587\u4ef6\u5904\u7406\u4e00\u81f4"}),"\uff0c\u53ea\u9700 ",(0,s.jsx)(n.code,{children:"foreach"})," \u5373\u53ef\u3002"]}),"\n",(0,s.jsx)(n.pre,{children:(0,s.jsx)(n.code,{className:"language-cs",metastring:"showLineNumbers {1-2,12}",children:'[HttpPost]\r\npublic async Task