Uploading 0 byte images - C# .NET Core and Async issues

Viewed 592

I've seen this issue come up on other questions and please believe me, I tried so many things to get it working. When I am in debug mode I can upload the image and get it to display perfectly, but once it's in production, all I get is the blank, 0 byte image. I've tried making the whole task async, included await commands, reset the stream position, and tried an Azure blob container. What am I doing wrong? The web application is hosted on Azure.

Controller, CategoriesController.cs

        [Route("save")]
        [HttpPost]
        public async Task<IActionResult> Save(SaleItem item, IFormFile photo)
        {
            if (item.AccessValidation == 000)
            {
                if (photo == null || photo.Length == 0)
                {
                    item.ImgURL = string.Empty;
                }
                else
                {
                    var path = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot/data/images", photo.FileName);
                    var stream = new FileStream(path, FileMode.Create);
                    await photo.CopyToAsync(stream).ConfigureAwait(true);
                    stream.Seek(0, SeekOrigin.Begin);
                    stream.Close();
                    item.ImgURL = photo.FileName;
                }
                item.AccessValidation = 1;
                WriteXML(item);
                return View("Success");
            }
            else
            {
                return Content("Invalid item submission!");
            }
        }

View, Post.cshtml

<form asp-action="save" method="post" enctype="multipart/form-data">
    <div class="grid-x grid-padding-x">
        <div class="cell large-8">
            <label>Item Description</label>
            <input type="text" id="itemDescription" asp-for="Description" />
        </div>
        <div class="cell large-4">
            <input type="submit" class="button large right" value="Save to Inventory" />
        </div>
    </div>

This question was answered - if anyone looks this up in the future and wants to know how I implemented it, here's what I needed to change: Controller, CategoriesController.cs

using Microsoft.AspNetCore.Hosting; //add this reference
namespace Home.Move.Inventory.Controllers
{
    public class CategoriesController : Controller
    {
        private readonly IWebHostEnvironment _env;
        public CategoriesController(IWebHostEnvironment env)
        {
            _env = env;
        }
        [Route("save")]
        [HttpPost]
        public async Task<IActionResult> Save(SaleItem item, IFormFile photo)
        {
            if (item.AccessValidation == 0000)
            {
                if (photo == null || photo.Length == 0)
                {
                    item.ImgURL = string.Empty;
                }
                else
                {

                    var path = Path.Combine(_env.ContentRootPath, "wwwroot/data/images", photo.FileName);
                    var stream = new FileStream(path, FileMode.Create);
                    await photo.CopyToAsync(stream).ConfigureAwait(true);
                    stream.Seek(0, SeekOrigin.Begin);
                    stream.Close();
                    item.ImgURL = photo.FileName;
                }
                item.AccessValidation = 1;
                WriteXML(item);
                return View("Success");
            }
            else
            {
                return Content("Invalid item submission!");
            }

        }
}

I also had to update other references to the contentrootpath - I think I was using it in some areas but not others, mostly due to lack of experience. My understanding is that what I did here was a dependency injection and it made sense to use that consistently as _env throughout the class.

0 Answers
Related