Kaynağa Gözat

完成文件上传接口POC

linhong 3 yıl önce
ebeveyn
işleme
e4e4598610

+ 79 - 0
Controllers/L1SvrController.cs

@@ -39,8 +39,87 @@ namespace LJProxy.Controllers
             {
                 requestBody = await reader.ReadToEndAsync();
             }
+            //var files = Request.Form.Files;
+            //if (files != null && files.Count > 0)
+            //{
+            //    UploadFilesRequest requestObj = new UploadFilesRequest();
+
+            //    foreach (var file in files)
+            //    {
+            //        var path = $"{AppDomain.CurrentDomain.BaseDirectory}test\\{file.FileName}";
+            //        byte[] buffer = new byte[2048];
+            //        using (var fs = file.OpenReadStream())
+            //        {
+            //            using (var fsn = System.IO.File.Create(path))
+            //            {
+            //                while (true)
+            //                {
+            //                    var size = fs.Read(buffer, 0, buffer.Length);
+            //                    if (size == 0)
+            //                    {
+            //                        break;
+            //                    }
+            //                    fsn.Write(buffer, 0, size);
+            //                }
+            //                fsn.Flush();
+            //            }
+            //        }
+            //    }
+            //}
+            var uriBuilder = new UriBuilder
+            {
+                Scheme = Request.Scheme,
+                Host = Request.Host.Host,
+                Port = Request.Host.Port.GetValueOrDefault(80),
+            };
+            var gateway= uriBuilder.Uri;
+            if (!string.IsNullOrEmpty(requestBody))
+            {
+                var reqObj = JObject.Parse(requestBody);
+                reqObj["gateway"] = gateway;
+                requestBody = JsonConvert.SerializeObject(reqObj);
+            }
             var rslt = _ljClient.Pool.DoExcute(apiName, requestBody);
             return Content(rslt, "application/json");
         }
+
+        [HttpPost]
+        [Route("uploadfiles")]
+        public IActionResult UploadFiles()
+        {
+            string token = string.Empty;
+            if (Request.Headers.ContainsKey("Authorization"))
+            {
+                token = Request.Headers["Authorization"];
+            }
+            var files = Request.Form.Files;
+            if (files != null && files.Count > 0)
+            {
+                UploadFilesRequest requestObj = new UploadFilesRequest();
+                List<FileInfoModel> fileList = new List<FileInfoModel>();
+                requestObj.token = token;
+                foreach (var file in files)
+                {
+                    FileInfoModel fileInfo = new FileInfoModel();
+                    fileInfo.FileName = file.FileName;
+                    using (var fs = file.OpenReadStream())
+                    {
+                        byte[] buffer = new byte[fs.Length];
+                        fs.Read(buffer, 0, buffer.Length);
+                        fileInfo.FileData = Convert.ToBase64String(buffer);
+                    }
+                    fileList.Add(fileInfo);
+                }
+                requestObj.FileList = fileList;
+                var requestBody = JsonConvert.SerializeObject(requestObj);
+                var rslt = _ljClient.Pool.DoExcute(requestObj.GetApiName(), requestBody);
+                return Content(rslt, "application/json");
+            }
+            else
+            {
+                return NotFound();
+            }
+            
+        }
     }
 }

+ 39 - 0
Controllers/TestController.cs

@@ -0,0 +1,39 @@
+using Microsoft.AspNetCore.Mvc;
+using System;
+using System.Collections.Generic;
+using System.IO;
+using System.Linq;
+using System.Net.Http;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace LJProxy.Controllers
+{
+    [Route("api/[controller]")]
+    public class TestController : Controller
+    {
+        private readonly IHttpClientFactory _clientFactory;
+        public TestController(IHttpClientFactory clientFactory)
+        {
+            _clientFactory = clientFactory;
+        }
+
+        [Route("postapi/{apiName}")]
+        [HttpPost]
+        public async Task<IActionResult> PostApi(string apiName)
+        {
+            string requestBody;
+            using (StreamReader reader = new StreamReader(Request.Body, Encoding.UTF8))
+            {
+                requestBody = await reader.ReadToEndAsync();
+            }
+            string url = $"http://127.0.0.1:58966/api/l1svr/svr/{apiName}";
+            var content = new StringContent(requestBody, Encoding.UTF8,"application/json");
+            var client = _clientFactory.CreateClient();
+            var response = await client.PostAsync(url, content);
+            string result = await response.Content.ReadAsStringAsync();
+            return Content(result, "application/json");
+
+        }
+    }
+}

+ 21 - 0
Models/UploadFiles.cs

@@ -0,0 +1,21 @@
+using LJLib.Net.SPI.Com;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Threading.Tasks;
+
+namespace LJProxy.Models
+{
+    public class UploadFilesRequest : LJRequest
+    {
+        public string token { get; set; }
+        public List<FileInfoModel> FileList { get; set; }
+
+    }
+
+    public class FileInfoModel
+    {
+        public string FileName { get; set; }
+        public string FileData { get; set; }
+    }
+}

Dosya farkı çok büyük olduğundan ihmal edildi
+ 1 - 1
Properties/PublishProfiles/FolderProfile.pubxml.user


+ 1 - 1
Properties/launchSettings.json

@@ -4,7 +4,7 @@
     "anonymousAuthentication": true,
     "iisExpress": {
       "applicationUrl": "http://127.0.0.1:58966",
-      "sslPort": 44358
+      "sslPort": 0
     }
   },
   "$schema": "http://json.schemastore.org/launchsettings.json",

+ 6 - 0
Startup.cs

@@ -3,6 +3,7 @@ using LJProxy.Services;
 using LJProxy.Settings;
 using Microsoft.AspNetCore.Builder;
 using Microsoft.AspNetCore.Hosting;
+using Microsoft.AspNetCore.Http.Features;
 using Microsoft.AspNetCore.HttpsPolicy;
 using Microsoft.AspNetCore.Mvc;
 using Microsoft.Extensions.Configuration;
@@ -35,6 +36,11 @@ namespace LJProxy
             services.AddSingleton<LJClientPoolService>();
             services.AddScoped<VersionService>();
             GlobalVar.InitSetting(Configuration.GetSection("Appsettings"));
+            services.AddHttpClient();
+            services.Configure<FormOptions>(opt => {
+                //opt.MultipartBodyLengthLimit = 1024 * 1024 * 10;
+                //opt.MultipartHeadersCountLimit = 5;
+            });
         }
 
         // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.