[HttpPost("[action]")]
public void Save(IList<IFormFile> UploadFiles)
{
long size = 0;
try
{
foreach (var file in UploadFiles)
{
var filename = ContentDispositionHeaderValue
.Parse(file.ContentDisposition)
.FileName
.Trim('"');
filename = hostingEnv.ContentRootPath.Replace("\\Server", "\\Client\\") + "wwwroot" + $@"\{filename}";
size += (int)file.Length;
if (!System.IO.File.Exists(filename))
{
using (FileStream fs = System.IO.File.Create(filename))
{
file.CopyTo(fs);
fs.Flush();
}
}
}
}
catch (Exception e)
{
Response.Clear();
Response.StatusCode = 204;
Response.HttpContext.Features.Get<IHttpResponseFeature>().ReasonPhrase = "File failed to upload";
Response.HttpContext.Features.Get<IHttpResponseFeature>().ReasonPhrase = e.Message;
}
} |
Hi Christopher,
I'm using the api to save images in the server project.
what would be the best way to get the images file data and URL for a variable so I can save the imageURL in sqlserver database to be able to view them after the data has been saved?
and how would I be able to view the images from the server side? will I have to use an API ?
You can get the uploaded file data and destination path URL on the server side in the save method.
[HttpPost("[action]")] public void Save(IList<IFormFile> UploadFiles) // You can get the file data in UploadFiles { long size = 0; try { foreach (var file in UploadFiles) { var filename = ContentDispositionHeaderValue .Parse(file.ContentDisposition) .FileName .Trim('"'); filename = hostingEnv.ContentRootPath.Replace("\\Server", "\\Client\\") + "wwwroot" + $@"\{filename}"; // destination file path url size += (int)file.Length; if (!System.IO.File.Exists(filename)) { using (FileStream fs = System.IO.File.Create(filename)) { file.CopyTo(fs); fs.Flush(); } } } } |
You can show the preview of the image using SfUploader with the help of Templates. Please refer to the below shared Syncfusion forum for more information.
What i mean is, after ive saved the image in the server path, id like to pass the names of the images uploaded to properties of an object. I'll be saving this names in the database
so in the client side when i get the image names i will be able to display the image using <img src"serverpath/imagename"/>
so how can i display the images saved in the server folder on the client side ?
You can refer to the below shared Syncfusion forum for your requirement.
https://www.syncfusion.com/forums/163494/how-to-pass-custom-data-to-the-client-on-blazor-file-upload