-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Closed
Labels
area-networkingIncludes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractionsIncludes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions
Description
I encountered errors in the following situations:
code like this:
public UploadFileDto UploadFile(HttpRequest request)
{
var trustedFileNameForDisplay = string.Empty;
//var untrustedFileNameForStorage = string.Empty;
var streamedFileContent = new byte[0];
var boundary = MultipartRequestHelper.GetBoundary(MediaTypeHeaderValue.Parse(request.ContentType), _formOptions.MultipartBoundaryLengthLimit);
var reader = new MultipartReader(boundary, request.Body);
var section = reader.ReadNextSectionAsync().Result;
while (section != null)
{
var hasContentDispositionHeader = ContentDispositionHeaderValue.TryParse(section.ContentDisposition, out var contentDisposition);
//untrustedFileNameForStorage = contentDisposition.FileName.Value;
trustedFileNameForDisplay = WebUtility.HtmlEncode(contentDisposition.FileName.Value);
var ext = Path.GetExtension(trustedFileNameForDisplay).ToLowerInvariant();
if (string.IsNullOrEmpty(ext) || !_permittedExtensions.Contains(ext))
{
throw new Exception("file is error");
}
if (hasContentDispositionHeader)
{
if (MultipartRequestHelper.HasFileContentDisposition(contentDisposition))
{
streamedFileContent = FileHelpers.ProcessStreamedFile(section, _fileSizeLimit);
}
}
section = reader.ReadNextSectionAsync().Result;
}
return new UploadFileDto
{
FileContent = streamedFileContent,
FileName = trustedFileNameForDisplay
};
}
1: this exception to be thrown when I use http and file size > 2MB
2: It works like this:
http,file size < 2MB ;
https ,no file size limit .
Another situation
use async code ,It works too,
code like this:
public async Task<UploadFileDto> UploadFile(HttpRequest request)
{
var trustedFileNameForDisplay = string.Empty;
//var untrustedFileNameForStorage = string.Empty;
var streamedFileContent = new byte[0];
var boundary = MultipartRequestHelper.GetBoundary(MediaTypeHeaderValue.Parse(request.ContentType), _formOptions.MultipartBoundaryLengthLimit);
var reader = new MultipartReader(boundary, request.Body);
var section = await reader.ReadNextSectionAsync();
while (section != null)
{
var hasContentDispositionHeader = ContentDispositionHeaderValue.TryParse(section.ContentDisposition, out var contentDisposition);
//untrustedFileNameForStorage = contentDisposition.FileName.Value;
trustedFileNameForDisplay = WebUtility.HtmlEncode(contentDisposition.FileName.Value);
var ext = Path.GetExtension(trustedFileNameForDisplay).ToLowerInvariant();
if (string.IsNullOrEmpty(ext) || !_permittedExtensions.Contains(ext))
{
throw new Exception("file is error");
}
if (hasContentDispositionHeader)
{
if (MultipartRequestHelper.HasFileContentDisposition(contentDisposition))
{
streamedFileContent = FileHelpers.ProcessStreamedFile(section, _fileSizeLimit);
}
}
section = await reader.ReadNextSectionAsync();
}
return new UploadFileDto
{
FileContent = streamedFileContent,
FileName = trustedFileNameForDisplay
};
}
I don't know why there's an exception when I use http and file size > 2MB.
but other situation is working.
Metadata
Metadata
Assignees
Labels
area-networkingIncludes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractionsIncludes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions