GetFileSuffixAsync(string fileName)\r\n {\r\n return await Task.Run(() =>\r\n {\r\n string suffix = Path.GetExtension(fileName);\r\n return suffix;\r\n });\r\n }<\/string><\/pre>\n1.2 \u4e0a\u4f20\u5355\u6587\u4ef6<\/p>\n
public class FileMessage\r\n {\r\n \/\/\/ < summary>\r\n \/\/\/ \u539f\u6587\u4ef6\u540d\u79f0\r\n \/\/\/ < \/summary>\r\n public string FileName { get; set; }\r\n \r\n \/\/\/ < summary>\r\n \/\/\/ \u9644\u4ef6\u540d\u79f0(\u534f\u8bae\u6216\u5176\u4ed6\u8981\u8fdb\u884c\u6570\u636e\u5e93\u4fdd\u5b58\u4e0e\u6a21\u578b\u7ed1\u5b9a\u7684\u547d\u540d)\r\n \/\/\/ < \/summary>\r\n public string ArgumentName { get; set; }\r\n \r\n \/\/\/ < summary>\r\n \/\/\/ \u6587\u4ef6\u5927\u5c0f(KB)\r\n \/\/\/ < \/summary>\r\n public string FileSize { get; set; }\r\n \r\n \/\/\/ < summary>\r\n \/\/\/ -1\uff1a\u4e0a\u4f20\u5931\u8d25 0\uff1a\u7b49\u5f85\u4e0a\u4f20 1\uff1a\u5df2\u4e0a\u4f20\r\n \/\/\/ < \/summary>\r\n public int FileStatus { get; set; }\r\n \r\n \/\/\/ < summary>\r\n \/\/\/ \u4e0a\u4f20\u7ed3\u679c\r\n \/\/\/ < \/summary>\r\n public string UploadResult { get; set; }\r\n \r\n \/\/\/ < summary>\r\n \/\/\/ \u521b\u5efa\u5b9e\u4f8b\r\n \/\/\/ < \/summary>\r\n \/\/\/ < param name=\"fileName\">\u539f\u6587\u4ef6\u540d\u79f0< \/param>\r\n \/\/\/ < param name=\"argumentName\">(\u65b0)\u9644\u4ef6\u540d\u79f0< \/param>\r\n \/\/\/ < param name=\"fileSize\">\u5927\u5c0f< \/param>\r\n \/\/\/ < param name=\"fileStatus\">\u6587\u4ef6\u72b6\u6001< \/param>\r\n \/\/\/ < returns>< \/returns>\r\n public static FileMessage CreateNew(string fileName,\r\n string argumentName,\r\n string fileSize,\r\n int fileStatus,\r\n string uploadResult)\r\n {\r\n return new FileMessage()\r\n {\r\n FileName = fileName,\r\n ArgumentName = argumentName,\r\n FileSize = fileSize,\r\n FileStatus = fileStatus,\r\n UploadResult = uploadResult\r\n };\r\n }\r\n }<\/pre>\n\/\/\/ < summary>\r\n\/\/\/ \u4e0a\u4f20\u6587\u4ef6\r\n \/\/\/ < \/summary>\r\n \/\/\/ < param name=\"file\">\u4e0a\u4f20\u7684\u6587\u4ef6< \/param>\r\n \/\/\/ < param name=\"fold\">\u8981\u5b58\u50a8\u7684\u6587\u4ef6\u5939< \/param>\r\n \/\/\/ < returns>< \/returns>\r\n public async static Task UploadFileAsync(IFormFile file, string fold)\r\n {\r\n string fileName = file.FileName;\r\n string path = Directory.GetCurrentDirectory() + @\"\/Upload\/\" + fold + \"\/\";\r\n if (!Directory.Exists(path))\r\n {\r\n Directory.CreateDirectory(path);\r\n }\r\n string argumentName = DateTime.Now.ToString(\"yyyyMMddHHmmssfff\") + await GetFileSuffixAsync(file.FileName);\r\n string fileSize = Math.Round((decimal)file.Length \/ 1024, 2) + \"k\";\r\n string filePath = Path.Combine(path, argumentName);\r\n try\r\n {\r\n using (FileStream stream = new FileStream(filePath, FileMode.Create))\r\n {\r\n await file.CopyToAsync(stream);\r\n }\r\n return FileMessage.CreateNew(fileName, argumentName, fileSize, 1, \"\u6587\u4ef6\u4e0a\u4f20\u6210\u529f\");\r\n }\r\n catch (Exception e)\r\n {\r\n return FileMessage.CreateNew(fileName, argumentName, fileSize, -1, \"\u6587\u4ef6\u4e0a\u4f20\u5931\u8d25\uff1a\" + e.Message);\r\n }\r\n }<\/filemessage><\/pre>\n1.3 \u4e0a\u4f20\u591a\u6587\u4ef6<\/p>\n
\/\/\/ < summary>\r\n\/\/\/ \u4e0a\u4f20\u591a\u6587\u4ef6\r\n\/\/\/ < \/summary>\r\n\/\/\/ < param name=\"files\">\u4e0a\u4f20\u7684\u6587\u4ef6\u96c6\u5408< \/param>\r\n\/\/\/ < param name=\"fold\">\u8981\u5b58\u50a8\u7684\u6587\u4ef6\u5939< \/param>\r\n\/\/\/ < returns>< \/returns>\r\n public async static Task> UploadFilesAsync(IFormFileCollection files, string fold)\r\n {\r\n string path = Directory.GetCurrentDirectory() + @\"\/Upload\/\" + fold + \"\/\";\r\n if (!Directory.Exists(path))\r\n {\r\n Directory.CreateDirectory(path);\r\n }\r\n List<\/filemessage> messages = new List<\/filemessage>();\r\n foreach (var file in files)\r\n {\r\n string fileName = file.FileName;\r\n string argumentName = DateTime.Now.ToString(\"yyyyMMddHHmmssfff\") + await GetFileSuffixAsync(file.FileName);\r\n string fileSize = Math.Round((decimal)file.Length \/ 1024, 2) + \"k\";\r\n string filePath = Path.Combine(path, argumentName);\r\n try\r\n {\r\n using (FileStream stream = new FileStream(filePath, FileMode.Create))\r\n {\r\n await file.CopyToAsync(stream);\r\n }\r\n messages.Add(FileMessage.CreateNew(fileName, argumentName, fileSize, 1, \"\u6587\u4ef6\u4e0a\u4f20\u6210\u529f\"));\r\n }\r\n catch (Exception e)\r\n {\r\n messages.Add(FileMessage.CreateNew(fileName, argumentName, fileSize, -1, \"\u6587\u4ef6\u4e0a\u4f20\u5931\u8d25\uff0c\u5931\u8d25\u539f\u56e0\uff1a\" + e.Message));\r\n }\r\n }\r\n return messages;\r\n }<\/filemessage><\/pre>\n[Route(\"api\/[controller]\")]\r\n [ApiController]\r\n public class ManageProtocolFileController : ControllerBase\r\n {\r\n private readonly string createName = \"\";\r\n private readonly IWebHostEnvironment _env;\r\n private readonly ILogger _logger;\r\n public ManageProtocolFileController(IWebHostEnvironment env,\r\n ILogger<\/manageprotocolfilecontroller> logger)\r\n {\r\n _env = env;\r\n _logger = logger;\r\n }\r\n \r\n \/\/\/ < summary>\r\n \/\/\/ \u534f\u8bae\u4e0a\u4f20\u9644\u4ef6\r\n \/\/\/ < \/summary>\r\n \/\/\/ < param name=\"file\">< \/param>\r\n \/\/\/ < returns>< \/returns>\r\n [HttpPost(\"upload\")]\r\n public async Task UploadProtocolFile([FromForm] IFormFile file)\r\n {\r\n return await UploadFileAsync(file, \"ManageProtocol\");\r\n }\r\n }<\/filemessage><\/manageprotocolfilecontroller><\/pre>\n\u4e8c\u3001\u6587\u4ef6\u4e0b\u8f7d<\/strong><\/div>\n2.1 \u83b7\u53d6ContentType\u5c5e\u6027<\/p>\n
\/\/\/ < summary>\r\n\/\/\/ \u83b7\u53d6\u6587\u4ef6ContentType\r\n\/\/\/ < \/summary>\r\n\/\/\/ < param name=\"fileName\">\u6587\u4ef6\u540d\u79f0< \/param>\r\n \/\/\/ < returns>< \/returns>\r\n public async static Task GetFileContentTypeAsync(string fileName)\r\n {\r\n return await Task.Run(() =>\r\n {\r\n string suffix = Path.GetExtension(fileName);\r\n var provider = new FileExtensionContentTypeProvider();\r\n var contentType = provider.Mappings[suffix];\r\n return contentType;\r\n });\r\n }<\/string><\/pre>\n2.2 \u6267\u884c\u4e0b\u8f7d<\/p>\n
[Route(\"api\/[controller]\")]\r\n[ApiController]\r\n public class ManageProtocolFileController : ControllerBase\r\n {\r\n private readonly string createName = \"\";\r\n private readonly IWebHostEnvironment _env;\r\n private readonly ILogger _logger;\r\n public ManageProtocolFileController(IWebHostEnvironment env,\r\n ILogger<\/manageprotocolfilecontroller> logger)\r\n {\r\n _env = env;\r\n _logger = logger;\r\n }\r\n \r\n \/\/\/ < summary>\r\n \/\/\/ \u4e0b\u8f7d\u9644\u4ef6\r\n \/\/\/ < \/summary>\r\n \/\/\/ < param name=\"fileName\">\u6587\u4ef6\u540d\u79f0\r\n \/\/\/ < returns>< \/returns>\r\n [HttpGet(\"download\")]\r\n public async Task Download([FromQuery] string fileName)\r\n {\r\n try\r\n {\r\n string rootPath = _env.ContentRootPath + @\"\/Upload\/ManageProtocolFile\";\r\n string filePath = Path.Combine(rootPath, fileName);\r\n var stream = System.IO.File.OpenRead(filePath);\r\n string contentType = await GetFileContentTypeAsync(fileName);\r\n _logger.LogInformation(\"\u7528\u6237:\" + createName + \"\u4e0b\u8f7d\u540e\u53f0\u5ba2\u6237\u534f\u8bae\u9644\u4ef6:\" + request.FileName);\r\n return File(stream, contentType, fileName);\r\n }\r\n catch (Exception e)\r\n {\r\n _logger.LogError(e, \"\u7528\u6237:\" + createName + \"\u4e0b\u8f7d\u540e\u53f0\u5ba2\u6237\u534f\u8bae\u9644\u4ef6\u51fa\u9519\uff0c\u51fa\u9519\u539f\u56e0:\" + e.Message);\r\n throw new Exception(e.ToString());\r\n }\r\n }\r\n}<\/filestreamresult><\/manageprotocolfilecontroller><\/pre>\n","protected":false},"excerpt":{"rendered":"\u672c\u6587\u5b9e\u4f8b\u4e3a\u5927\u5bb6\u5206\u4eab\u4e86ASP.NET Core\u5b9e\u73b0\u6587\u4ef6\u4e0a\u4f20\u548c\u4e0b\u8f7d\u7684\u5177\u4f53\u4ee3\u7801\uff0c\u4f9b\u5927\u5bb6\u53c2\u8003\uff0c\u5177\u4f53\u5185\u5bb9\u5982\u4e0b 1.1 \u83b7 […]<\/p>\n","protected":false},"author":1482,"featured_media":80683,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-248830","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/248830","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/users\/1482"}],"replies":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/comments?post=248830"}],"version-history":[{"count":6,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/248830\/revisions"}],"predecessor-version":[{"id":248836,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/248830\/revisions\/248836"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/media\/80683"}],"wp:attachment":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/media?parent=248830"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/categories?post=248830"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/tags?post=248830"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}