Home > database >  Upload file using HttpClient
Upload file using HttpClient

Time:02-14

i need upload any file vedio,image and doc by api from client to server This code works for me, but for files smaller than 2MB I can't download files up to 10 , 20MB I tried to change the code and style I also converted it to a byte[] and split it into several files, but it didn't work Please Help

this code for client

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
                    {
                        
                        
                    }

}

this code for server

    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);
            }

please help

CodePudding user response:

After communicating with a friend After searching a lot, I found the cause of the problem is the server itself iis As it gives a default value for the maximum upload limit of 2 megabytes

Solve the problem if the server is on iis

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

IIS -> web. config :

<?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>
  • Related