第一部分:編寫webApi接口,接受客戶端的文件流,再將文件流存為圖片
/// <summary>
? ? ? ? /// 上傳FQC檢測圖片
? ? ? ? /// </summary>
? ? ? ? /// <param name="checkId">檢測單號</param>
? ? ? ? /// <param name="macNum">機(jī)臺號</param>
? ? ? ? /// <param name="imageName">圖片名稱(如1001.bmp)</param>
? ? ? ? /// <returns></returns>
? ? ? ? [HttpPost]
? ? ? ? public async Task<IActionResult> UploadFQCImage(string checkId, string macNum, string imageName)
? ? ? ? {
? ? ? ? ? ? var result = new BaseResult { code = 1 };
? ? ? ? ? ? try
? ? ? ? ? ? {
? ? ? ? ? ? ? ? var request = this.HttpContext.Request;
? ? ? ? ? ? ? ? request.EnableBuffering();
? ? ? ? ? ? ? ? var requestReader = new StreamReader(request.Body);
? ? ? ? ? ? ? ? var requestContent = await requestReader.ReadToEndAsync();
? ? ? ? ? ? ? ? request.Body.Position = 0;
? ? ? ? ? ? ? ? if (requestContent.Length > 0)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? string webRootPath = _env.WebRootPath;
? ? ? ? ? ? ? ? ? ? string subpath = $@"FQC\{macNum}\{DateTime.Today.ToString("yyyy-MM-dd")}\";
? ? ? ? ? ? ? ? ? ? string FilePath = $@"{webRootPath}\upload\{subpath}";
? ? ? ? ? ? ? ? ? ? DirectoryInfo di = new DirectoryInfo(FilePath);
? ? ? ? ? ? ? ? ? ? if (!di.Exists)
? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? di.Create();
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? var imgUrl = subpath + imageName;
? ? ? ? ? ? ? ? ? ? var fullName = FilePath + @"\" + imageName;
? ? ? ? ? ? ? ? ? ? //將收到的流存成文件
? ? ? ? ? ? ? ? ? ? using var fileStream = System.IO.File.Create(fullName);
? ? ? ? ? ? ? ? ? ? await requestReader.BaseStream.CopyToAsync(fileStream);
? ? ? ? ? ? ? ? ? ? await fileStream.FlushAsync();
? ? ? ? ? ? ? ? ? ? result.code = 200;
? ? ? ? ? ? ? ? ? ? result.msg = $"文件{imgUrl}上傳成功";
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? else
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? result.msg = "此接口通過文件流上傳圖片脆侮,接口未獲取到文件流!";
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? ? ? catch (Exception ex)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? result.msg = ex.Message;
? ? ? ? ? ? }
? ? ? ? ? ? return Ok(result);
? ? ? ? }
第二部分:將圖片轉(zhuǎn)為流,然后調(diào)用上面的WebApi接口進(jìn)行圖片上傳
? ? ? ? /// <summary>
? ? ? ? /// 測試上專FQC圖片接口(臨時(shí)用)
? ? ? ? /// </summary>
? ? ? ? /// <returns></returns>
? ? ? ? [HttpPost]
? ? ? ? public IActionResult UploadReport()
? ? ? ? {
? ? ? ? ? ? //以下幾個(gè)變量可移為參數(shù),直接這樣寫是為了方便測試
? ? ? ? ? ? var path = @"D:\23.bmp";
? ? ? ? ? ? using (var client = new HttpClient())
? ? ? ? ? ? {
? ? ? ? ? ? ? ? FileStream stream = new FileStream(path, FileMode.Open, FileAccess.Read);
? ? ? ? ? ? ? ? var content = new StreamContent(stream);
? ? ? ? ? ? ? ? //接收流的接口地址 (某次我忘了加控制器Home,導(dǎo)致post失敗)
? ? ? ? ? ? ? ? var url = "http://127.0.0.1:8080/api/Upload/UploadFQCImage?checkId=1&macNum=D12&imageName=23.bmp";
? ? ? ? ? ? ? ? HttpResponseMessage response = client.PostAsync(url, content).Result;
? ? ? ? ? ? ? ? stream.Dispose();
? ? ? ? ? ? ? ? response.EnsureSuccessStatusCode();
? ? ? ? ? ? ? ? var res = response.Content.ReadAsStringAsync().Result;
? ? ? ? ? ? ? ? return Ok(res);
? ? ? ? ? ? }
? ? ? ? }
作者:Roy yi? ?于南陽