经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » C# » 查看文章
MAUI Blazor 如何通过url使用本地文件 - Yu-Core
来源:cnblogs  作者:Yu-Core  时间:2023/11/29 16:17:43  对本文有异议

前言

上一篇文章 MAUI Blazor 显示本地图片的新思路 中, 提出了通过webview拦截,从而在前端中显示本地图片的思路。不过当时还不完善,随后也发现了很多问题。比如,

  1. 不同平台上的url不统一。这对于需要存储图片路径并且多端互通的需求来说,并不友好。至少 FileSystem.AppDataDirectoryFileSystem.CacheDirectory 下的文件生成的url应该统一。
  2. 音频文件和视频文件无法使用。理论上可以用于各种文件,但是音频和视频不能播放,应该是需要相应的处理
  3. Windows上有限制。大于9~10M的图片不显示
  4. iOS/ Mac有跨域问题。尤其是调用用于截图的js库,图片会由于跨域不出现在截图中

所以,在这篇文章中,对这个思路进行完善,使之成为一个可行的方案。

例如 <img src='appdata/Image/image1.jpg' > 会显示 FileSystem.AppDataDirectory 文件夹下的 Image 文件夹下的 image1.jpg 这个图片
<video src='cache/Video/video1.mp4' controls > 会播放 FileSystem.CacheDirectory 文件夹下的 Video 文件夹下的 video1.mp4 这个视频
对于其他路径的文件来说,url设为 file/ 加上转义后的完整路径

正文

准备工作

新建一个MAUI Blazor项目

参考 配置基于文件名的多目标 ,更改项目文件(以.csproj结尾的文件),添加以下代码

  1. <!-- Android -->
  2. <ItemGroup Condition="$(TargetFramework.StartsWith('net8.0-android')) != true">
  3. <Compile Remove="**\**\*.Android.cs" />
  4. <None Include="**\**\*.Android.cs" Exclude="$(DefaultItemExcludes);$(DefaultExcludesInProjectFolder)" />
  5. </ItemGroup>
  6. <!-- Both iOS and Mac Catalyst -->
  7. <ItemGroup Condition="$(TargetFramework.StartsWith('net8.0-ios')) != true AND $(TargetFramework.StartsWith('net8.0-maccatalyst')) != true">
  8. <Compile Remove="**\**\*.MaciOS.cs" />
  9. <None Include="**\**\*.MaciOS.cs" Exclude="$(DefaultItemExcludes);$(DefaultExcludesInProjectFolder)" />
  10. </ItemGroup>
  11. <!-- iOS -->
  12. <ItemGroup Condition="$(TargetFramework.StartsWith('net8.0-ios')) != true">
  13. <Compile Remove="**\**\*.iOS.cs" />
  14. <None Include="**\**\*.iOS.cs" Exclude="$(DefaultItemExcludes);$(DefaultExcludesInProjectFolder)" />
  15. </ItemGroup>
  16. <!-- Mac Catalyst -->
  17. <ItemGroup Condition="$(TargetFramework.StartsWith('net8.0-maccatalyst')) != true">
  18. <Compile Remove="**\**\*.MacCatalyst.cs" />
  19. <None Include="**\**\*.MacCatalyst.cs" Exclude="$(DefaultItemExcludes);$(DefaultExcludesInProjectFolder)" />
  20. </ItemGroup>
  21. <!-- Windows -->
  22. <ItemGroup Condition="$(TargetFramework.Contains('-windows')) != true">
  23. <Compile Remove="**\*.Windows.cs" />
  24. <None Include="**\*.Windows.cs" Exclude="$(DefaultItemExcludes);$(DefaultExcludesInProjectFolder)" />
  25. </ItemGroup>

添加一个处理ContentType的静态类

