/// <summary>
/// 上传文件 - 武汉站点
/// </summary>
[HttpPost]
public IActionResult UploadWH(RequestFileModel model)
{
var extensionUpper = Path.GetExtension(model.File.FileName).ToUpper().TrimStart('.');
// POST请求,转发数据
using (var stream = model.File.OpenReadStream())
{
var fileType = FileExtension.GetFileType(stream);
var flag = FileExtension.CheckExtention(extensionUpper, fileType);
if (!flag)
{
//throw new FileErrorException("请选择支持的文件格式");
return EvosErrorContentResult("errorFormat", $"请选择支持的文件格式", fileType, HttpStatusCode.BadRequest);
}
stream.Seek(0, SeekOrigin.Begin);
var result = FileExtension.UploadFileWH(model, stream, _domainConfig.EvosFileSite);
return Ok(JsonConvert.DeserializeObject<JObject>(result));
}
}
/// <summary>
/// /// <summary>
/// 转发文件-武汉文件站点
/// </summary>
/// <param name="model"></param>
/// <param name="stream"></param>
/// <param name="fileSite"></param>
/// <returns></returns>
public static string UploadFileWH(RequestFileModel model, Stream stream, string fileSite)
{
ServicePointManager.ServerCertificateValidationCallback += (s, cert, chain, sslPolicyErrors) => true;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
string result = "";
byte[] buffer = new byte[stream.Length];
stream.Read(buffer, 0, buffer.Length);
var handler = new HttpClientHandler
{
ServerCertificateCustomValidationCallback = delegate { return true; }
};
var webRequest = new HttpClient(handler);
using (var multipartFormDataContent = new MultipartFormDataContent())
{
var values = new[]
{
new KeyValuePair<string, string>("id", model.Id??""),
new KeyValuePair<string, string>("refId", model.ReFld??""),
new KeyValuePair<string, string>("refType", model.RefType??""),
new KeyValuePair<string, string>("bventId", model.BventId??""),
new KeyValuePair<string, string>("remarks", model.Remarks??""),
new KeyValuePair<string, string>("useOriginName", model.UseOriginName.ToString())
};
foreach (var keyValuePair in values)
{
multipartFormDataContent.Add(new StringContent(keyValuePair.Value),
String.Format(""{0}"", keyValuePair.Key));
}
multipartFormDataContent.Add(new ByteArrayContent(buffer), "file", model.File.FileName);
result = webRequest.PostAsync(fileSite, multipartFormDataContent).Result.Content.ReadAsStringAsync().Result;
}
return result;
}
}
public class RequestFileModel
{
public IFormFile File { get; set; }
// 如果传了文件id,则会更新文件内容,如果文件不存在会抛错
public string Id { get; set; }
// 选填,关联id,根据此字段可以查出业务关联的所有文件
public string ReFld { get; set; }
//:选填,关联类型,分类字段,用于标识文件的业务类型
public string RefType { get; set; }
//:选填,会议id
public string BventId { get; set; }
//:备注,选填
public string Remarks { get; set; }
//: 选填,上传文件是否使用原名称,默认是
public bool UseOriginName { get; set; } = true;
}