I am trying to upload an image using the RadzenUpload component:
<RadzenUpload Multiple="false" Accept="image/*" Url="upload/single" Progress="@((args) => OnProgress(args, "Images only upload"))"
Complete=@OnComplete Error="@((args) => OnImageUploadError(args, "Image Upload Error"))" />
The controller for this looks like this, I want to get the byte array of the image to later store it in the database (when the user clicks a save button, not directly now):
public partial class ImageUploadController : Controller
{
[HttpPost("upload/single")]
public async Task<IActionResult> Single(IFormFile file)
{
try
{
var fileBytes = await GetImageFileBytes(file);
//return Ok(fileBase64);
//return Ok(new { Completed = true, FileBase64 = fileBase64 }); // works not
//return Ok(new { Completed = true, ImageUploaded = true }); // works
//return Ok(new { Completed = true, FileBase64 = "test" }); // works
return Ok("data:image/png;base64," + Convert.ToBase64String(fileBytes)); // works not
}
catch (Exception ex)
{
return StatusCode(500, ex.Message);
}
}...
I convert it to Base64 because returning a byte array failed but also I am unable to get the Base64 value back in the RawResponse.
The following EventHandler in my Blazor component (where also the RadzenUpload is placed) is not triggered when returning the image data as Base64 or byte array. It is triggered when I return a small test string or simply the { Completed = true } as result:
void OnComplete(UploadCompleteEventArgs args)
{
string x = args.RawResponse;
}
I guess this is maybe because (even with very small images) the response is too big and this is not a recommended approach? I don't see any exceptions occuring it's just that the event handler is not getting triggered in this case.
If this is a dumb idea of doing file uploads, what would be a better approach - I don't want to write the file directly into the DB inside the controller as I would like to wait for that until the user clicks the save button on the form (but upload to server could start already, even bevor save button is pressed, so I would like to avoid setting "Auto='false'" for the RadzenUpload component if possible).