用来获取文件的ContentType
没找到什么太好的方法,偶然看到Maui的源码中的一段,还不错,不过是internal修饰的,就直接抄来了
新建Utilities/MimeType文件夹,在里面添加 StaticContentProvider.cs
代码如下:

  1. #nullable disable
  2. // Copyright (c) .NET Foundation. All rights reserved.
  3. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
  4. using System.Diagnostics.CodeAnalysis;
  5. namespace MauiBlazorLocalMediaFile.Utilities
  6. {
  7. internal partial class StaticContentProvider
  8. {
  9. private static readonly FileExtensionContentTypeProvider ContentTypeProvider = new();
  10. internal static string GetResponseContentTypeOrDefault(string path)
  11. => ContentTypeProvider.TryGetContentType(path, out var matchedContentType)
  12. ? matchedContentType
  13. : "application/octet-stream";
  14. internal static IDictionary<string, string> GetResponseHeaders(string contentType)
  15. => new Dictionary<string, string>(StringComparer.Ordinal)
  16. {
  17. { "Content-Type", contentType },
  18. { "Cache-Control", "no-cache, max-age=0, must-revalidate, no-store" },
  19. };
  20. internal class FileExtensionContentTypeProvider
  21. {
  22. // Notes:
  23. // - This table was initially copied from IIS and has many legacy entries we will maintain for backwards compatibility.
  24. // - We only plan to add new entries where we expect them to be applicable to a majority of developers such as being
  25. // used in the project templates.
  26. #region Extension mapping table
  27. /// <summary>
  28. /// Creates a new provider with a set of default mappings.
  29. /// </summary>
  30. public FileExtensionContentTypeProvider()
  31. : this(new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase)
  32. {
  33. { ".323", "text/h323" },
  34. { ".3g2", "video/3gpp2" },
  35. { ".3gp2", "video/3gpp2" },
  36. { ".3gp", "video/3gpp" },
  37. { ".3gpp", "video/3gpp" },
  38. { ".aac", "audio/aac" },
  39. { ".aaf", "application/octet-stream" },
  40. { ".aca", "application/octet-stream" },
  41. { ".accdb", "application/msaccess" },
  42. { ".accde", "application/msaccess" },
  43. { ".accdt", "application/msaccess" },
  44. { ".acx", "application/internet-property-stream" },
  45. { ".adt", "audio/vnd.dlna.adts" },
  46. { ".adts", "audio/vnd.dlna.adts" },
  47. { ".afm", "application/octet-stream" },
  48. { ".ai", "application/postscript" },
  49. { ".aif", "audio/x-aiff" },
  50. { ".aifc", "audio/aiff" },
  51. { ".aiff", "audio/aiff" },
  52. { ".appcache", "text/cache-manifest" },
  53. { ".application", "application/x-ms-application" },
  54. { ".art", "image/x-jg" },
  55. { ".asd", "application/octet-stream" },
  56. { ".asf", "video/x-ms-asf" },
  57. { ".asi", "application/octet-stream" },
  58. { ".asm", "text/plain" },
  59. { ".asr", "video/x-ms-asf" },
  60. { ".asx", "video/x-ms-asf" },
  61. { ".atom", "application/atom+xml" },
  62. { ".au", "audio/basic" },
  63. { ".avi", "video/x-msvideo" },
  64. { ".axs", "application/olescript" },
  65. { ".bas", "text/plain" },
  66. { ".bcpio", "application/x-bcpio" },
  67. { ".bin", "application/octet-stream" },
  68. { ".bmp", "image/bmp" },
  69. { ".c", "text/plain" },
  70. { ".cab", "application/vnd.ms-cab-compressed" },
  71. { ".calx", "application/vnd.ms-office.calx" },
  72. { ".cat", "application/vnd.ms-pki.seccat" },
  73. { ".cdf", "application/x-cdf" },
  74. { ".chm", "application/octet-stream" },
  75. { ".class", "application/x-java-applet" },
  76. { ".clp", "application/x-msclip" },
  77. { ".cmx", "image/x-cmx" },
  78. { ".cnf", "text/plain" },
  79. { ".cod", "image/cis-cod" },
  80. { ".cpio", "application/x-cpio" },
  81. { ".cpp", "text/plain" },
  82. { ".crd", "application/x-mscardfile" },
  83. { ".crl", "application/pkix-crl" },
  84. { ".crt", "application/x-x509-ca-cert" },
  85. { ".csh", "application/x-csh" },
  86. { ".css", "text/css" },
  87. { ".csv", "text/csv" }, // https://tools.ietf.org/html/rfc7111#section-5.1
  88. { ".cur", "application/octet-stream" },
  89. { ".dcr", "application/x-director" },
  90. { ".deploy", "application/octet-stream" },
  91. { ".der", "application/x-x509-ca-cert" },
  92. { ".dib", "image/bmp" },
  93. { ".dir", "application/x-director" },
  94. { ".disco", "text/xml" },
  95. { ".dlm", "text/dlm" },
  96. { ".doc", "application/msword" },
  97. { ".docm", "application/vnd.ms-word.document.macroEnabled.12" },
  98. { ".docx", "application/vnd.openxmlformats-officedocument.wordprocessingml.document" },
  99. { ".dot", "application/msword" },
  100. { ".dotm", "application/vnd.ms-word.template.macroEnabled.12" },
  101. { ".dotx", "application/vnd.openxmlformats-officedocument.wordprocessingml.template" },
  102. { ".dsp", "application/octet-stream" },
  103. { ".dtd", "text/xml" },
  104. { ".dvi", "application/x-dvi" },
  105. { ".dvr-ms", "video/x-ms-dvr" },
  106. { ".dwf", "drawing/x-dwf" },
  107. { ".dwp", "application/octet-stream" },
  108. { ".dxr", "application/x-director" },
  109. { ".eml", "message/rfc822" },
  110. { ".emz", "application/octet-stream" },
  111. { ".eot", "application/vnd.ms-fontobject" },
  112. { ".eps", "application/postscript" },
  113. { ".etx", "text/x-setext" },
  114. { ".evy", "application/envoy" },
  115. { ".exe", "application/vnd.microsoft.portable-executable" }, // https://www.iana.org/assignments/media-types/application/vnd.microsoft.portable-executable
  116. { ".fdf", "application/vnd.fdf" },
  117. { ".fif", "application/fractals" },
  118. { ".fla", "application/octet-stream" },
  119. { ".flr", "x-world/x-vrml" },
  120. { ".flv", "video/x-flv" },
  121. { ".gif", "image/gif" },
  122. { ".gtar", "application/x-gtar" },
  123. { ".gz", "application/x-gzip" },
  124. { ".h", "text/plain" },
  125. { ".hdf", "application/x-hdf" },
  126. { ".hdml", "text/x-hdml" },
  127. { ".hhc", "application/x-oleobject" },
  128. { ".hhk", "application/octet-stream" },
  129. { ".hhp", "application/octet-stream" },
  130. { ".hlp", "application/winhlp" },
  131. { ".hqx", "application/mac-binhex40" },
  132. { ".hta", "application/hta" },
  133. { ".htc", "text/x-component" },
  134. { ".htm", "text/html" },
  135. { ".html", "text/html" },
  136. { ".htt", "text/webviewhtml" },
  137. { ".hxt", "text/html" },
  138. { ".ical", "text/calendar" },
  139. { ".icalendar", "text/calendar" },
  140. { ".ico", "image/x-icon" },
  141. { ".ics", "text/calendar" },
  142. { ".ief", "image/ief" },
  143. { ".ifb", "text/calendar" },
  144. { ".iii", "application/x-iphone" },
  145. { ".inf", "application/octet-stream" },
  146. { ".ins", "application/x-internet-signup" },
  147. { ".isp", "application/x-internet-signup" },
  148. { ".IVF", "video/x-ivf" },
  149. { ".jar", "application/java-archive" },
  150. { ".java", "application/octet-stream" },
  151. { ".jck", "application/liquidmotion" },
  152. { ".jcz", "application/liquidmotion" },
  153. { ".jfif", "image/pjpeg" },
  154. { ".jpb", "application/octet-stream" },
  155. { ".jpe", "image/jpeg" },
  156. { ".jpeg", "image/jpeg" },
  157. { ".jpg", "image/jpeg" },
  158. { ".js", "application/javascript" },
  159. { ".json", "application/json" },
  160. { ".jsx", "text/jscript" },
  161. { ".latex", "application/x-latex" },
  162. { ".lit", "application/x-ms-reader" },
  163. { ".lpk", "application/octet-stream" },
  164. { ".lsf", "video/x-la-asf" },
  165. { ".lsx", "video/x-la-asf" },
  166. { ".lzh", "application/octet-stream" },
  167. { ".m13", "application/x-msmediaview" },
  168. { ".m14", "application/x-msmediaview" },
  169. { ".m1v", "video/mpeg" },
  170. { ".m2ts", "video/vnd.dlna.mpeg-tts" },
  171. { ".m3u", "audio/x-mpegurl" },
  172. { ".m4a", "audio/mp4" },
  173. { ".m4v", "video/mp4" },
  174. { ".man", "application/x-troff-man" },
  175. { ".manifest", "application/x-ms-manifest" },
  176. { ".map", "text/plain" },
  177. { ".markdown", "text/markdown" },
  178. { ".md", "text/markdown" },
  179. { ".mdb", "application/x-msaccess" },
  180. { ".mdp", "application/octet-stream" },
  181. { ".me", "application/x-troff-me" },
  182. { ".mht", "message/rfc822" },
  183. { ".mhtml", "message/rfc822" },
  184. { ".mid", "audio/mid" },
  185. { ".midi", "audio/mid" },
  186. { ".mix", "application/octet-stream" },
  187. { ".mmf", "application/x-smaf" },
  188. { ".mno", "text/xml" },
  189. { ".mny", "application/x-msmoney" },
  190. { ".mov", "video/quicktime" },
  191. { ".movie", "video/x-sgi-movie" },
  192. { ".mp2", "video/mpeg" },
  193. { ".mp3", "audio/mpeg" },
  194. { ".mp4", "video/mp4" },
  195. { ".mp4v", "video/mp4" },
  196. { ".mpa", "video/mpeg" },
  197. { ".mpe", "video/mpeg" },
  198. { ".mpeg", "video/mpeg" },
  199. { ".mpg", "video/mpeg" },
  200. { ".mpp", "application/vnd.ms-project" },
  201. { ".mpv2", "video/mpeg" },
  202. { ".ms", "application/x-troff-ms" },
  203. { ".msi", "application/octet-stream" },
  204. { ".mso", "application/octet-stream" },
  205. { ".mvb", "application/x-msmediaview" },
  206. { ".mvc", "application/x-miva-compiled" },
  207. { ".nc", "application/x-netcdf" },
  208. { ".nsc", "video/x-ms-asf" },
  209. { ".nws", "message/rfc822" },
  210. { ".ocx", "application/octet-stream" },
  211. { ".oda", "application/oda" },
  212. { ".odc", "text/x-ms-odc" },
  213. { ".ods", "application/oleobject" },
  214. { ".oga", "audio/ogg" },
  215. { ".ogg", "video/ogg" },
  216. { ".ogv", "video/ogg" },
  217. { ".ogx", "application/ogg" },
  218. { ".one", "application/onenote" },
  219. { ".onea", "application/onenote" },
  220. { ".onetoc", "application/onenote" },
  221. { ".onetoc2", "application/onenote" },
  222. { ".onetmp", "application/onenote" },
  223. { ".onepkg", "application/onenote" },
  224. { ".osdx", "application/opensearchdescription+xml" },
  225. { ".otf", "font/otf" },
  226. { ".p10", "application/pkcs10" },
  227. { ".p12", "application/x-pkcs12" },
  228. { ".p7b", "application/x-pkcs7-certificates" },
  229. { ".p7c", "application/pkcs7-mime" },
  230. { ".p7m", "application/pkcs7-mime" },
  231. { ".p7r", "application/x-pkcs7-certreqresp" },
  232. { ".p7s", "application/pkcs7-signature" },
  233. { ".pbm", "image/x-portable-bitmap" },
  234. { ".pcx", "application/octet-stream" },
  235. { ".pcz", "application/octet-stream" },
  236. { ".pdf", "application/pdf" },
  237. { ".pfb", "application/octet-stream" },
  238. { ".pfm", "application/octet-stream" },
  239. { ".pfx", "application/x-pkcs12" },
  240. { ".pgm", "image/x-portable-graymap" },
  241. { ".pko", "application/vnd.ms-pki.pko" },
  242. { ".pma", "application/x-perfmon" },
  243. { ".pmc", "application/x-perfmon" },
  244. { ".pml", "application/x-perfmon" },
  245. { ".pmr", "application/x-perfmon" },
  246. { ".pmw", "application/x-perfmon" },
  247. { ".png", "image/png" },
  248. { ".pnm", "image/x-portable-anymap" },
  249. { ".pnz", "image/png" },
  250. { ".pot", "application/vnd.ms-powerpoint" },
  251. { ".potm", "application/vnd.ms-powerpoint.template.macroEnabled.12" },
  252. { ".potx", "application/vnd.openxmlformats-officedocument.presentationml.template" },
  253. { ".ppam", "application/vnd.ms-powerpoint.addin.macroEnabled.12" },
  254. { ".ppm", "image/x-portable-pixmap" },
  255. { ".pps", "application/vnd.ms-powerpoint" },
  256. { ".ppsm", "application/vnd.ms-powerpoint.slideshow.macroEnabled.12" },
  257. { ".ppsx", "application/vnd.openxmlformats-officedocument.presentationml.slideshow" },
  258. { ".ppt", "application/vnd.ms-powerpoint" },
  259. { ".pptm", "application/vnd.ms-powerpoint.presentation.macroEnabled.12" },
  260. { ".pptx", "application/vnd.openxmlformats-officedocument.presentationml.presentation" },
  261. { ".prf", "application/pics-rules" },
  262. { ".prm", "application/octet-stream" },
  263. { ".prx", "application/octet-stream" },
  264. { ".ps", "application/postscript" },
  265. { ".psd", "application/octet-stream" },
  266. { ".psm", "application/octet-stream" },
  267. { ".psp", "application/octet-stream" },
  268. { ".pub", "application/x-mspublisher" },
  269. { ".qt", "video/quicktime" },
  270. { ".qtl", "application/x-quicktimeplayer" },
  271. { ".qxd", "application/octet-stream" },
  272. { ".ra", "audio/x-pn-realaudio" },
  273. { ".ram", "audio/x-pn-realaudio" },
  274. { ".rar", "application/octet-stream" },
  275. { ".ras", "image/x-cmu-raster" },
  276. { ".rf", "image/vnd.rn-realflash" },
  277. { ".rgb", "image/x-rgb" },
  278. { ".rm", "application/vnd.rn-realmedia" },
  279. { ".rmi", "audio/mid" },
  280. { ".roff", "application/x-troff" },
  281. { ".rpm", "audio/x-pn-realaudio-plugin" },
  282. { ".rtf", "application/rtf" },
  283. { ".rtx", "text/richtext" },
  284. { ".scd", "application/x-msschedule" },
  285. { ".sct", "text/scriptlet" },
  286. { ".sea", "application/octet-stream" },
  287. { ".setpay", "application/set-payment-initiation" },
  288. { ".setreg", "application/set-registration-initiation" },
  289. { ".sgml", "text/sgml" },
  290. { ".sh", "application/x-sh" },
  291. { ".shar", "application/x-shar" },
  292. { ".sit", "application/x-stuffit" },
  293. { ".sldm", "application/vnd.ms-powerpoint.slide.macroEnabled.12" },
  294. { ".sldx", "application/vnd.openxmlformats-officedocument.presentationml.slide" },
  295. { ".smd", "audio/x-smd" },
  296. { ".smi", "application/octet-stream" },
  297. { ".smx", "audio/x-smd" },
  298. { ".smz", "audio/x-smd" },
  299. { ".snd", "audio/basic" },
  300. { ".snp", "application/octet-stream" },
  301. { ".spc", "application/x-pkcs7-certificates" },
  302. { ".spl", "application/futuresplash" },
  303. { ".spx", "audio/ogg" },
  304. { ".src", "application/x-wais-source" },
  305. { ".ssm", "application/streamingmedia" },
  306. { ".sst", "application/vnd.ms-pki.certstore" },
  307. { ".stl", "application/vnd.ms-pki.stl" },
  308. { ".sv4cpio", "application/x-sv4cpio" },
  309. { ".sv4crc", "application/x-sv4crc" },
  310. { ".svg", "image/svg+xml" },
  311. { ".svgz", "image/svg+xml" },
  312. { ".swf", "application/x-shockwave-flash" },
  313. { ".t", "application/x-troff" },
  314. { ".tar", "application/x-tar" },
  315. { ".tcl", "application/x-tcl" },
  316. { ".tex", "application/x-tex" },
  317. { ".texi", "application/x-texinfo" },
  318. { ".texinfo", "application/x-texinfo" },
  319. { ".tgz", "application/x-compressed" },
  320. { ".thmx", "application/vnd.ms-officetheme" },
  321. { ".thn", "application/octet-stream" },
  322. { ".tif", "image/tiff" },
  323. { ".tiff", "image/tiff" },
  324. { ".toc", "application/octet-stream" },
  325. { ".tr", "application/x-troff" },
  326. { ".trm", "application/x-msterminal" },
  327. { ".ts", "video/vnd.dlna.mpeg-tts" },
  328. { ".tsv", "text/tab-separated-values" },
  329. { ".ttc", "application/x-font-ttf" },
  330. { ".ttf", "application/x-font-ttf" },
  331. { ".tts", "video/vnd.dlna.mpeg-tts" },
  332. { ".txt", "text/plain" },
  333. { ".u32", "application/octet-stream" },
  334. { ".uls", "text/iuls" },
  335. { ".ustar", "application/x-ustar" },
  336. { ".vbs", "text/vbscript" },
  337. { ".vcf", "text/x-vcard" },
  338. { ".vcs", "text/plain" },
  339. { ".vdx", "application/vnd.ms-visio.viewer" },
  340. { ".vml", "text/xml" },
  341. { ".vsd", "application/vnd.visio" },
  342. { ".vss", "application/vnd.visio" },
  343. { ".vst", "application/vnd.visio" },
  344. { ".vsto", "application/x-ms-vsto" },
  345. { ".vsw", "application/vnd.visio" },
  346. { ".vsx", "application/vnd.visio" },
  347. { ".vtx", "application/vnd.visio" },
  348. { ".wasm", "application/wasm" },
  349. { ".wav", "audio/wav" },
  350. { ".wax", "audio/x-ms-wax" },
  351. { ".wbmp", "image/vnd.wap.wbmp" },
  352. { ".wcm", "application/vnd.ms-works" },
  353. { ".wdb", "application/vnd.ms-works" },
  354. { ".webm", "video/webm" },
  355. { ".webmanifest", "application/manifest+json" }, // https://w3c.github.io/manifest/#media-type-registration
  356. { ".webp", "image/webp" },
  357. { ".wks", "application/vnd.ms-works" },
  358. { ".wm", "video/x-ms-wm" },
  359. { ".wma", "audio/x-ms-wma" },
  360. { ".wmd", "application/x-ms-wmd" },
  361. { ".wmf", "application/x-msmetafile" },
  362. { ".wml", "text/vnd.wap.wml" },
  363. { ".wmlc", "application/vnd.wap.wmlc" },
  364. { ".wmls", "text/vnd.wap.wmlscript" },
  365. { ".wmlsc", "application/vnd.wap.wmlscriptc" },
  366. { ".wmp", "video/x-ms-wmp" },
  367. { ".wmv", "video/x-ms-wmv" },
  368. { ".wmx", "video/x-ms-wmx" },
  369. { ".wmz", "application/x-ms-wmz" },
  370. { ".woff", "application/font-woff" }, // https://www.w3.org/TR/WOFF/#appendix-b
  371. { ".woff2", "font/woff2" }, // https://www.w3.org/TR/WOFF2/#IMT
  372. { ".wps", "application/vnd.ms-works" },
  373. { ".wri", "application/x-mswrite" },
  374. { ".wrl", "x-world/x-vrml" },
  375. { ".wrz", "x-world/x-vrml" },
  376. { ".wsdl", "text/xml" },
  377. { ".wtv", "video/x-ms-wtv" },
  378. { ".wvx", "video/x-ms-wvx" },
  379. { ".x", "application/directx" },
  380. { ".xaf", "x-world/x-vrml" },
  381. { ".xaml", "application/xaml+xml" },
  382. { ".xap", "application/x-silverlight-app" },
  383. { ".xbap", "application/x-ms-xbap" },
  384. { ".xbm", "image/x-xbitmap" },
  385. { ".xdr", "text/plain" },
  386. { ".xht", "application/xhtml+xml" },
  387. { ".xhtml", "application/xhtml+xml" },
  388. { ".xla", "application/vnd.ms-excel" },
  389. { ".xlam", "application/vnd.ms-excel.addin.macroEnabled.12" },
  390. { ".xlc", "application/vnd.ms-excel" },
  391. { ".xlm", "application/vnd.ms-excel" },
  392. { ".xls", "application/vnd.ms-excel" },
  393. { ".xlsb", "application/vnd.ms-excel.sheet.binary.macroEnabled.12" },
  394. { ".xlsm", "application/vnd.ms-excel.sheet.macroEnabled.12" },
  395. { ".xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" },
  396. { ".xlt", "application/vnd.ms-excel" },
  397. { ".xltm", "application/vnd.ms-excel.template.macroEnabled.12" },
  398. { ".xltx", "application/vnd.openxmlformats-officedocument.spreadsheetml.template" },
  399. { ".xlw", "application/vnd.ms-excel" },
  400. { ".xml", "text/xml" },
  401. { ".xof", "x-world/x-vrml" },
  402. { ".xpm", "image/x-xpixmap" },
  403. { ".xps", "application/vnd.ms-xpsdocument" },
  404. { ".xsd", "text/xml" },
  405. { ".xsf", "text/xml" },
  406. { ".xsl", "text/xml" },
  407. { ".xslt", "text/xml" },
  408. { ".xsn", "application/octet-stream" },
  409. { ".xtp", "application/octet-stream" },
  410. { ".xwd", "image/x-xwindowdump" },
  411. { ".z", "application/x-compress" },
  412. { ".zip", "application/x-zip-compressed" },
  413. })
  414. {
  415. }
  416. #endregion
  417. /// <summary>
  418. /// Creates a lookup engine using the provided mapping.
  419. /// It is recommended that the IDictionary instance use StringComparer.OrdinalIgnoreCase.
  420. /// </summary>
  421. /// <param name="mapping"></param>
  422. public FileExtensionContentTypeProvider(IDictionary<string, string> mapping)
  423. {
  424. if (mapping == null)
  425. {
  426. throw new ArgumentNullException(nameof(mapping));
  427. }
  428. Mappings = mapping;
  429. }
  430. /// <summary>
  431. /// The cross reference table of file extensions and content-types.
  432. /// </summary>
  433. public IDictionary<string, string> Mappings { get; private set; }
  434. /// <summary>
  435. /// Given a file path, determine the MIME type
  436. /// </summary>
  437. /// <param name="subpath">A file path</param>
  438. /// <param name="contentType">The resulting MIME type</param>
  439. /// <returns>True if MIME type could be determined</returns>
  440. public bool TryGetContentType(string subpath, [MaybeNullWhen(false)] out string contentType)
  441. {
  442. var extension = GetExtension(subpath);
  443. if (extension == null)
  444. {
  445. contentType = null;
  446. return false;
  447. }
  448. return Mappings.TryGetValue(extension, out contentType);
  449. }
  450. private static string GetExtension(string path)
  451. {
  452. // Don't use Path.GetExtension as that may throw an exception if there are
  453. // invalid characters in the path. Invalid characters should be handled
  454. // by the FileProviders
  455. if (string.IsNullOrWhiteSpace(path))
  456. {
  457. return null;
  458. }
  459. int index = path.LastIndexOf('.');
  460. if (index < 0)
  461. {
  462. return null;
  463. }
  464. return path.Substring(index);
  465. }
  466. }
  467. }
  468. }

