Skip to content

Instantly share code, notes, and snippets.

View scattered-code's full-sized avatar
🏠
Working from home / Living at work

Alexandru Puiu scattered-code

🏠
Working from home / Living at work
View GitHub Profile
...
.UseKestrel(options =>
{
options.Limits.MaxRequestBodySize = 209715200;
});
...
[HttpPut("upload")]
[DisableRequestSizeLimit] //or [RequestFormLimits(MultipartBodyLengthLimit = 629145600)]
[RequestSizeLimit(209715200)]
public async Task Upload([FromQuery] Guid? messageId)
{
...
[HttpPut("upload")]
[DisableRequestSizeLimit] //or [RequestFormLimits(MultipartBodyLengthLimit = 629145600)]
public async Task Upload([FromQuery] Guid? messageId)
{
...
<system.webServer>
<security>
<requestFiltering>
<requestLimits maxAllowedContentLength="209715200" />
</requestFiltering>
</security>
</system.webServer>
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IdentityModel.Tokens.Jwt;
using System.IO;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading;
using System.Threading.Tasks;
// Source: https://forums.xamarin.com/discussion/180009/how-to-work-with-custom-progressable-stream-content
using System;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
namespace Misc
{
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IdentityModel.Tokens.Jwt;
using System.IO;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading;
using System.Threading.Tasks;
var storageAccount = CloudStorageAccount.Parse(_configuration["MicrosoftAzureStorage:AzureStorageConnectionString"]);
var backOffPeriod = TimeSpan.FromSeconds(2);
var blobClient = storageAccount.CreateCloudBlobClient();
blobClient.DefaultRequestOptions = new BlobRequestOptions()
{
SingleBlobUploadThresholdInBytes = 1024 * 1024, //1MB, the minimum
ParallelOperationThreadCount = 1,
RetryPolicy = new ExponentialRetry(backOffPeriod, maxAttempts: 5),
};
var tempFilename = Path.Combine(Path.GetTempPath(), $"{Guid.NewGuid()}.tmp");
using (var stream = new FileStream(tempFilename, FileMode.CreateNew))
{
const int chunkSize = 1024;
var buffer = new byte[chunkSize];
var bytesRead = 0;
do
{
bytesRead = await fileSection.FileStream.ReadAsync(buffer, 0, buffer.Length);
await stream.WriteAsync(buffer, 0, bytesRead);
var boundary = RequestHelpers.GetBoundary(_httpContextAccessor.HttpContext.Request.ContentType);
var reader = new MultipartReader(boundary, _httpContextAccessor.HttpContext.Request.Body);
var section = await reader.ReadNextSectionAsync();
var fileSection = section.AsFileSection();
var originalFilename = fileSection.FileName;