请求端:
var upurl = Config.GetValue("fileupUrl"); HttpPostedFile file = files[0]; MemoryStream memoryStream = new MemoryStream(); file.InputStream.CopyTo(memoryStream); byte[] fileBytes = memoryStream.ToArray(); object sendScObj = new { relaPath = virtualPath, fileName = fileName }; ServicePointManager.ServerCertificateValidationCallback += (s, cert, chain, sslPolicyErrors) => true; ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls; var handler = new HttpClientHandler { ServerCertificateCustomValidationCallback = delegate { return true; } }; using (System.Net.Http.HttpClient client = new System.Net.Http.HttpClient(handler))//若想手动设置Cookie则必须设置UseCookies = false { var fileContent = new ByteArrayContent(fileBytes); fileContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data") { Name = "file", FileName = fileName }; var formDataContent = new MultipartFormDataContent(); formDataContent.Add(fileContent); formDataContent.Headers.Add("UserName", ""); formDataContent.Headers.Add("PassWord", ""); formDataContent.Headers.Add("SendPlatCode", ""); var response = await client.PostAsync(upurl, formDataContent); response.EnsureSuccessStatusCode(); }
接收端:
[HttpPost] public async Task<object> syncReceStream() { #region 授权 string AuthcodeID = ""; var authResult = Authorization(out AuthcodeID); if (authResult.Code != ClientCode.Succeed) { return authResult; } var ips = Config.GetValue("yxIP"); var bendiPath = Config.GetValue("BendiPath"); if (string.IsNullOrEmpty(bendiPath) || string.IsNullOrEmpty(ips) || !ips.Contains(AuthcodeID)) { authResult.Code = ClientCode.FindNull; authResult.ErrorMessage = "未找到网络"; return authResult; } #endregion ApiResultModel apiModel = new ApiResultModel(); try { //获取流 var requestfile = System.Web.HttpContext.Current.Request.Files[0]; if (requestfile != null && requestfile.ContentLength > 0) { var request = await Request.Content.ReadAsStringAsync(); if (string.IsNullOrEmpty(request)) { apiModel.Code = ClientCode.FindNull; apiModel.ErrorMessage = "未找到参数"; return apiModel; } var model = Util.Json.ToObject<AnnexInfoEntity>(request.ToString()); var RoutePath = bendiPath + model.RelaPath; if (!System.IO.Directory.Exists(RoutePath)) { System.IO.Directory.CreateDirectory(RoutePath); } //本地文件夹 requestfile.SaveAs(RoutePath); apiModel.Code = ClientCode.Succeed; } else { apiModel.Code = ClientCode.Exception; apiModel.ErrorMessage = "文件异常"; } } catch (Exception ex) { apiModel.Code = ClientCode.Exception; apiModel.ErrorMessage = ex.Message; } return apiModel; }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了