Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- try
- {
- var cancellationTokenSource = new CancellationTokenSource();
- cancellationTokenSource.CancelAfter(1000);
- section = await multipartReader.ReadNextSectionAsync(cancellationTokenSource.Token);
- }
- catch (Exception ex)
- {
- throw;
- }
- if (sb.ToString().Contains("Content-Type: multipart/form-data"))
- {
- string boundary = FindBoundary(sb.ToString());
- MultipartReader multipartReader = new MultipartReader(boundary, httpContext.Stream);
- var section = await multipartReader.ReadNextSectionAsync();
- while (section != null)
- {
- // process each image
- const int chunkSize = 1024;
- var buffer = new byte[chunkSize];
- var bytesRead = 0;
- var fileName = GetFileName(section.ContentDisposition);
- using (var stream = new MemoryStream())
- {
- do
- {
- try
- {
- bytesRead = await section.Body.ReadAsync(buffer, 0, buffer.Length);
- }
- catch (Exception ex)
- {
- Console.Write(ex);
- }
- stream.Write(buffer, 0, bytesRead);
- } while (bytesRead > 0);
- retVal.Files.Add(new Tuple<string, string, byte[]>("", fileName, stream.ToArray()));
- }
- try
- {
- var cancellationTokenSource = new CancellationTokenSource();
- cancellationTokenSource.CancelAfter(1000);
- section = await multipartReader.ReadNextSectionAsync(cancellationTokenSource.Token);
- }
- catch (Exception ex)
- {
- throw;
- }
- }
- foreach (var file in retVal.Files)
- {
- File.WriteAllBytes("d:\" + file.Item2, file.Item3);
- }
- }
Add Comment
Please, Sign In to add comment