|
| 1 | +// Copyright (c) Argo Zhang (argo@163.com). All rights reserved. |
| 2 | +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. |
| 3 | +// Website: https://www.blazor.zone or https://argozhang.github.io/ |
| 4 | + |
| 5 | +using Microsoft.AspNetCore.Components.Forms; |
| 6 | + |
| 7 | +namespace BootstrapBlazor.Components; |
| 8 | + |
| 9 | +/// <summary> |
| 10 | +/// UploadFile 扩展方法类 |
| 11 | +/// </summary> |
| 12 | +public static class UploadFileExtensions |
| 13 | +{ |
| 14 | + /// <summary> |
| 15 | + /// 获取 base64 格式图片字符串 |
| 16 | + /// </summary> |
| 17 | + /// <param name="upload"></param> |
| 18 | + /// <param name="format"></param> |
| 19 | + /// <param name="maxWidth"></param> |
| 20 | + /// <param name="maxHeight"></param> |
| 21 | + /// <param name="maxAllowedSize"></param> |
| 22 | + /// <param name="token"></param> |
| 23 | + [ExcludeFromCodeCoverage] |
| 24 | + public static async Task RequestBase64ImageFileAsync(this UploadFile upload, string format, int maxWidth, int maxHeight, long maxAllowedSize = 512000, CancellationToken token = default) |
| 25 | + { |
| 26 | + if (upload.File != null) |
| 27 | + { |
| 28 | + try |
| 29 | + { |
| 30 | + var imageFile = await upload.File.RequestImageFileAsync(format, maxWidth, maxHeight); |
| 31 | + using var fileStream = imageFile.OpenReadStream(maxAllowedSize, token); |
| 32 | + using var memoryStream = new MemoryStream(); |
| 33 | + await fileStream.CopyToAsync(memoryStream, token); |
| 34 | + upload.PrevUrl = $"data:{format};base64,{Convert.ToBase64String(memoryStream.ToArray())}"; |
| 35 | + upload.Uploaded = true; |
| 36 | + } |
| 37 | + catch (Exception ex) |
| 38 | + { |
| 39 | + upload.Code = 1004; |
| 40 | + upload.PrevUrl = null; |
| 41 | + upload.Error = ex.Message; |
| 42 | + } |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + /// <summary> |
| 47 | + /// 保存到文件方法 已过期 |
| 48 | + /// </summary> |
| 49 | + /// <param name="upload"></param> |
| 50 | + /// <param name="fileName"></param> |
| 51 | + /// <param name="maxAllowedSize"></param> |
| 52 | + /// <param name="token"></param> |
| 53 | + /// <returns></returns> |
| 54 | + [ExcludeFromCodeCoverage] |
| 55 | + [Obsolete("Use SaveToFileAsync method")] |
| 56 | + public static Task<bool> SaveToFile(this UploadFile upload, string fileName, long maxAllowedSize = 512000, CancellationToken token = default) => upload.SaveToFileAsync(fileName, maxAllowedSize, token); |
| 57 | + |
| 58 | + /// <summary> |
| 59 | + /// 保存到文件方法 |
| 60 | + /// </summary> |
| 61 | + /// <param name="upload"></param> |
| 62 | + /// <param name="fileName"></param> |
| 63 | + /// <param name="maxAllowedSize"></param> |
| 64 | + /// <param name="token"></param> |
| 65 | + /// <returns></returns> |
| 66 | + [ExcludeFromCodeCoverage] |
| 67 | + public static async Task<bool> SaveToFileAsync(this UploadFile upload, string fileName, long maxAllowedSize = 512000, CancellationToken token = default) |
| 68 | + { |
| 69 | + var ret = false; |
| 70 | + if (upload.File != null) |
| 71 | + { |
| 72 | + // 文件保护,如果文件存在则先删除 |
| 73 | + if (File.Exists(fileName)) |
| 74 | + { |
| 75 | + try |
| 76 | + { |
| 77 | + File.Delete(fileName); |
| 78 | + } |
| 79 | + catch (Exception ex) |
| 80 | + { |
| 81 | + upload.Code = 1002; |
| 82 | + upload.Error = ex.Message; |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + if (upload.Code == 0) |
| 87 | + { |
| 88 | + var folder = Path.GetDirectoryName(fileName); |
| 89 | + if (!string.IsNullOrEmpty(folder) && !Directory.Exists(folder)) |
| 90 | + { |
| 91 | + Directory.CreateDirectory(folder); |
| 92 | + } |
| 93 | + |
| 94 | + using var uploadFile = File.OpenWrite(fileName); |
| 95 | + try |
| 96 | + { |
| 97 | + // 打开文件流 |
| 98 | + var stream = upload.File.OpenReadStream(maxAllowedSize, token); |
| 99 | + var buffer = new byte[4 * 1096]; |
| 100 | + int bytesRead = 0; |
| 101 | + double totalRead = 0; |
| 102 | + |
| 103 | + // 开始读取文件 |
| 104 | + while ((bytesRead = await stream.ReadAsync(buffer, token)) > 0) |
| 105 | + { |
| 106 | + totalRead += bytesRead; |
| 107 | + await uploadFile.WriteAsync(buffer.AsMemory(0, bytesRead), token); |
| 108 | + |
| 109 | + if (upload.UpdateCallback != null) |
| 110 | + { |
| 111 | + var percent = (int)((totalRead / upload.File.Size) * 100); |
| 112 | + if (percent > upload.ProgressPercent) |
| 113 | + { |
| 114 | + upload.ProgressPercent = percent; |
| 115 | + upload.UpdateCallback(upload); |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | + upload.Uploaded = true; |
| 120 | + ret = true; |
| 121 | + } |
| 122 | + catch (Exception ex) |
| 123 | + { |
| 124 | + upload.Code = 1003; |
| 125 | + upload.Error = ex.Message; |
| 126 | + } |
| 127 | + } |
| 128 | + } |
| 129 | + return ret; |
| 130 | + } |
| 131 | + |
| 132 | + /// <summary> |
| 133 | + /// 获得图片字节数组方法 |
| 134 | + /// </summary> |
| 135 | + /// <param name="upload"></param> |
| 136 | + /// <param name="format"></param> |
| 137 | + /// <param name="maxWidth"></param> |
| 138 | + /// <param name="maxHeight"></param> |
| 139 | + /// <param name="maxAllowedSize"></param> |
| 140 | + /// <param name="token"></param> |
| 141 | + /// <returns></returns> |
| 142 | + [ExcludeFromCodeCoverage] |
| 143 | + [Obsolete("Use GetBytesAsync method")] |
| 144 | + public static Task<byte[]?> GetByteArray(this UploadFile upload, string format, int maxWidth, int maxHeight, long maxAllowedSize = 512000, CancellationToken token = default) => upload.GetBytesAsync(format, maxWidth, maxHeight, maxAllowedSize, token); |
| 145 | + |
| 146 | + /// <summary> |
| 147 | + /// 获得图片字节数组方法 |
| 148 | + /// </summary> |
| 149 | + /// <param name="upload"></param> |
| 150 | + /// <param name="format"></param> |
| 151 | + /// <param name="maxWidth"></param> |
| 152 | + /// <param name="maxHeight"></param> |
| 153 | + /// <param name="maxAllowedSize"></param> |
| 154 | + /// <param name="token"></param> |
| 155 | + /// <returns></returns> |
| 156 | + [ExcludeFromCodeCoverage] |
| 157 | + public static async Task<byte[]?> GetBytesAsync(this UploadFile upload, string format, int maxWidth, int maxHeight, long maxAllowedSize = 512000, CancellationToken token = default) |
| 158 | + { |
| 159 | + byte[]? ret = null; |
| 160 | + if (upload.File != null) |
| 161 | + { |
| 162 | + try |
| 163 | + { |
| 164 | + var imageFile = await upload.File.RequestImageFileAsync(format, maxWidth, maxHeight); |
| 165 | + using var fileStream = imageFile.OpenReadStream(maxAllowedSize, token); |
| 166 | + using var memoryStream = new MemoryStream(); |
| 167 | + await fileStream.CopyToAsync(memoryStream, token); |
| 168 | + ret = memoryStream.ToArray(); |
| 169 | + } |
| 170 | + catch (Exception ex) |
| 171 | + { |
| 172 | + upload.Code = 1004; |
| 173 | + upload.Error = ex.Message; |
| 174 | + upload.PrevUrl = null; |
| 175 | + } |
| 176 | + } |
| 177 | + return ret; |
| 178 | + } |
| 179 | + |
| 180 | + /// <summary> |
| 181 | + /// 获得图片字节数组方法 |
| 182 | + /// </summary> |
| 183 | + /// <param name="upload"></param> |
| 184 | + /// <param name="maxAllowedSize"></param> |
| 185 | + /// <param name="token"></param> |
| 186 | + /// <returns></returns> |
| 187 | + [ExcludeFromCodeCoverage] |
| 188 | + public static async Task<byte[]?> GetBytesAsync(this UploadFile upload, long maxAllowedSize = 512000, CancellationToken token = default) |
| 189 | + { |
| 190 | + byte[]? ret = null; |
| 191 | + if (upload.File != null) |
| 192 | + { |
| 193 | + try |
| 194 | + { |
| 195 | + using var fileStream = upload.File.OpenReadStream(maxAllowedSize, token); |
| 196 | + using var memoryStream = new MemoryStream(); |
| 197 | + await fileStream.CopyToAsync(memoryStream, token); |
| 198 | + ret = memoryStream.ToArray(); |
| 199 | + } |
| 200 | + catch (Exception ex) |
| 201 | + { |
| 202 | + upload.Code = 1004; |
| 203 | + upload.Error = ex.Message; |
| 204 | + upload.PrevUrl = null; |
| 205 | + } |
| 206 | + } |
| 207 | + return ret; |
| 208 | + } |
| 209 | +} |
0 commit comments