Files
radzen-blazor/RadzenBlazorDemos.Host/Controllers/UploadController.cs
Pavlo Iatsiuk 0ad1200870 #2406 - Allow to define Http method for Upload component (#2407)
* #2406 - Allow to define Http method for Upload component

* #2406 - Allow to define Http method for Upload component and stream raw file data

* #2406 - Allow to define Http method for Upload component
Example how to stream file

---------

Co-authored-by: Pavlo Iatsiuk <pavlo.iatsiuk@nems.eco>
2026-01-12 09:22:15 +02:00

159 lines
4.4 KiB
C#

using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Hosting;
using System;
using System.IO;
using System.Threading.Tasks;
namespace RadzenBlazorDemos
{
[DisableRequestSizeLimit]
public partial class UploadController : Controller
{
private readonly IWebHostEnvironment environment;
public UploadController(IWebHostEnvironment environment)
{
this.environment = environment;
}
[HttpPut("upload/stream")]
public async Task<IActionResult> Stream()
{
try
{
// Put your code here
using (var ms = new MemoryStream())
{
await Request.Body.CopyToAsync(ms);
byte[] fileBytes = ms.ToArray();
return Ok(new { Completed = true, fileSize = fileBytes.Length });
}
}
catch (Exception ex)
{
return StatusCode(500, ex.Message);
}
}
[HttpPost("upload/single")]
public IActionResult Single(IFormFile file)
{
try
{
// Put your code here
return Ok(new { Completed = true });
}
catch (Exception ex)
{
return StatusCode(500, ex.Message);
}
}
[HttpPost("upload/image")]
public IActionResult Image(IFormFile file)
{
try
{
// Used for demo purposes only
DeleteOldFiles();
var fileName = $"upload-{DateTime.Today.ToString("yyyy-MM-dd")}-{Guid.NewGuid()}{Path.GetExtension(file.FileName)}";
using (var stream = new FileStream(Path.Combine(environment.WebRootPath, fileName), FileMode.Create))
{
// Save the file
file.CopyTo(stream);
// Return the URL of the file
var url = Url.Content($"~/images/{fileName}");
return Ok(new { Url = url });
}
}
catch (Exception ex)
{
return StatusCode(500, ex.Message);
}
}
void DeleteOldFiles()
{
foreach (var file in Directory.GetFiles(environment.WebRootPath))
{
var fileName = Path.GetFileName(file);
if (fileName.StartsWith("upload-") && !fileName.StartsWith($"upload-{DateTime.Today.ToString("yyyy-MM-dd")}"))
{
try
{
System.IO.File.Delete(file);
}
catch
{
}
}
}
}
[HttpPost("upload/multiple")]
public IActionResult Multiple(IFormFile[] files)
{
try
{
// Put your code here
return StatusCode(200);
}
catch (Exception ex)
{
return StatusCode(500, ex.Message);
}
}
[HttpPost("upload/custom-header")]
public IActionResult CustomHeader(IFormFile file)
{
try
{
var uploadedBy = Request.Headers["X-Uploaded-By"].ToString();
var authorization = Request.Headers["Authorization"].ToString();
return Ok(new { uploadedBy, authorization});
}
catch (Exception ex)
{
return StatusCode(500, ex.Message);
}
}
[HttpPost("upload/{id}")]
public IActionResult Post(IFormFile[] files, int id, [FromQuery] string query)
{
try
{
return Ok(new { id, query});
}
catch (Exception ex)
{
return StatusCode(500, ex.Message);
}
}
[HttpPost("upload/specific")]
public IActionResult Specific(IFormFile myName)
{
try
{
// Put your code here
return Ok(new { Completed = true });
}
catch (Exception ex)
{
return StatusCode(500, ex.Message);
}
}
}
}