Uploading of image
Arguments
Argument 1 : IFormFile file //The file to upload.
Argument 2 : string? _path //The directory path inside wwwroot where the file will be stored. `images` folder is the default.
Argument 3 : string? _fileName //The custom filename. Original filename will be used if not provided.
Sample Usage
private async Task SaveCompanyLogo(IFormFile _file, string? _filename)
{
try
{
using var client = new HttpClient();
var content = new MultipartFormDataContent();
var uniqueFileName = _filename != null ? _filename + Path.GetExtension(_file.FileName) : _file.FileName;
var path = "images/Logo";
using (var fileStream = _file.OpenReadStream())
{
var streamContent = new StreamContent(fileStream);
content.Add(streamContent,"file", uniqueFileName); //Argument 1
content.Add(new StringContent(path), "_path"); //Argument 2
content.Add(new StringContent(uniqueFileName), "_fileName"); //Argument 3
var response = await client.PostAsync($"https://assetserver.tipco.ph/apiv1/upload", content);
if (!response.IsSuccessStatusCode)
{
var errorMessage = await response.Content.ReadAsStringAsync();
throw new Exception(errorMessage);
}
}
return uniqueFileName;
}catch (Exception ex)
{
throw new Exception(ex.Message);
}
}