i try to upload and get image in webapi net core but when i get imageUrl it don't run even i use app.UseStaticFile() In Program.cs I use app.UseStaticFiles();
Code Upload and get Image
[HttpPut("UploadImage")]
public async Task<IActionResult> UploadImage(IFormFile formFile, string productId)
{
APIResponse response = new APIResponse();
try
{
string Filepath = GetFilePath(productId);
if (!System.IO.File.Exists(Filepath))
{
System.IO.Directory.CreateDirectory(Filepath);
}
string imagepath = Filepath + "\\" + productId + "png";
if (System.IO.File.Exists(imagepath))
{
System.IO.File.Delete(imagepath);
}
using (FileStream stream = System.IO.File.Create(imagepath))
{
await formFile.CopyToAsync(stream);
response.ResponseCode = 200;
response.Result = "pass";
}
}
catch (Exception ex)
{
response.ErrorMessage = ex.Message;
}
return Ok(response);
}
[HttpGet("GetImage")]
public async Task<IActionResult> GetImage(string productId)
{
string imageUrl = string.Empty;
string hostUrl = $"{this.Request.Scheme}://{this.Request.Host}{this.Request.PathBase}";
try
{
string Filepath = GetFilePath(productId);
string imagepath = Filepath + "\\" + productId + "png";
if (System.IO.File.Exists(imagepath))
{
imageUrl = hostUrl + "/Upload/Product/" + productId + "/" + productId + ".png";
}
else
{
return NotFound();
}
}
catch(Exception ex)
{
}
return Ok(imageUrl);
}
[NonAction]
public string GetFilePath(string productId)
{
return this.environment.WebRootPath + "\\Upload\\Product\\" + productId;
}
This is wwwroot wwwroot
This is result swagger Image not found
Please help me with this problem!!!
In your wwwroot folder, image is named as - '001png' (No dot between 001 and extension) and in your swagger, you are getting '001.png'
Could you please check that ?