创建自定义的BlazorWebViewHandler类

BlazorWebViewHandler是MAUI Blazor中处理BlazorWebView相关的一个类,我们自定义一个类替换它,添加自己需要的一些处理逻辑

Maui Blazor中iOS / Mac和其他平台的baseUrl是不统一的,iOS / Mac是 app://0.0.0.0 (原因在Maui源码的注释中有写到,iOS WKWebView doesn't allow handling 'http'/'https' schemes, so we use the fake 'app' scheme),其他平台是 https://0.0.0.0 ,所以我们的url设为相对路径才能统一,而且与页面同源,不会有跨域问题(笔者之前在iOS / Mac上的做法就是注册自定义协议,使用html2canvas截图时,结果发生了跨域问题)。

添加MauiBlazorWebViewHandler.cs
代码如下

  1. using Microsoft.AspNetCore.Components.WebView.Maui;
  2. namespace MauiBlazorLocalMediaFile
  3. {
  4. public partial class MauiBlazorWebViewHandler : BlazorWebViewHandler
  5. {
  6. private const string AppHostAddress = "0.0.0.0";
  7. #if IOS || MACCATALYST
  8. public const string BaseUri = $"app://{AppHostAddress}/";
  9. #else
  10. public const string BaseUri = $"https://{AppHostAddress}/";
  11. #endif
  12. public readonly static Dictionary<string, string> AppFilePathMap = new()
  13. {
  14. { FileSystem.AppDataDirectory, "appdata" },
  15. { FileSystem.CacheDirectory, "cache" },
  16. };
  17. private static readonly string OtherFileMapPath = "file";
  18. //把真实的文件路径转化为url相对路径
  19. public static string FilePathToUrlRelativePath(string filePath)
  20. {
  21. foreach (var item in AppFilePathMap)
  22. {
  23. if (filePath.StartsWith(item.Key))
  24. {
  25. return item.Value + filePath[item.Key.Length..].Replace(Path.DirectorySeparatorChar, '/');
  26. }
  27. }
  28. return OtherFileMapPath + "/" + Uri.EscapeDataString(filePath);
  29. }
  30. //把url相对路径转化为真实的文件路径
  31. public static string UrlRelativePathToFilePath(string urlRelativePath)
  32. {
  33. UrlRelativePathToFilePath(urlRelativePath, out string path);
  34. return path;
  35. }
  36. private static bool Intercept(string uri, out string path)
  37. {
  38. if (!uri.StartsWith(BaseUri))
  39. {
  40. path = string.Empty;
  41. return false;
  42. }
  43. var urlRelativePath = uri[BaseUri.Length..];
  44. return UrlRelativePathToFilePath(urlRelativePath, out path);
  45. }
  46. private static bool UrlRelativePathToFilePath(string urlRelativePath, out string path)
  47. {
  48. if (string.IsNullOrEmpty(urlRelativePath))
  49. {
  50. path = string.Empty;
  51. return false;
  52. }
  53. urlRelativePath = Uri.UnescapeDataString(urlRelativePath);
  54. foreach (var item in AppFilePathMap)
  55. {
  56. if (urlRelativePath.StartsWith(item.Value + '/'))
  57. {
  58. string urlRelativePathSub = urlRelativePath[(item.Value.Length + 1)..];
  59. path = Path.Combine(item.Key, urlRelativePathSub.Replace('/', Path.DirectorySeparatorChar));
  60. if (File.Exists(path))
  61. {
  62. return true;
  63. }
  64. }
  65. }
  66. if (urlRelativePath.StartsWith(OtherFileMapPath + '/'))
  67. {
  68. string urlRelativePathSub = urlRelativePath[(OtherFileMapPath.Length + 1)..];
  69. path = urlRelativePathSub.Replace('/', Path.DirectorySeparatorChar);
  70. if (File.Exists(path))
  71. {
  72. return true;
  73. }
  74. }
  75. path = string.Empty;
  76. return false;
  77. }
  78. }
  79. }
