failed to upload file to http server from local

ahmed omar 181 Reputation points
2023-04-29T16:25:59.27+00:00

Hello

I'm trying to upload file from my local Pc to httpClient

 public static async Task<string> Uplaod(IWebHostEnvironment _webHostEnviromant,string filePath)
        {
            var filePath = @"C:\Intel\house.png";
            string newName;
            string ext;
                try
                {
                    string pth = _webHostEnviromant.WebRootPath + "\\images\\";
                    if (!Directory.Exists(pth))
                    {
                        Directory.CreateDirectory(pth);
                    }
                     newName = Guid.NewGuid().ToString();
                    pth =Path.Combine(pth,newName );
                     ext=Path.GetExtension(filePath);
                        System.IO.File.Copy(filePath,pth+ext);
                    
                }
                catch (Exception ex)
                { return ex.ToString(); }
               return newName+ext;
        }

it will work fine if i'm working on the local API , but once I upload it to http server , it will gives this error

 "image": "System.IO.DirectoryNotFoundException: Could not find a part of the path 'C:\\Intel\\house.png'.\r\n   at System.IO.FileSystem.CopyFile(String sourceFullPath, String destFullPath, Boolean overwrite)\r\n   at System.IO.File.Copy(String sourceFileName, String destFileName, Boolean overwrite)\r\n   at System.IO.File.Copy(String sourceFileName, String destFileName)
Developer technologies ASP.NET ASP.NET API
{count} votes

2 answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 77,686 Reputation points Volunteer Moderator
    2023-04-29T18:11:33.76+00:00

    Does the file “c:\Intel\house.png” already exist on the server, so it can be copied. Don’t see what this question has to do with upload, the code just copies a local file to a new location.


  2. Bruce (SqlWork.com) 77,686 Reputation points Volunteer Moderator
    2023-04-30T15:47:17.7833333+00:00

    You appear to want to upload a file in mvc, here is a tutorial

    https://www.c-sharpcorner.com/article/upload-files-in-asp-net-mvc-5/

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.