調用文件接口,需要一個上傳文件和一個Region參數,參考調用實例
public async Task<WebApiResult> UploadFile(UploadFileModel info, IList<IFormFile> files) { try { var postContent = new MultipartFormDataContent(); string boundary = string.Format("--{0}", DateTime.Now.Ticks.ToString("x")); postContent.Headers.Add("ContentType", $"multipart/form-data, boundary={boundary}"); var requestUri = "/api/File/UploadFiles"; if (files.Any()) { var stream = files[0].OpenReadStream(); //files為文件key, files[0].FileName 為文件名稱 postContent.Add(new StreamContent(stream, (int)stream.Length), "files", files[0].FileName); //Region為請求文件接口需要的參數,根據調用接口參數而定 postContent.Add(new StringContent(info.Region), "Region"); } var response = await _httpclient.PostAsync(requestUri, postContent); if (response.IsSuccessStatusCode) { var responseStr = await response.Content.ReadAsStringAsync(); var responseObj = JsonConvert.DeserializeObject<WebApiResult>(responseStr); return responseObj; } //WebApiResult為自定義反會值 return new WebApiResult(ApiResultCode.Fail, "保存失敗"); } catch (Exception ex) { throw new Exception("保存file異常"); } }