Android

添加MauiBlazorWebViewHandler.Android.cs
代码如下

  1. using Android.Webkit;
  2. using MauiBlazorLocalMediaFile.Utilities;
  3. using WebView = Android.Webkit.WebView;
  4. namespace MauiBlazorLocalMediaFile
  5. {
  6. public partial class MauiBlazorWebViewHandler
  7. {
  8. #pragma warning disable CA1416 // 验证平台兼容性
  9. protected override void ConnectHandler(WebView platformView)
  10. {
  11. base.ConnectHandler(platformView);
  12. platformView.SetWebViewClient(new MyWebViewClient(platformView.WebViewClient));
  13. }
  14. #nullable disable
  15. private class MyWebViewClient : WebViewClient
  16. {
  17. private WebViewClient WebViewClient { get; }
  18. public MyWebViewClient(WebViewClient webViewClient)
  19. {
  20. WebViewClient = webViewClient;
  21. }
  22. public override bool ShouldOverrideUrlLoading(Android.Webkit.WebView view, IWebResourceRequest request)
  23. {
  24. return WebViewClient.ShouldOverrideUrlLoading(view, request);
  25. }
  26. public override WebResourceResponse ShouldInterceptRequest(Android.Webkit.WebView view, IWebResourceRequest request)
  27. {
  28. var intercept = InterceptCustomPathRequest(request, out WebResourceResponse webResourceResponse);
  29. if (intercept)
  30. {
  31. return webResourceResponse;
  32. }
  33. return WebViewClient.ShouldInterceptRequest(view, request);
  34. }
  35. public override void OnPageFinished(Android.Webkit.WebView view, string url)
  36. => WebViewClient.OnPageFinished(view, url);
  37. protected override void Dispose(bool disposing)
  38. {
  39. if (!disposing)
  40. return;
  41. WebViewClient.Dispose();
  42. }
  43. private static bool InterceptCustomPathRequest(IWebResourceRequest request, out WebResourceResponse webResourceResponse)
  44. {
  45. webResourceResponse = null;
  46. var uri = request.Url.ToString();
  47. if (!Intercept(uri, out string path))
  48. {
  49. return false;
  50. }
  51. if (!File.Exists(path))
  52. {
  53. return false;
  54. }
  55. webResourceResponse = CreateWebResourceResponse(request, path);
  56. return true;
  57. }
  58. private static WebResourceResponse CreateWebResourceResponse(IWebResourceRequest request, string path)
  59. {
  60. string contentType = StaticContentProvider.GetResponseContentTypeOrDefault(path);
  61. var headers = StaticContentProvider.GetResponseHeaders(contentType);
  62. FileStream stream = File.OpenRead(path);
  63. var length = stream.Length;
  64. long rangeStart = 0;
  65. long rangeEnd = length - 1;
  66. string encoding = "UTF-8";
  67. int stateCode = 200;
  68. string reasonPhrase = "OK";
  69. //适用于音频视频文件资源的响应
  70. bool partial = request.RequestHeaders.TryGetValue("Range", out string rangeString);
  71. if (partial)
  72. {
  73. //206,可断点续传
  74. stateCode = 206;
  75. reasonPhrase = "Partial Content";
  76. var ranges = rangeString.Split('=');
  77. if (ranges.Length > 1 && !string.IsNullOrEmpty(ranges[1]))
  78. {
  79. string[] rangeDatas = ranges[1].Split("-");
  80. rangeStart = Convert.ToInt64(rangeDatas[0]);
  81. if (rangeDatas.Length > 1 && !string.IsNullOrEmpty(rangeDatas[1]))
  82. {
  83. rangeEnd = Convert.ToInt64(rangeDatas[1]);
  84. }
  85. }
  86. headers.Add("Accept-Ranges", "bytes");
  87. headers.Add("Content-Range", $"bytes {rangeStart}-{rangeEnd}/{length}");
  88. }
  89. //这一行删去似乎也不影响
  90. headers.Add("Content-Length", (rangeEnd - rangeStart + 1).ToString());
  91. var response = new WebResourceResponse(contentType, encoding, stateCode, reasonPhrase, headers, stream);
  92. return response;
  93. }
  94. }
  95. }
  96. }
