Asp.Net Core 在大文件的上传下载中,官方文档给的方案是设置 Content-Type 为 multipart/form-data,以下下为客户端发送上传的请求
string Boundary = new Guid().ToString().Replace("-", "");
MultipartFormDataContent content = new MultipartFormDataContent(Boundary);
content.Headers.ContentType = MediaTypeHeaderValue.Parse( $"multipart/form-data; boundary={Boundary}" );
//传入文件
var fileContent = System.IO.File.ReadAllBytesAsync( fullPath ).Result;
var byteArrayContent = new ByteArrayContent(fileContent);
content.Add( byteArrayContent, "file", "123.txt" );
//传入token(字符串格式)
string token = "12312";
System.Net.Http.StringContent stringContent = new(token);
content.Add( stringContent, "token" );
content.Add( new StringContent( "num1" ), "num1" );//参数一
content.Add( new StringContent( "num2" ), "num1" );//参数二
//发送请求
var postResponse = client.PostAsync(reqUrl, content).Result;
//获取请求结果
string res = postResponse.Content.ReadAsStringAsync( ).Result;
但在官方示例中只给出了怎样将上述的文件流保存成文件,但是没有提供如果获取附加参数 如上述num1 和 num2 的方法,在网络上找了很多资料都为找到服务端控制器获取的方法,请问需如何获取?下面为服务端控制器代码:
/// <summary>
/// Action for upload large file
/// </summary>
/// <remarks>
/// Request to this action will not trigger any model binding or model validation,
/// because this is a no-argument action
/// </remarks>
/// <returns></returns>
[DisableRequestSizeLimit]
[HttpPost]
[Route( nameof( UploadLargeFile ) )]
public async Task<IActionResult> UploadLargeFile( )
{
//MultipartFormDataContent multipartFormDataContent = new MultipartFormDataContent();
var request = HttpContext.Request;
// validation of Content-Type
// 1. first, it must be a form-data request
// 2. a boundary should be found in the Content-Type
Boolean bb = false;
Microsoft.Net.Http.Headers.MediaTypeHeaderValue mediaTypeHeader = null;
try
{
MediaTypeHeaderValue.TryParse( request.ContentType, out mediaTypeHeader );
bb = true;
}
catch ( Exception ex )
{
Console.WriteLine( ex.Message );
}
if ( !request.HasFormContentType ||
// !MediaTypeHeaderValue.TryParse( request.ContentType, out var mediaTypeHeader ) ||
!bb||
string.IsNullOrEmpty( mediaTypeHeader.Boundary.Value ) )
{
return new UnsupportedMediaTypeResult( );
}
// var boundary = MultipartRequestHelper.GetBoundary(
//MediaTypeHeaderValue.Parse(Request.ContentType),
//_defaultFormOptions.MultipartBoundaryLengthLimit);
try
{
var reader = new MultipartReader(mediaTypeHeader.Boundary.Value, request.Body);
var section = await reader.ReadNextSectionAsync();
var boundary = GetBoundary( MediaTypeHeaderValue.Parse( request.ContentType), int.MaxValue);
// This sample try to get the first file from request and save it
// Make changes according to your needs in actual use
while ( section != null )
{
var hasContentDispositionHeader = ContentDispositionHeaderValue.TryParse(section.ContentDisposition,
out var contentDisposition);
if ( hasContentDispositionHeader && contentDisposition.DispositionType.Equals( "form-data" ) &&
!string.IsNullOrEmpty( contentDisposition.FileName.Value ) )
{
// Don't trust any file name, file extension, and file data from the request unless you trust them completely
// Otherwise, it is very likely to cause problems such as virus uploading, disk filling, etc
// In short, it is necessary to restrict and verify the upload
// Here, we just use the temporary folder and a random file name
// Get the temporary folder, and combine a random file name with it
var fileName = Path.GetRandomFileName();
var saveToPath = Path.Combine(Path.GetTempPath(), fileName);
using ( var targetStream = System.IO.File.Create( saveToPath ) )
{
await section.Body.CopyToAsync( targetStream );
}
return Ok( );
}
section = await reader.ReadNextSectionAsync( );
}
}
catch ( Exception ex )
{
Console.WriteLine( ex.Message );
}
// If the code runs to this location, it means that no files have been saved
return BadRequest( "No files data in the request." );
}