© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•10mo ago•
56 replies
morry329#

Photo upload always fails

I am about to hit my head against the keyboard - as per video attached my photo upload onto my ASP.NET app always fails.
The culprit seems to be the files in this code

    [System.Web.Mvc.HttpPost]
    public async Task<JsonResult> UploadListingImage()
    {
        String result = string.Empty;
        var files = Request.Form.Files; //Debug shows files is always null
        foreach (IFormFile sourceFile in files) //files is null so it does not go thru the loop
        {
            string FileName = sourceFile.Name + ".jpg";
            string imagepath = GetActualPath(FileName);

            try
            {
                if (System.IO.File.Exists(imagepath))
                {
                    System.IO.File.Delete(imagepath);
                }

                using (FileStream stream = System.IO.File.Create(imagepath))
                {
                    await sourceFile.CopyToAsync(stream);
                     result = "pass";
                }
            }
            catch (ArgumentNullException exp)
            {
                Console.WriteLine("Please provide a valid file path.");
            }
        }

        return Json(result); //once the loop is skipped the operation directly returns this one
    }
    [System.Web.Mvc.HttpPost]
    public async Task<JsonResult> UploadListingImage()
    {
        String result = string.Empty;
        var files = Request.Form.Files; //Debug shows files is always null
        foreach (IFormFile sourceFile in files) //files is null so it does not go thru the loop
        {
            string FileName = sourceFile.Name + ".jpg";
            string imagepath = GetActualPath(FileName);

            try
            {
                if (System.IO.File.Exists(imagepath))
                {
                    System.IO.File.Delete(imagepath);
                }

                using (FileStream stream = System.IO.File.Create(imagepath))
                {
                    await sourceFile.CopyToAsync(stream);
                     result = "pass";
                }
            }
            catch (ArgumentNullException exp)
            {
                Console.WriteLine("Please provide a valid file path.");
            }
        }

        return Json(result); //once the loop is skipped the operation directly returns this one
    }
`
Could anyone kindly point me in the right direction as to why files always go null in this code?
For the full contect here is my code https://paste.mod.gg/azthzgzpckja/0
BlazeBin - azthzgzpckja
A tool for sharing your source code with the world!
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources
Was this page helpful?

Similar Threads

Recent Announcements
Next page

Similar Threads

How to go from taking in one photo upload to taking a multiple photo upload
C#CC# / help
2y ago
Photo or File Uploading
C#CC# / help
3y ago
❔ SVG photo is not visible
C#CC# / help
4y ago
❔ JSON serialization fails
C#CC# / help
3y ago