iOS / Mac

iOS / Mac中我们要替换Maui对于app://自定义协议的注册,但是很多类、方法、属性、字段是不公开的,也就是internal和private,所以我们的代码用了很多反射。

添加 MauiBlazorWebViewHandler.MaciOS.cs
代码如下

  1. using Foundation;
  2. using Microsoft.AspNetCore.Components.WebView;
  3. using Microsoft.AspNetCore.Components.WebView.Maui;
  4. using Microsoft.Extensions.Logging;
  5. using MauiBlazorLocalMediaFile.Utilities;
  6. using System.Globalization;
  7. using System.Reflection;
  8. using System.Runtime.Versioning;
  9. using UIKit;
  10. using WebKit;
  11. using RectangleF = CoreGraphics.CGRect;
  12. namespace MauiBlazorLocalMediaFile
  13. {
  14. #nullable disable
  15. public partial class MauiBlazorWebViewHandler
  16. {
  17. private BlazorWebViewHandlerReflection _base;
  18. private BlazorWebViewHandlerReflection Base => _base ??= new(this);
  19. [SupportedOSPlatform("ios11.0")]
  20. protected override WKWebView CreatePlatformView()
  21. {
  22. Base.LoggerCreatingWebKitWKWebView();
  23. var config = new WKWebViewConfiguration();
  24. // By default, setting inline media playback to allowed, including autoplay
  25. // and picture in picture, since these things MUST be set during the webview
  26. // creation, and have no effect if set afterwards.
  27. // A custom handler factory delegate could be set to disable these defaults
  28. // but if we do not set them here, they cannot be changed once the
  29. // handler's platform view is created, so erring on the side of wanting this
  30. // capability by default.
  31. if (OperatingSystem.IsMacCatalystVersionAtLeast(10) || OperatingSystem.IsIOSVersionAtLeast(10))
  32. {
  33. config.AllowsPictureInPictureMediaPlayback = true;
  34. config.AllowsInlineMediaPlayback = true;
  35. config.MediaTypesRequiringUserActionForPlayback = WKAudiovisualMediaTypes.None;
  36. }
  37. VirtualView.BlazorWebViewInitializing(new BlazorWebViewInitializingEventArgs()
  38. {
  39. Configuration = config
  40. });
  41. // Legacy Developer Extras setting.
  42. config.Preferences.SetValueForKey(NSObject.FromObject(Base.DeveloperToolsEnabled), new NSString("developerExtrasEnabled"));
  43. config.UserContentController.AddScriptMessageHandler(Base.CreateWebViewScriptMessageHandler(), "webwindowinterop");
  44. config.UserContentController.AddUserScript(new WKUserScript(
  45. new NSString(Base.BlazorInitScript), WKUserScriptInjectionTime.AtDocumentEnd, true));
  46. // iOS WKWebView doesn't allow handling 'http'/'https' schemes, so we use the fake 'app' scheme
  47. config.SetUrlSchemeHandler(new SchemeHandler(this), urlScheme: "app");
  48. var webview = new WKWebView(RectangleF.Empty, config)
  49. {
  50. BackgroundColor = UIColor.Clear,
  51. AutosizesSubviews = true
  52. };
  53. if (OperatingSystem.IsIOSVersionAtLeast(16, 4) || OperatingSystem.IsMacCatalystVersionAtLeast(13, 3))
  54. {
  55. // Enable Developer Extras for Catalyst/iOS builds for 16.4+
  56. webview.SetValueForKey(NSObject.FromObject(Base.DeveloperToolsEnabled), new NSString("inspectable"));
  57. }
  58. VirtualView.BlazorWebViewInitialized(Base.CreateBlazorWebViewInitializedEventArgs(webview));
  59. Base.LoggerCreatedWebKitWKWebView();
  60. return webview;
  61. }
  62. private class SchemeHandler : NSObject, IWKUrlSchemeHandler
  63. {
  64. private readonly MauiBlazorWebViewHandler _webViewHandler;
  65. public SchemeHandler(MauiBlazorWebViewHandler webViewHandler)
  66. {
  67. _webViewHandler = webViewHandler;
  68. }
  69. [Export("webView:startURLSchemeTask:")]
  70. [SupportedOSPlatform("ios11.0")]
  71. public void StartUrlSchemeTask(WKWebView webView, IWKUrlSchemeTask urlSchemeTask)
  72. {
  73. var intercept = InterceptCustomPathRequest(urlSchemeTask);
  74. if (intercept)
  75. {
  76. return;
  77. }
  78. var responseBytes = GetResponseBytes(urlSchemeTask.Request.Url?.AbsoluteString ?? "", out var contentType, statusCode: out var statusCode);
  79. if (statusCode == 200)
  80. {
  81. using (var dic = new NSMutableDictionary<NSString, NSString>())
  82. {
  83. dic.Add((NSString)"Content-Length", (NSString)(responseBytes.Length.ToString(CultureInfo.InvariantCulture)));
  84. dic.Add((NSString)"Content-Type", (NSString)contentType);
  85. // Disable local caching. This will prevent user scripts from executing correctly.
  86. dic.Add((NSString)"Cache-Control", (NSString)"no-cache, max-age=0, must-revalidate, no-store");
  87. if (urlSchemeTask.Request.Url != null)
  88. {
  89. using var response = new NSHttpUrlResponse(urlSchemeTask.Request.Url, statusCode, "HTTP/1.1", dic);
  90. urlSchemeTask.DidReceiveResponse(response);
  91. }
  92. }
  93. urlSchemeTask.DidReceiveData(NSData.FromArray(responseBytes));
  94. urlSchemeTask.DidFinish();
  95. }
  96. }
  97. private byte[] GetResponseBytes(string? url, out string contentType, out int statusCode)
  98. {
  99. var allowFallbackOnHostPage = _webViewHandler.Base.IsBaseOfPage(_webViewHandler.Base.AppOriginUri, url);
  100. url = _webViewHandler.Base.QueryStringHelperRemovePossibleQueryString(url);
  101. _webViewHandler.Base.LoggerHandlingWebRequest(url);
  102. if (_webViewHandler.Base.TryGetResponseContentInternal(url, allowFallbackOnHostPage, out statusCode, out var statusMessage, out var content, out var headers))
  103. {
  104. statusCode = 200;
  105. using var ms = new MemoryStream();
  106. content.CopyTo(ms);
  107. content.Dispose();
  108. contentType = headers["Content-Type"];
  109. _webViewHandler?.Base.LoggerResponseContentBeingSent(url, statusCode);
  110. return ms.ToArray();
  111. }
  112. else
  113. {
  114. _webViewHandler?.Base.LoggerReponseContentNotFound(url);
  115. statusCode = 404;
  116. contentType = string.Empty;
  117. return Array.Empty<byte>();
  118. }
  119. }
  120. [Export("webView:stopURLSchemeTask:")]
  121. public void StopUrlSchemeTask(WKWebView webView, IWKUrlSchemeTask urlSchemeTask)
  122. {
  123. }
  124. private static bool InterceptCustomPathRequest(IWKUrlSchemeTask urlSchemeTask)
  125. {
  126. var uri = urlSchemeTask.Request.Url.ToString();
  127. if (uri == null)
  128. {
  129. return false;
  130. }
  131. if (!Intercept(uri, out string path))
  132. {
  133. return false;
  134. }
  135. if (!File.Exists(path))
  136. {
  137. return false;
  138. }
  139. long length = new FileInfo(path).Length;
  140. string contentType = StaticContentProvider.GetResponseContentTypeOrDefault(path);
  141. using (var dic = new NSMutableDictionary<NSString, NSString>())
  142. {
  143. dic.Add((NSString)"Content-Length", (NSString)(length.ToString(CultureInfo.InvariantCulture)));
  144. dic.Add((NSString)"Content-Type", (NSString)contentType);
  145. // Disable local caching. This will prevent user scripts from executing correctly.
  146. dic.Add((NSString)"Cache-Control", (NSString)"no-cache, max-age=0, must-revalidate, no-store");
  147. using var response = new NSHttpUrlResponse(urlSchemeTask.Request.Url, 200, "HTTP/1.1", dic);
  148. urlSchemeTask.DidReceiveResponse(response);
  149. }
  150. urlSchemeTask.DidReceiveData(NSData.FromFile(path));
  151. urlSchemeTask.DidFinish();
  152. return true;
  153. }
  154. }
  155. }
  156. public class BlazorWebViewHandlerReflection
  157. {
  158. public BlazorWebViewHandlerReflection(BlazorWebViewHandler blazorWebViewHandler)
  159. {
  160. _blazorWebViewHandler = blazorWebViewHandler;
  161. _logger = new(() =>
  162. {
  163. var property = Type.GetProperty("Logger", BindingFlags.NonPublic | BindingFlags.Instance);
  164. return (ILogger)property?.GetValue(_blazorWebViewHandler);
  165. });
  166. _blazorInitScript = new(() =>
  167. {
  168. var property = Type.GetField("BlazorInitScript", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static);
  169. return (string)property?.GetValue(_blazorWebViewHandler);
  170. });
  171. _appOriginUri = new(() =>
  172. {
  173. var property = Type.GetField("AppOriginUri", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static);
  174. return (Uri)property?.GetValue(_blazorWebViewHandler);
  175. });
  176. }
  177. private readonly BlazorWebViewHandler _blazorWebViewHandler;
  178. private static readonly Type Type = typeof(BlazorWebViewHandler);
  179. private static readonly Assembly Assembly = Type.Assembly;
  180. private static readonly Type TypeLog = Assembly.GetType("Microsoft.AspNetCore.Components.WebView.Log")!;
  181. private readonly Lazy<ILogger> _logger;
  182. private readonly Lazy<string> _blazorInitScript;
  183. private readonly Lazy<Uri> _appOriginUri;
  184. private object WebviewManager;
  185. private MethodInfo MethodTryGetResponseContentInternal;
  186. private MethodInfo MethodIsBaseOfPage;
  187. private MethodInfo MethodQueryStringHelperRemovePossibleQueryString;
  188. public ILogger Logger => _logger.Value;
  189. public string BlazorInitScript => _blazorInitScript.Value;
  190. public Uri AppOriginUri => _appOriginUri.Value;
  191. public bool DeveloperToolsEnabled => GetDeveloperToolsEnabled();
  192. public void LoggerCreatingWebKitWKWebView()
  193. {
  194. var method = TypeLog.GetMethod("CreatingWebKitWKWebView");
  195. method?.Invoke(null, new object[] { Logger });
  196. }
  197. public void LoggerCreatedWebKitWKWebView()
  198. {
  199. var method = TypeLog.GetMethod("CreatedWebKitWKWebView");
  200. method?.Invoke(null, new object[] { Logger });
  201. }
  202. public void LoggerHandlingWebRequest(string url)
  203. {
  204. var method = TypeLog.GetMethod("HandlingWebRequest");
  205. method?.Invoke(null, new object[] { Logger, url });
  206. }
  207. public void LoggerResponseContentBeingSent(string url, int statusCode)
  208. {
  209. var method = TypeLog.GetMethod("ResponseContentBeingSent");
  210. method?.Invoke(null, new object[] { Logger, url, statusCode });
  211. }
  212. public void LoggerReponseContentNotFound(string url)
  213. {
  214. var method = TypeLog.GetMethod("ReponseContentNotFound");
  215. method?.Invoke(null, new object[] { Logger, url });
  216. }
  217. private bool GetDeveloperToolsEnabled()
  218. {
  219. var PropertyDeveloperTools = Type.GetProperty("DeveloperTools", BindingFlags.NonPublic | BindingFlags.Instance);
  220. var DeveloperTools = PropertyDeveloperTools.GetValue(_blazorWebViewHandler);
  221. var type = DeveloperTools.GetType();
  222. var Enabled = type.GetProperty("Enabled", BindingFlags.Public | BindingFlags.Instance);
  223. return (bool)Enabled?.GetValue(DeveloperTools);
  224. }
  225. public IWKScriptMessageHandler CreateWebViewScriptMessageHandler()
  226. {
  227. Type webViewScriptMessageHandlerType = Type.GetNestedType("WebViewScriptMessageHandler", BindingFlags.NonPublic);
  228. if (webViewScriptMessageHandlerType != null)
  229. {
  230. // 获取 MessageReceived 方法信息
  231. MethodInfo messageReceivedMethod = Type.GetMethod("MessageReceived", BindingFlags.Instance | BindingFlags.NonPublic);
  232. if (messageReceivedMethod != null)
  233. {
  234. // 创建 WebViewScriptMessageHandler 实例
  235. object webViewScriptMessageHandlerInstance = Activator.CreateInstance(webViewScriptMessageHandlerType, new object[] { Delegate.CreateDelegate(typeof(Action<Uri, string>), _blazorWebViewHandler, messageReceivedMethod) });
  236. return (IWKScriptMessageHandler)webViewScriptMessageHandlerInstance;
  237. }
  238. }
  239. return null;
  240. }
  241. public BlazorWebViewInitializedEventArgs CreateBlazorWebViewInitializedEventArgs(WKWebView wKWebView)
  242. {
  243. var blazorWebViewInitializedEventArgs = new BlazorWebViewInitializedEventArgs();
  244. PropertyInfo property = typeof(BlazorWebViewInitializedEventArgs).GetProperty("WebView", BindingFlags.Public | BindingFlags.Instance);
  245. property.SetValue(blazorWebViewInitializedEventArgs, wKWebView);
  246. return blazorWebViewInitializedEventArgs;
  247. }
  248. public bool TryGetResponseContentInternal(string uri, bool allowFallbackOnHostPage, out int statusCode, out string statusMessage, out Stream content, out IDictionary<string, string> headers)
  249. {
  250. if (MethodTryGetResponseContentInternal == null)
  251. {
  252. var Field_webviewManager = Type.GetField("_webviewManager", BindingFlags.NonPublic | BindingFlags.Instance);
  253. WebviewManager = Field_webviewManager.GetValue(_blazorWebViewHandler);
  254. MethodTryGetResponseContentInternal = WebviewManager.GetType().GetMethod("TryGetResponseContentInternal", BindingFlags.NonPublic | BindingFlags.Instance);
  255. }
  256. // 定义参数
  257. object[] parameters = new object[] { uri, allowFallbackOnHostPage, 0, null, null, null };
  258. bool result = (bool)MethodTryGetResponseContentInternal.Invoke(WebviewManager, parameters);
  259. // 获取返回值和输出参数
  260. statusCode = (int)parameters[2];
  261. statusMessage = (string)parameters[3];
  262. content = (Stream)parameters[4];
  263. headers = (IDictionary<string, string>)parameters[5];
  264. return result;
  265. }
  266. public bool IsBaseOfPage(Uri baseUri, string? uriString)
  267. {
  268. if (MethodIsBaseOfPage == null)
  269. {
  270. var type = Assembly.GetType("Microsoft.AspNetCore.Components.WebView.Maui.UriExtensions")!;
  271. MethodIsBaseOfPage = type.GetMethod("IsBaseOfPage", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static);
  272. }
  273. return (bool)MethodIsBaseOfPage.Invoke(null, new object[] { baseUri, uriString });
  274. }
  275. public string QueryStringHelperRemovePossibleQueryString(string? url)
  276. {
  277. if (MethodQueryStringHelperRemovePossibleQueryString == null)
  278. {
  279. var type = Assembly.GetType("Microsoft.AspNetCore.Components.WebView.QueryStringHelper")!;
  280. MethodQueryStringHelperRemovePossibleQueryString = type.GetMethod("RemovePossibleQueryString", BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance);
  281. }
  282. return (string)MethodQueryStringHelperRemovePossibleQueryString.Invoke(null, new object[] { url });
  283. }
  284. }
  285. }
