locked
Trying to upload an image but get error: DirectoryNotFoundException: Could not find a part of the path RRS feed

  • Question

  • User1514251478 posted

    I have a method that is supposed to upload images to a directory in wwwroot. But i get the following error:

    DirectoryNotFoundException: Could not find a part of the path 'C:(path)\(solutionname)\wwwroot\images\b3889bda-09af-4e4e-8fce-a74d1d881c37\.jpg'.

    Here's the code:

            public Image Upload(HttpContext httpContext, Image image) 
            {
                string webRootPath = _webHostEnvironment.WebRootPath;
                var files = httpContext.Request.Form.Files;
    
                if (files.Count > 0)
                {
                    if (FileCheck(files[0]))
                    {
                        string filename = Guid.NewGuid().ToString();
                        var uploads = Path.Combine(webRootPath, @"images");
                        var extention = Path.GetExtension(files[0].FileName);
                        using (var fileStreams = new FileStream(Path.Combine(uploads, filename, extention), FileMode.Create))
                        {
                            files[0].CopyTo(fileStreams);
                        }
                        image.Url = Path.Combine(uploads, filename, extention);
    
                    }
                }
                return image;
            }

    I am sure that the directory exists, i've even used the Directory.CreateDirectory to make sure. 

    Why do i get this error?

    Friday, June 26, 2020 9:37 AM

Answers

  • User1514251478 posted

    Here was the problem: the method Path.Combine(uploads, filename, extention) made the program look for \wwwroot\images\b3889bda-09af-4e4e-8fce-a74d1d881c37\.jpg (notice the last \ before .jpg). What i intended to do was  Path.Combine(uploads, filename + extention) which makes the program look for \wwwroot\images\b3889bda-09af-4e4e-8fce-a74d1d881c37.jpg instead. 

    Heres the code that was fixed:

    using (var fileStreams = new FileStream(Path.Combine(uploads, filename + extention), FileMode.Create))
                        {
                            files[0].CopyTo(fileStreams);
                        }
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, June 26, 2020 11:54 AM

All replies

  • User1514251478 posted

    Here was the problem: the method Path.Combine(uploads, filename, extention) made the program look for \wwwroot\images\b3889bda-09af-4e4e-8fce-a74d1d881c37\.jpg (notice the last \ before .jpg). What i intended to do was  Path.Combine(uploads, filename + extention) which makes the program look for \wwwroot\images\b3889bda-09af-4e4e-8fce-a74d1d881c37.jpg instead. 

    Heres the code that was fixed:

    using (var fileStreams = new FileStream(Path.Combine(uploads, filename + extention), FileMode.Create))
                        {
                            files[0].CopyTo(fileStreams);
                        }
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, June 26, 2020 11:54 AM
  • User475983607 posted

    The file extension is being used to built the path.

    public Image Upload(HttpContext httpContext, Image image) 
    {
        string webRootPath = _webHostEnvironment.WebRootPath;
        var files = httpContext.Request.Form.Files;
    
        if (files.Count > 0)
        {
            if (FileCheck(files[0]))
            {
                string filename = Guid.NewGuid().ToString() + Path.GetExtension(files[0].FileName);
                var uploads = Path.Combine(webRootPath, @"images");
                using (var fileStreams = new FileStream(Path.Combine(uploads, filename), FileMode.Create))
                {
                    files[0].CopyTo(fileStreams);
                }
                image.Url = Path.Combine(uploads, filename);
    
            }
        }
        return image;
    }

    Friday, June 26, 2020 11:56 AM