I have the following code (I am still developing it) that successfully uploads a file to the needed path.
private async Task AddImage(IFormFile image, string filePath)
{
List<string> PermittedFileTypes = new List<string> {
"image/jpeg",
"image/png",
};
if (PermittedFileTypes.Contains(image.ContentType)) {
// HERE I WILL CHECK NAME AND CHANGE IF IT ALREADY EXSISTS
using (var stream = new FileStream(Path.Combine(filePath, image.FileName), FileMode.Create))
{
await image.CopyToAsync(stream);
}
}
}
I am coming along issues, where files are attempting to be uploaded with the same name (obviously getting errors), but a different file. So I want to check if the file exists, and if it does change the name of the file, probably append a "_#) to the end and then upload the file again under the new name. Problem is the IFormFile.FileName is only for get, and I am not able to set the file name.
Looking online I see where people suggest copying the file to a new name, but since the file is not able to be uploaded I can not do that. Any help is appreciated!