Windows

添加MauiBlazorWebViewHandler.Windows.cs
代码如下

  1. using MauiBlazorLocalMediaFile.Utilities;
  2. using Microsoft.Web.WebView2.Core;
  3. using System.Runtime.InteropServices.WindowsRuntime;
  4. using Windows.Storage.Streams;
  5. using WebView2Control = Microsoft.UI.Xaml.Controls.WebView2;
  6. namespace MauiBlazorLocalMediaFile
  7. {
  8. public partial class MauiBlazorWebViewHandler
  9. {
  10. protected override void ConnectHandler(WebView2Control platformView)
  11. {
  12. base.ConnectHandler(platformView);
  13. platformView.CoreWebView2Initialized += CoreWebView2Initialized;
  14. }
  15. protected override void DisconnectHandler(WebView2Control platformView)
  16. {
  17. platformView.CoreWebView2Initialized -= CoreWebView2Initialized;
  18. base.DisconnectHandler(platformView);
  19. }
  20. private void CoreWebView2Initialized(WebView2Control sender, Microsoft.UI.Xaml.Controls.CoreWebView2InitializedEventArgs args)
  21. {
  22. var webview2 = sender.CoreWebView2;
  23. webview2.WebResourceRequested += WebView2WebResourceRequested;
  24. }
  25. async void WebView2WebResourceRequested(CoreWebView2 webview2, CoreWebView2WebResourceRequestedEventArgs args)
  26. {
  27. await InterceptCustomPathRequest(webview2, args);
  28. }
  29. static async Task<bool> InterceptCustomPathRequest(CoreWebView2 webview2, CoreWebView2WebResourceRequestedEventArgs args)
  30. {
  31. string uri = args.Request.Uri;
  32. if (!Intercept(uri, out string filePath))
  33. {
  34. return false;
  35. }
  36. if (File.Exists(filePath))
  37. {
  38. args.Response = await CreateWebResourceResponse(webview2, args, filePath);
  39. }
  40. else
  41. {
  42. args.Response = webview2.Environment.CreateWebResourceResponse(null, 404, "Not Found", string.Empty);
  43. }
  44. return true;
  45. static string GetHeaderString(IDictionary<string, string> headers) =>
  46. string.Join(Environment.NewLine, headers.Select(kvp => $"{kvp.Key}: {kvp.Value}"));
  47. static async Task<CoreWebView2WebResourceResponse> CreateWebResourceResponse(CoreWebView2 webview2, CoreWebView2WebResourceRequestedEventArgs args, string filePath)
  48. {
  49. var contentType = StaticContentProvider.GetResponseContentTypeOrDefault(filePath);
  50. var headers = StaticContentProvider.GetResponseHeaders(contentType);
  51. using var contentStream = File.OpenRead(filePath);
  52. var length = contentStream.Length;
  53. long rangeStart = 0;
  54. long rangeEnd = length - 1;
  55. int statusCode = 200;
  56. string reasonPhrase = "OK";
  57. //适用于音频视频文件资源的响应
  58. bool partial = args.Request.Headers.Contains("Range");
  59. if (partial)
  60. {
  61. statusCode = 206;
  62. reasonPhrase = "Partial Content";
  63. var rangeString = args.Request.Headers.GetHeader("Range");
  64. var ranges = rangeString.Split('=');
  65. if (ranges.Length > 1 && !string.IsNullOrEmpty(ranges[1]))
  66. {
  67. string[] rangeDatas = ranges[1].Split("-");
  68. rangeStart = Convert.ToInt64(rangeDatas[0]);
  69. if (rangeDatas.Length > 1 && !string.IsNullOrEmpty(rangeDatas[1]))
  70. {
  71. rangeEnd = Convert.ToInt64(rangeDatas[1]);
  72. }
  73. else
  74. {
  75. //每次加载4Mb,不能设置太多
  76. rangeEnd = Math.Min(rangeEnd, rangeStart + 4 * 1024 * 1024);
  77. }
  78. }
  79. headers.Add("Accept-Ranges", "bytes");
  80. headers.Add("Content-Range", $"bytes {rangeStart}-{rangeEnd}/{length}");
  81. }
  82. headers.Add("Content-Length", (rangeEnd - rangeStart + 1).ToString());
  83. var headerString = GetHeaderString(headers);
  84. IRandomAccessStream stream = await ReadStreamRange(contentStream, rangeStart, rangeEnd);
  85. return webview2.Environment.CreateWebResourceResponse(stream, statusCode, reasonPhrase, headerString);
  86. }
  87. static async Task<IRandomAccessStream> ReadStreamRange(Stream contentStream, long start, long end)
  88. {
  89. long length = end - start + 1;
  90. contentStream.Position = start;
  91. using var memoryStream = new MemoryStream();
  92. StreamCopy(contentStream, memoryStream, length);
  93. // 将内存流的位置重置为起始位置
  94. memoryStream.Seek(0, SeekOrigin.Begin);
  95. var randomAccessStream = new InMemoryRandomAccessStream();
  96. await randomAccessStream.WriteAsync(memoryStream.GetWindowsRuntimeBuffer());
  97. return randomAccessStream;
  98. }
  99. // 辅助方法,用于限制StreamCopy复制的数据长度
  100. static void StreamCopy(Stream source, Stream destination, long length)
  101. {
  102. //缓冲区设为1Mb,应该是够了
  103. byte[] buffer = new byte[1024 * 1024];
  104. int bytesRead;
  105. while (length > 0 && (bytesRead = source.Read(buffer, 0, (int)Math.Min(buffer.Length, length))) > 0)
  106. {
  107. destination.Write(buffer, 0, bytesRead);
  108. length -= bytesRead;
  109. }
  110. }
  111. }
  112. }
  113. }

