2

my question is, how can i edit or throttle the upload speed when i upload a file to my ASP.Net core web API. We have many clients with very very slow internet, so when the program starts uploading files, the entire bandwidth is used and nothing can be done on the internet. I have 2 code examples here:

1:Client:

class Program
{
    static void Main(string[] args)
    {
        FileStream stream = new FileStream(@"C:\TEMP\te...", FileMode.Open);
        
        Upload(stream).Wait();
    }
    public static async Task Upload(FileStream image)
    {
        using (var client = new HttpClient())
        {
            
            var content = new MultipartFormDataContent();
            content.Add(new StreamContent(image), "te...");
            try
            {
                HttpResponseMessage response =
             await client.PostAsync("https://localhost:5001/api/uploadtest", content);
                var cont = await response.Content.ReadAsStringAsync();
                Console.WriteLine(cont);
            }
            catch(Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
            

            
            
            Console.ReadKey();
        }
        
    }
}

2:ASP.Net Core Web-API

[Route("api/[controller]")]
[ApiController]
public class UploadTestController : ControllerBase
{
    Stream stream;
   
    // POST api/<UploadTestController>
    [DisableRequestSizeLimit]
    [HttpPost]
    public async Task<IActionResult> Post()
    {
        
        stream = Request.Body;
        try
        {
           
            using (var fstream = System.IO.File.OpenWrite(@"F:\TMP\te..."))
            {
                await stream.CopyToAsync(fstream);
            }
            return Ok("test");
        }
        catch(Exception ex)
        {
            Console.WriteLine(ex.ToString());
            return BadRequest();
        }

       
        
    }

    
}
lillcoder
  • 23
  • 4
  • You can't . Ethernet you have a multi hop connection. The speed is based on the slowest hop in the connection. HTTP is the application layer and the speed is the transport layer. From application layer the only thing you can do is enable GZIP to compress the data. – jdweng May 27 '21 at 10:40
  • What about sending the file per chunk? An example found [here](https://stackoverflow.com/a/53159296/797882) – CodeNotFound May 27 '21 at 10:42
  • @FranzGleichmann yes that helped. But i always get a Task canceld exception from the Client now if it takes to long... – lillcoder May 27 '21 at 12:04
  • 3
    crank up the timeout settings – Franz Gleichmann May 27 '21 at 12:07
  • @FranzGleichmann that solved the problem. Thank you very much!! – lillcoder May 27 '21 at 12:40

0 Answers0