Guest User

Untitled

a guest
Oct 23rd, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.38 KB | None | 0 0
  1. try
  2. {
  3. var cancellationTokenSource = new CancellationTokenSource();
  4. cancellationTokenSource.CancelAfter(1000);
  5. section = await multipartReader.ReadNextSectionAsync(cancellationTokenSource.Token);
  6. }
  7. catch (Exception ex)
  8. {
  9. throw;
  10. }
  11.  
  12. if (sb.ToString().Contains("Content-Type: multipart/form-data"))
  13. {
  14. string boundary = FindBoundary(sb.ToString());
  15. MultipartReader multipartReader = new MultipartReader(boundary, httpContext.Stream);
  16. var section = await multipartReader.ReadNextSectionAsync();
  17. while (section != null)
  18. {
  19. // process each image
  20. const int chunkSize = 1024;
  21. var buffer = new byte[chunkSize];
  22. var bytesRead = 0;
  23. var fileName = GetFileName(section.ContentDisposition);
  24.  
  25. using (var stream = new MemoryStream())
  26. {
  27. do
  28. {
  29. try
  30. {
  31. bytesRead = await section.Body.ReadAsync(buffer, 0, buffer.Length);
  32. }
  33. catch (Exception ex)
  34. {
  35.  
  36. Console.Write(ex);
  37. }
  38.  
  39. stream.Write(buffer, 0, bytesRead);
  40. } while (bytesRead > 0);
  41. retVal.Files.Add(new Tuple<string, string, byte[]>("", fileName, stream.ToArray()));
  42. }
  43.  
  44. try
  45. {
  46. var cancellationTokenSource = new CancellationTokenSource();
  47. cancellationTokenSource.CancelAfter(1000);
  48. section = await multipartReader.ReadNextSectionAsync(cancellationTokenSource.Token);
  49. }
  50. catch (Exception ex)
  51. {
  52. throw;
  53. }
  54.  
  55.  
  56. }
  57. foreach (var file in retVal.Files)
  58. {
  59. File.WriteAllBytes("d:\" + file.Item2, file.Item3);
  60. }
  61. }
Add Comment
Please, Sign In to add comment