.netcore3.1 winfrom 实现文件上传

网友投稿 286 2022-09-04

.netcore3.1 winfrom 实现文件上传

说明:实现文件的上传功能。后端使用.net core3.1作为接口服务,用于接收文件。前端使用winfrom 实现文件上传。

1、.net core3.1 实现webApi接口。

///

/// 客户端:上传图片、视频 /// /// [HttpPost] [Route("postUploadFile")] public ResultObject postUploadFile() { ResultObject result = new ResultObject(); try { IFormFileCollection files = Request.Form.Files; foreach (IFormFile file in files) { //目录 string directory = Path.Combine(Directory.GetCurrentDirectory(), "Upload"); if (!Directory.Exists(directory)) { Directory.CreateDirectory(directory); } string filePath = Path.Combine(directory, file.FileName); // 写入文件 using (var stream = new FileStream(filePath, FileMode.Create)) { file.CopyTo(stream); stream.Flush(); } } result = CommonResult.success(); } catch (Exception vErr) { result = CommonResult.failure(vErr.Message); } return result; }

2、winfrom 项目代码

public class UploadFileHelper { ///

/// Http Post 可传文件参数 /// /// 字符串 字典数据 /// 文件 字典数据 /// public bool PostJsonDataFile(Dictionary dic, Dictionary> dicFile) { bool vCheck = true; string str = ""; try { string url = ConfigurationManager.AppSettings["UploadFileUrl"].ToString(); Uri uri = new Uri(url); var = new HttpClientHandler(); HttpClient client = new HttpClient( client.Timeout = Timeout.InfiniteTimeSpan; var form = new MultipartFormDataContent(); string boundary = string.Format("--{0}", DateTime.Now.Ticks.ToString("x")); form.Headers.Add("ContentType", $"multipart/form-data, boundary={boundary}"); if (dic != null) { foreach (var key in dic.Keys) { form.Add(new StringContent(dic[key].ToString()), key); } } if (dicFile != null) { foreach (var key in dicFile.Keys) { var fileContent = new ByteArrayContent(dicFile[key].Value); fileContent.Headers.ContentType = new MediaTypeHeaderValue("text/plain"); form.Add(fileContent, name: key, fileName: dicFile[key].Key); } } Util.SetCertificatePolicy();//为SSL/TLS 安全通道建立信任关系 HttpResponseMessage response = client.PostAsync(url, form).Result; str = response.Content.ReadAsStringAsync().Result; } catch (Exception vErr) { vCheck = false; str = vErr.Message; } return vCheck; } } public static class Util { /// /// Sets the cert policy. /// public static void SetCertificatePolicy() { ServicePointManager.ServerCertificateValidationCallback += RemoteCertificateValidate; } /// /// Remotes the certificate validate. /// private static bool RemoteCertificateValidate( object sender, X509Certificate cert, X509Chain chain, SslPolicyErrors error) { // trust any certificate!!! System.Console.WriteLine("Warning, trust any certificate"); return true; } }

string vName = DateTime.Now.ToString("yyyyMMddHHmmssffff") + "_" + new Random().Next(1, 10000) + System.IO.Path.GetExtension(vFileNames[i]); //上传图片 if (vCheck) { Dictionary> vParam = new Dictionary>(); vParam.Add("imageFile", new KeyValuePair(vName, System.IO.File.ReadAllBytes(vFileNames[i]))); UploadFileHelper uploadFile = new UploadFileHelper(); vCheck = uploadFile.PostJsonDataFile(null, vParam); }

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:智能指针手写
下一篇:春秋宋国
相关文章

 发表评论

暂时没有评论,来抢沙发吧~