简体   繁体   中英

Core mvc: Files not getting saved in wwwroot folder but creating files with "wwwroot.....jpg" name in the root

The image uploading is working perfectly in the development server in the windows environment but when I run the code in the Remote Linux server, the files get uploaded but in the root folder and for which the files can not be accessed by the website.

public async Task<IActionResult> Index(IList<IFormFile> files,Type type)
    {
        Startup.Progress = 0;


        foreach (IFormFile source in files)
        {
            if (isFileImage(source))
            {
                string filename = ContentDispositionHeaderValue.Parse(source.ContentDisposition).FileName.ToString().Trim('"');

                filename = this.EnsureCorrectFilename(filename);

                string serverFilePath = this.GetPathAndFilename(filename);

                try
                {
                    await source.CopyToAsync(new FileStream(serverFilePath,FileMode.Create));
                }
                catch (Exception e)
                {
                }
                finally
                {

                }
            }

        }

        return Content("Success");

    }

    private string GetPathAndFilename(string filename)
    {
        string path = Path.Combine(Directory.GetCurrentDirectory(),@"wwwroot\images\materials", filename);

        return path;
    }

This is the code responsible for uploading an image. In the development windows environment, it works perfectly as the files get saved in the "wwwroot\\images\\materials" folder. But when the code is run the Remote Linux serves the files get uploaded but are saved in the root folder with "wwwroot\\images\\materials*.jpg" name. Even when running the code in development mode in the Remote server this problem occurs.

Since you're using Path.Combine I would suggest passing each part of the path as a parameter. So instead of @"wwwroot\\images\\materials" as one parameter, you would pass them separately "wwwroot", "images", "materials" .

Try this simple. In this you have to inject _hostingEnvironment so you can get ContentRootPath

            string folderName = "Upload/Profile/" + user.Id;
            string webRootPath = _hostingEnvironment.ContentRootPath;
            string newPath = Path.Combine(webRootPath, folderName);
            if (!Directory.Exists(newPath))
            {
                Directory.CreateDirectory(newPath);
            }
            string extention = file.ContentType.Split("/")[1];
            string fileName = user.Id + ".jpg";
            string fullPath = Path.Combine(newPath, fileName);
            string envpath = folderName + "/" + fileName;
            using (var stream = new FileStream(fullPath, FileMode.Create))
            {
                file.CopyTo(stream);
            }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM