/// <summary> /// 上傳檔案 /// </summary> /// <param name="file"></param> /// <returns></returns> [HttpPost, Route("UploadFile")] [NonAuthorize] public async Task<Response<string>> UploadFile(IFormFile file) { if (file == null || file.Length == 0) { return ResultBuilder.FailResult("", "檔案不存在"); } var requestUrl = $"{CommonConfig.ZFUrl}/special/upload/file"; // 讀取檔案 Stream fileStream = file.OpenReadStream(); // 例項化multipart表單模型 var formData = new MultipartFormDataContent(); // 設定檔案型別表單項,使用StreamContent存放檔案流 //檔名不能帶中文 Todo後面改成檔案id string encodedFileName = Uri.EscapeDataString(file.FileName); formData.Add(new StreamContent(fileStream), "file", encodedFileName); // 例項化HttpClient _httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", CommonConfig.ZFToken); // 傳送請求 HttpResponseMessage result = await _httpClient.PostAsync(requestUrl, formData); // 接受結果 var responseContent = await result.Content.ReadAsStringAsync(); _log.Info($"檔案上傳: {responseContent}"); fileStream.Close(); return ResultBuilder.SuccessResult("檔案上傳成功"); }
file.FileName檔名不能帶中文