Advertisement
Guest User

Untitled

a guest
Nov 1st, 2020
20
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.95 KB | None | 0 0
  1.    
  2. //отправка
  3.  private async Task SendFileSomewhere(Stream stream)
  4.     {
  5.         using var request = new HttpRequestMessage()
  6.         {
  7.             Method = HttpMethod.Post,
  8.             RequestUri = new Uri("https://localhost:44301/FuckingFileSend"),
  9.             Content = new StreamContent(stream),
  10.         };
  11.         using var response = await http.SendAsync(request);
  12.         // TODO check response status etc.
  13.     }
  14.  
  15. //принятие
  16.  
  17.  private const long MaxFileSize = 10L * 1024L * 1024L * 1024L; // 10GB, adjust to your need
  18.  
  19.         [RequestSizeLimit(MaxFileSize)]
  20.         [RequestFormLimits(MultipartBodyLengthLimit = MaxFileSize)]
  21.         [Route("/FuckingFileSend")]
  22.         public async Task ReceiveFile()
  23.         {
  24.             var boundary = HttpContext.Request.GetMultipartBoundary();
  25.             // var boundary = MultipartRequestHelper.GetBoundary(MediaTypeHeaderValue.Parse(Request.ContentType));
  26.             var reader = new MultipartReader(boundary, Request.Body);
  27.  
  28.             // note: this is for a single file, you could also process multiple files
  29.             MultipartSection section = null;
  30.             try
  31.             {
  32.                 section = await reader.ReadNextSectionAsync();
  33.             }
  34.             catch(Exception e)
  35.             {
  36.         //вот тут ловлю исключение Multipart body length limit 16384 exceeded"
  37.             }
  38.            
  39.             var fileName = contentDisposition.FileNameStar.ToString();
  40.             if (string.IsNullOrEmpty(fileName))
  41.             {
  42.                 fileName = contentDisposition.FileName.ToString();
  43.             }
  44.             using var fileStream = section.Body;
  45.             //    await SendFileSomewhere(fileStream);
  46.  
  47.             string path = Path.Combine(environment.ContentRootPath, "upload", fileName);
  48.             using var Stream = new FileStream(path, FileMode.Create);
  49.             fileStream.CopyTo(CurrentStreams.First().Value);
  50.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement