I have a web application here. The user uploads a .png file whos path I save in my database and copy the IFormFile to a different folder outside the application folder on my drive.
What happens is the file is getting created but its size is 0 bytes.
My controller:
[HttpPost]
public async Task<IActionResult> AddImage(Images file)
{
file.is_deleted = "0";
DateTime currentDateTime = DateTime.Now;
string formattedDateTime = currentDateTime.ToString("yyyy-MM-dd");
file.uploaded_on = formattedDateTime;
file.uploaded_by = GetUserName();
file.image_name = file.image_name.Replace(" ", "_");
if (ModelState.IsValid)
{
if (file.ImageUpload != null)
{
string imageStorage = GetImageStorage();
string fileName = Guid.NewGuid().ToString() + "_" + file.image_name + ".png";
imageStorage += fileName;
var newFileStream = new FileStream(imageStorage, FileMode.Create);
_ = file.ImageUpload.CopyToAsync(newFileStream);
newFileStream.Close();
file.image_path = fileName;
}
}
file.ImageUpload = null;
_ = kneoHttpClient.ApiPostResponse(ConfigAPI() + "Image/Add/", file);
file = null;
return View("Index");
}
The data gets added to the database correctly so the copying is the only problem.
My HTML:
<div class="row form-group">
<div class="custom-file">
<img src="" id="default-image" />
<input asp-for="ImageUpload" class="custom-file-input" id="myfile_input" required />
<label class="custom-file-label" for="myfile_input" id="upload_image_button">Upload Image</label>
<span asp-validation-for="ImageUpload" class="text-danger"></span>
</div>
</div>
Thanks for the support! Cheers!
I tried closing the fileStream as shown, it didnt work. I tried clearing the file object but that too did not work.
Change
var newFileStream = new FileStream(imageStorage, FileMode.Create);
_ = file.ImageUpload.CopyToAsync(newFileStream);
newFileStream.Close();
to this
using(var newFileStream = new FileStream(imageStorage, FileMode.Create))
{
await file.ImageUpload.CopyToAsync(newFileStream);
}
Asynchronous operations must be awaited, otherwise they might get discarded as soon as the request ends and the garbage collector cleans up any orphaned objects, like the Task returned by CopyToAsync
.
Streams should be declared inside using blocks to ensure they get closed even if an exception occurs. Stream.Dispose()
will call Close()
internally