在MauiProgram.cs中添加

添加在builder.Services.AddMauiBlazorWebView();的下面

  1. builder.Services.ConfigureMauiHandlers(delegate (IMauiHandlersCollection handlers)
  2. {
  3. handlers.AddHandler<IBlazorWebView>((IServiceProvider _) => new MauiBlazorWebViewHandler());
  4. });

试验一下

添加一个复制文件的静态类

简单写一个方法,用于把选中的文件复制到指定目录,并且返回所需要的url相对路径
为了防止文件被重复复制,我们以md5作为文件名

添加文件夹Utilities/File,在里面添加一个静态类MediaResourceFile.cs
代码如下

  1. using System.Security.Cryptography;
  2. namespace MauiBlazorLocalMediaFile.Utilities
  3. {
  4. public static class MediaResourceFile
  5. {
  6. public static async Task<string?> CreateMediaResourceFileAsync(string targetDirectoryPath, string? sourceFilePath)
  7. {
  8. if (string.IsNullOrEmpty(sourceFilePath))
  9. {
  10. return null;
  11. }
  12. using Stream stream = File.OpenRead(sourceFilePath);
  13. //新的文件以文件的md5为文件名,确保文件不会重复存在
  14. //获取文件的md5有一点耗时,暂时没想到更好的方案
  15. var fn = stream.CreateMD5() + Path.GetExtension(sourceFilePath);
  16. var targetFilePath = Path.Combine(targetDirectoryPath, fn);
  17. //如果文件存在就不用复制了
  18. if (!File.Exists(targetFilePath))
  19. {
  20. if (sourceFilePath.StartsWith(FileSystem.CacheDirectory))
  21. {
  22. stream.Close();
  23. await FileMoveAsync(sourceFilePath, targetFilePath);
  24. }
  25. else
  26. {
  27. //将流的位置重置为起始位置
  28. stream.Seek(0, SeekOrigin.Begin);
  29. await FileCopyAsync(targetFilePath, stream);
  30. }
  31. }
  32. return MauiBlazorWebViewHandler.FilePathToUrlRelativePath(targetFilePath);
  33. }
  34. private static async Task FileCopyAsync(string targetFilePath, Stream sourceStream)
  35. {
  36. CreateFileDirectory(targetFilePath);
  37. using (FileStream localFileStream = File.OpenWrite(targetFilePath))
  38. {
  39. await sourceStream.CopyToAsync(localFileStream, 1024 * 1024);
  40. };
  41. }
  42. private static Task FileMoveAsync(string sourceFilePath, string targetFilePath)
  43. {
  44. CreateFileDirectory(targetFilePath);
  45. File.Move(sourceFilePath, targetFilePath);
  46. return Task.CompletedTask;
  47. }
  48. private static void CreateFileDirectory(string filePath)
  49. {
  50. string? directoryPath = Path.GetDirectoryName(filePath);
  51. if (!Directory.Exists(directoryPath))
  52. {
  53. Directory.CreateDirectory(directoryPath!);
  54. }
  55. }
  56. private static string CreateMD5(this Stream stream, int bufferSize = 1024 * 1024)
  57. {
  58. using MD5 md5 = MD5.Create();
  59. byte[] buffer = new byte[bufferSize];
  60. int bytesRead;
  61. while ((bytesRead = stream.Read(buffer, 0, buffer.Length)) > 0)
  62. {
  63. md5.TransformBlock(buffer, 0, bytesRead, buffer, 0);
  64. }
  65. md5.TransformFinalBlock(buffer, 0, 0);
  66. byte[] hash = md5.Hash ?? [];
  67. return BitConverter.ToString(hash).Replace("-", "").ToLower();
  68. }
  69. }
  70. }
