|
@@ -153,55 +153,90 @@ public class HttpHelper
|
|
|
};
|
|
|
}
|
|
|
|
|
|
- public static async Task<string> UploadFileAsync(string localFile, string baseUrl, CancellationToken token = default)
|
|
|
+ public static async Task<string> UploadFileAsync(string localFile,string baseUrl=null, CancellationToken token = default)
|
|
|
{
|
|
|
- string url = SysConfig.GetUrl("File/UploadFileAsync");
|
|
|
+ string url = SysConfig.GetUrl("file/upload", baseUrl);
|
|
|
try
|
|
|
{
|
|
|
var factory = IocContainer.GetService<IHttpClientFactory>();
|
|
|
var client = factory.CreateClient(httpFileKey);
|
|
|
- using (var fileStream = File.OpenRead(localFile))
|
|
|
- using (var fileContent = new StreamContent(fileStream))
|
|
|
- {
|
|
|
- // 3. (可选)设置文件 MIME 类型
|
|
|
- string fileName = Path.GetFileName(localFile);
|
|
|
- string contentType = GetMimeType(fileName);
|
|
|
- fileContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(contentType);
|
|
|
+ using var formData = new MultipartFormDataContent();
|
|
|
+ var fileContent = new StreamContent(File.OpenRead(localFile));
|
|
|
+ fileContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/octet-stream");
|
|
|
+
|
|
|
+ //注意:files必须和接口中的参数保持一致
|
|
|
+ formData.Add(fileContent, "files", Path.GetFileName(localFile));
|
|
|
+
|
|
|
|
|
|
- // 4. 构造 MultipartFormDataContent
|
|
|
- using var formData = new MultipartFormDataContent();
|
|
|
- formData.Add(fileContent, "file", fileName);
|
|
|
- var response = await client.PostAsync(url, formData);
|
|
|
- if (response.IsSuccessStatusCode)
|
|
|
+ // 发送请求
|
|
|
+ var response = await client.PostAsync(url, formData, token);
|
|
|
+ if (response.IsSuccessStatusCode)
|
|
|
+ {
|
|
|
+ string responseBody = await response.Content.ReadAsStringAsync(token);
|
|
|
+ var AjaxResult = JsonConvert.DeserializeObject<AjaxResult<List<string>>>(responseBody);
|
|
|
+ if (AjaxResult.code == 200)
|
|
|
{
|
|
|
- string responseBody = await response.Content.ReadAsStringAsync();
|
|
|
- var AjaxResult = JsonConvert.DeserializeObject<AjaxResult<string>>(responseBody);
|
|
|
- if (AjaxResult.code == 200)
|
|
|
- {
|
|
|
- return AjaxResult.data;
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- throw new Exception(AjaxResult.msg);
|
|
|
- }
|
|
|
+ return AjaxResult.data.FirstOrDefault();
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
- throw new Exception($"上传文件{Path.GetFileName(localFile)}到{url}失败!");
|
|
|
+ throw new Exception(AjaxResult.msg);
|
|
|
}
|
|
|
}
|
|
|
+ else
|
|
|
+ {
|
|
|
+ throw new Exception($"上传文件{Path.GetFileName(localFile)}到{url}失败!");
|
|
|
+ }
|
|
|
+
|
|
|
}
|
|
|
- catch (TaskCanceledException)
|
|
|
+ catch (Exception ex)
|
|
|
{
|
|
|
- if (token != default && token.IsCancellationRequested)
|
|
|
- throw new TaskCanceledException();
|
|
|
+ throw new Exception($"上传文件{Path.GetFileName(localFile)}到{url}失败!", ex);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static async Task<List<string>> UploadFileAsync(IEnumerable<string> localFiles, string baseUrl=null, CancellationToken token = default)
|
|
|
+ {
|
|
|
+ string url = SysConfig.GetUrl("file/upload",baseUrl);
|
|
|
+ try
|
|
|
+ {
|
|
|
+ var factory = IocContainer.GetService<IHttpClientFactory>();
|
|
|
+ var client = factory.CreateClient(httpFileKey);
|
|
|
+ using var formData = new MultipartFormDataContent();
|
|
|
+
|
|
|
+ foreach (var localFile in localFiles)
|
|
|
+ {
|
|
|
+ var fileContent = new StreamContent(File.OpenRead(localFile));
|
|
|
+ fileContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/octet-stream");
|
|
|
+
|
|
|
+ //注意:files必须和接口中的参数保持一致
|
|
|
+ formData.Add(fileContent, "files", Path.GetFileName(localFile));
|
|
|
+ }
|
|
|
+
|
|
|
+ // 发送请求
|
|
|
+ var response = await client.PostAsync(url, formData, token);
|
|
|
+ if (response.IsSuccessStatusCode)
|
|
|
+ {
|
|
|
+ string responseBody = await response.Content.ReadAsStringAsync(token);
|
|
|
+ var AjaxResult = JsonConvert.DeserializeObject<AjaxResult<List<string>>>(responseBody);
|
|
|
+ if (AjaxResult.code == 200)
|
|
|
+ {
|
|
|
+ return AjaxResult.data;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ throw new Exception(AjaxResult.msg);
|
|
|
+ }
|
|
|
+ }
|
|
|
else
|
|
|
- throw new Exception($"上传文件{Path.GetFileName(localFile)}到{url}超时!");
|
|
|
+ {
|
|
|
+ throw new Exception($"批量上传文件到{url}失败!");
|
|
|
+ }
|
|
|
|
|
|
}
|
|
|
catch (Exception ex)
|
|
|
{
|
|
|
- throw new Exception($"上传文件{Path.GetFileName(localFile)}到{url}失败!", ex);
|
|
|
+ throw new Exception($"批量上传文件到到{url}失败!", ex);
|
|
|
}
|
|
|
}
|
|
|
|