14

Why is my following codes sometimes work, but sometimes it does't work?

 private bool UploadFile(IFormFile ufile, string fname)
 {
     if (ufile.Length > 0)
     {
          string fullpath = Path.Combine(_env.WebRootPath, fname);
          using (var fileStream = new FileStream(fullpath, FileMode.Create))
          {
               ufile.CopyToAsync(fileStream);
          }
          return true;
     }
     return false;
 }

The code did managed to save the picture to a folder which I created under wwwroot, but the picture is not appearing, and even in Visual Studio too.

Is there a way to solve it?

Thanks.

Even when I open up the file explorer of the folder that is storing the pictures, the picture is like is there but not showing any image.

6
  • do you have any solutions that you'd like to share with?
    – Matt
    Commented Jan 16, 2019 at 5:24
  • Check this answer Commented Jan 16, 2019 at 5:46
  • there is a property >> "Show all files" in visual studio solution explorer toolbar. Click to view not include files which then be available to view.
    – Nomi Ali
    Commented Jan 16, 2019 at 6:05
  • @HoomanBahreini That is not correct. The WebRootPath is the wwwroot while ContentRootPath would be the location you set as the IIS root for the application.
    – poke
    Commented Jan 16, 2019 at 9:12
  • “but sometimes it does't work” – What exactly doesn’t work? Check your logs to see what is going on. And try debugging to see what might go wrong there.
    – poke
    Commented Jan 16, 2019 at 9:13

3 Answers 3

21

Try as follows. File will be uploaded to images folder under wwwroot folder.

private async Task<bool> UploadFile(IFormFile ufile)
{
     if (ufile != null && ufile.Length > 0)
     {
          var fileName = Path.GetFileName(ufile.FileName);
          var filePath = Path.Combine(Directory.GetCurrentDirectory(), @"wwwroot\images", fileName);
          using (var fileStream = new FileStream(filePath, FileMode.Create))
          {
              await ufile.CopyToAsync(fileStream);
          }
          return true;
      }
      return false;
}
10
  • May I know what does "await ufile.CopyToAsync(fileSteam)" means?
    – Matt
    Commented Jan 16, 2019 at 5:08
  • This is an asynchronous method which copy the FileStream to the specified path Commented Jan 16, 2019 at 5:10
  • But what is the difference between upload the image files to wwwroot/images and wwwroot/myFolder?
    – Matt
    Commented Jan 16, 2019 at 5:12
  • No difference but I have just used a sample name as images. You can also use your named folder like : var filePath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot\\yourfolderaname", fileName); Commented Jan 16, 2019 at 5:15
  • 2
    Please do not use Directory.GetCurrentDirectory() inside of your code. Use the hosting environment to retrieve the location of the content root.
    – poke
    Commented Jan 16, 2019 at 9:12
1

Had same problem with dot net core, here's what I did:

-Make virtual directory

-Map it to that folder path (inside wwwroot)

-Make your fullpath equals to this VD ; absolute path (can keep it in config file)

-Give write permissions for this folder to iisuser

1
  • Hi thanks for the help, do you have screenschots for each steps that you don't mind sharing with me?
    – Matt
    Commented Jan 16, 2019 at 5:22
0

allow accessing to static files, just add this line in you startup.cs file under Configure method:

app.UseStaticFiles();
3
  • "but the picture is not appearing" you mean it is not appearing in the internet browser? or you don't see it at all even when you browse manually in file browser?
    – LazZiya
    Commented Jan 16, 2019 at 5:38
  • But sometimes it does show itself. Is this something to do with the settings or the codes?
    – Matt
    Commented Jan 16, 2019 at 6:04
  • 1
    actually saying "sometimes" is not clear enough to know the reason, it could be the file size or type which is preventing the file from being saved. Just do more precise tests to find when the files are saved and when they are not, then it can be easy to solve.
    – LazZiya
    Commented Jan 16, 2019 at 6:17

Not the answer you're looking for? Browse other questions tagged or ask your own question.