写一个选中视频文件并显示的示例页面
  1. @page "/video"
  2. @using MauiBlazorLocalMediaFile.Utilities
  3. <h1>Video</h1>
  4. <video src="@Src" controls style="max-width:100%;"></video>
  5. <div style="word-break: break-all;">Src="@Src"</div>
  6. <button class="btn btn-primary" @onclick="()=>Pick(true)">选中并复制到AppDataDirectory</button>
  7. <button class="btn btn-primary" @onclick="()=>Pick(false)">选中不复制(仅限Windows)</button>
  8. @code {
  9. private string? Src;
  10. private async void Pick(bool copy)
  11. {
  12. #if !WINDOWS
  13. if (!copy)
  14. {
  15. return;
  16. }
  17. #endif
  18. var result = await MediaPicker.Default.PickVideoAsync();
  19. var path = result?.FullPath;
  20. if (path is null)
  21. {
  22. return;
  23. }
  24. if (copy)
  25. {
  26. var targetDirectoryPath = Path.Combine(FileSystem.AppDataDirectory, "Video");
  27. Src = await MediaResourceFile.CreateMediaResourceFileAsync(targetDirectoryPath, path);
  28. }
  29. else
  30. {
  31. Src = MauiBlazorWebViewHandler.FilePathToUrlRelativePath(path);
  32. }
  33. await InvokeAsync(StateHasChanged);
  34. }
  35. }
截图

用笔者比较喜欢的动画电影《魁拔》作为视频文件,大约500MB多一些

Windows

image

Android

iOS / Mac选中视频会被压缩,特别慢,所以就用一个短的视频了

iOS

Mac

image

后来补的一张音频的截图,用的原始路径

image

后记

这篇文章改了又改,总觉得有不妥之处。实在改不动了,就这么地吧,可能写的还是不够详细。

笔者水平有限,性能上可能还存在优化的空间,希望各位大佬不吝赐教,提出宝贵意见

源码

本文中的例子的源码放到 Github 和 Gitee 了

有需要的可以去看一下

Github: https://github.com/Yu-Core/MauiBlazorLocalMediaFile

Gitee: https://gitee.com/Yu-core/MauiBlazorLocalMediaFile

原文链接:https://www.cnblogs.com/Yu-Core/p/17855661.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号