使用HttpClient上载文件



我需要通过api将任何文件vedio、image和doc从客户端上传到服务器此代码适用于我,但适用于小于2MB的文件我不能下载1020MB以下的文件我试图更改代码和样式我还将其转换为byte[],并将其拆分为几个文件,但它不起作用请帮助

此代码用于客户端

uri = new Uri(WebAPIUrl + "setimg/");
try
{
var content = new MultipartFormDataContent();
if(mediafile!=null)
{
var streamContent = new StreamContent(mediafile);
streamContent.Headers.Add("Content-Type", "application/octet-stream");
content.Add(streamContent, ""img"", $""{mediafile.Name}"");

}

string json = JsonConvert.SerializeObject(u);
StringContent content1 = new StringContent(json, Encoding.UTF8, "application/json");
content.Add(content1, "value");
bool fin = false;
do
{
fin = true;
var response = await client.PostAsync(uri, content);
if (response.IsSuccessStatusCode)
{
var result = await response.Content.ReadAsStringAsync();
try
{
var Items = JsonConvert.DeserializeObject<List<string>>(result);
return Items;
}
catch
{


}

}

这是服务器的代码

public List<string> upload()
{
var httpRequset = HttpContext.Current.Request;
string imageslink = "";
if (httpRequset.Files.Count > 0)
{
foreach (string file in httpRequset.Files)
{
var postedfile = httpRequset.Files[file];
var filename = postedfile.FileName.Split('\').LastOrDefault().Split('/').LastOrDefault();
// var filepath = HttpContext.Current.Server.MapPath("~/Uploads/" + filename);
imageslink += "D:\casting\" + filename + ";";
var filepath = "D:\casting\" + filename;
postedfile.SaveAs(filepath);
}

请帮助

与朋友交流后经过大量搜索,我发现问题的原因是服务器本身因为它给出了2兆字节最大上传限制的默认值

解决服务器在iis 上的问题

https://stackoverflow.com/a/58566617/8967661

IIS->网状物配置:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.web>
<!-- ~ 2GB -->
<httpRuntime maxRequestLength="2147483647" /> // kbytes
</system.web>
<system.webServer>
<security>
<requestFiltering>
<!-- ~ 4GB -->
<requestLimits maxAllowedContentLength="4294967295" /> // bytes
</requestFiltering>
</security>
</system.webServer>
</configuration>

最新更新