31 lines
882 B
C#
31 lines
882 B
C#
using Microsoft.AspNetCore.Mvc;
|
|
|
|
namespace HTTP2FileStreams;
|
|
|
|
[ApiController]
|
|
[Route("api/[controller]")]
|
|
public class UploadController : ControllerBase
|
|
{
|
|
[HttpPost]
|
|
[DisableFormValueModelBinding]
|
|
public async Task<IActionResult> Upload()
|
|
{
|
|
var fileName = Request.Headers["X-File-Name"].ToString();
|
|
if (string.IsNullOrEmpty(fileName))
|
|
return BadRequest("File name header is missing");
|
|
|
|
fileName = Uri.UnescapeDataString(fileName);
|
|
|
|
if (!Directory.Exists("Uploads"))
|
|
Directory.CreateDirectory("Uploads");
|
|
|
|
var filePath = Path.Combine("Uploads", fileName);
|
|
await using var fileStream = System.IO.File.Create(filePath);
|
|
await Request.Body.CopyToAsync(fileStream);
|
|
|
|
return Ok(new {
|
|
fileName,
|
|
size = fileStream.Length
|
|
});
|
|
}
|
|
} |