Net5 版本以Core为底层非framework框架的windowservice 服务。
}}
以下是Job
using Microsoft.Extensions.Logging;using Quartz;using System;using System.IO;using System.Threading.Tasks;namespace WorkerService.Job.Test{ [DisallowConcurrentExecution] public class HelloTestJob2 : IJob { private readonly ILogger<HelloTestJob2> _logger; public HelloTestJob2(ILogger<HelloTestJob2> logger) { _logger = logger; } public Task Execute(IJobExecutionContext context) { FileStream stream = new FileStream(@"d:aa1.txt", FileMode.Create);//fileMode指定是读取还是写入 StreamWriter writer = new StreamWriter(stream); writer.WriteLine("123456aaa" + DateTimeOffset.Now);//写入一行,写完后会自动换行 writer.Write("abc");//写完后不会换行 writer.WriteLine("ABC"); writer.Close();//释放内存 stream.Close();//释放内存 return Task.CompletedTask; } }}程序会根据Corn 设定的运行时间定期在Task Execute(IJobExecutionContext context)方法内运行
然后就是蛮搞笑的,大伙都不用Net5 吗。写服务上传文件。遇到问题搜索NET5处理文件上传问题,居然都是空白的。 那我就只好自己写解决方案了。
客户端图片上传的HTTPHelper.cs部分代码如下
/// <summary> /// 上传文件 /// </summary> /// <param name="url">请求地址</param> /// <param name="path">文件路径(带文件名)</param> /// <returns></returns> public static string HttpPostFile(string url, string path) { // 设置参数 HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest; CookieContainer cookieContainer = new CookieContainer(); request.CookieContainer = cookieContainer; request.AllowAutoRedirect = true; request.Method = "POST";string boundary = DateTime.Now.Ticks.ToString("X"); // 随机分隔线 request.ContentType = "multipart/form-data;charset=utf-8;boundary=" + boundary; byte[] itemBoundaryBytes = Encoding.UTF8.GetBytes("rn--" + boundary + "rn"); byte[] endBoundaryBytes = Encoding.UTF8.GetBytes("rn--" + boundary + "--rn"); int pos = path.LastIndexOf("\"); string fileName = path.Substring(pos + 1); //请求头部信息 StringBuilder sbHeader = new StringBuilder(string.Format("Content-Disposition:form-data;name="file";filename="{0}"rnContent-Type:application/octet-streamrnrn", fileName)); byte[] postHeaderBytes = Encoding.UTF8.GetBytes(sbHeader.ToString()); StringBuilder builder = new StringBuilder($"Content-Disposition:form-data;name="subPath"rnrntmswechat"); byte[] postHeaderBytestwo = Encoding.UTF8.GetBytes(builder.ToString()); FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read); byte[] bArr = new byte[fs.Length]; fs.Read(bArr, 0, bArr.Length); fs.Close(); Stream postStream = request.GetRequestStream(); postStream.Write(itemBoundaryBytes, 0, itemBoundaryBytes.Length); postStream.Write(postHeaderBytes, 0, postHeaderBytes.Length); postStrwnBDnvEeam.Write(bArr, 0, bArr.Length); postStream.Write(itemBoundaryBytes, 0, itemBoundaryBytes.Length); postStream.Write(postHeaderBytestwo, 0, postHeaderBytestwo.Length); postStream.Write(endBoundaryBytes, 0, endBoundaryBytes.Length); postStream.Close(); //发送请求并获取相应回应数据 HttpWebResponse response = request.GetResponse() as HttpWebResponse; //直到request.GetResponse()程序才开始向目标网页发送Post请求 Stream instream = response.GetResponseStream(); StreamReader sr = new StreamReader(instream, Encoding.UTF8); //返回结果网页(html)代码 string content = sr.ReadToEnd(); return content; }重点是服务端的接收,部分代码如下
try { var files = Request.Form.Files; if (files != null) { var file = files[0]; var location = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase + $"Image\" + file.FileName; if (!Directory.Exists(System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase + $"Image\")) //判断上传文件夹是否存在,若不存在,则创建 { Directory.CreateDirectory(System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase + $"Image\"); //创建文件夹 } using (var stream = new FileStream(location, FileMode.Create)) { await file.CopyToAsync(stream); result = 1; } } //using (var reader = new StreamReader(Request.Body))//从身体里读取 //{ // var body = await reader.ReadToEndAsync(); //} } catch (Exception e ) { throw; }哪怕你用的是文件流上传,不是表单提交。但是你的文件依旧在Request.Form.Files 里!!!!
但你也可以通过Request.body 读到流
//using (var reader = new StreamReader(Request.Body))//从身体里读取//{// var body = await reader.ReadToEndAsync();//}








