Hi, I need some advice. I use Radzen Angular and upload the image to the server in the Upload component. I need to rename the uploaded image before uploading it to the server. Can it be done somehow? I use the application on a tablet and there is an option to insert an image directly from the camera, unfortunately, the camera gives the name image.jpg and I still only have one image on the server. I can't save another one because one and the same one keeps being overwritten.
thanks for the tips on how i can fix it.
Why not rename it when you save it on the server?
Ok i can rename file here but i need write name file to DB. How i can get back filename ? my method look now :
using System;
using System.IO;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Http;
namespace Test
{
[Route("upload")]
public partial class UploadController : Controller
{
[HttpPost]
public ActionResult Post(IFormFile file)
{
try
{
if (file != null && file.Length > 0)
{
string fileName = Guid.NewGuid().ToString() + Path.GetExtension(file.FileName);
// string fileName = file.FileName;
string targetDirectory = Path.Combine(Directory.GetCurrentDirectory(), "Images");
string targetPath = Path.Combine(targetDirectory, fileName);
// Vytvoření složky, pokud neexistuje
if (!Directory.Exists(targetDirectory))
{
Directory.CreateDirectory(targetDirectory);
}
using (var stream = new FileStream(targetPath, FileMode.Create))
{
file.CopyTo(stream);
}
return StatusCode(200,fileName);
}
else
{
return StatusCode(400, "No file was selected for upload.");
}
}
catch (Exception ex)
{
return StatusCode(500, ex.Message);
}
}
}
}
and how i can read filename from return StatusCode(200,fileName); in my client ?
Thank you
see the example code
and also
Hi so my solution is here
but i had a problem when i wanted to return only fileName as string
return StatusCode(200,fileName);
And the solution is here
return StatusCode(200,new{result = fileName.ToString()});
I had to return the fileName as object Now i can read data from event and work with upgraded filename on the client site.
Thank you
1 Like