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.

Matt
  • 199
  • 1
  • 1
  • 9
  • do you have any solutions that you'd like to share with? – Matt Jan 16 '19 at 05:24
  • Check [this answer](https://stackoverflow.com/questions/49398965/what-is-the-equivalent-of-server-mappath-in-asp-net-core/49399105#49399105) – Hooman Bahreini Jan 16 '19 at 05: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 Jan 16 '19 at 06: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 Jan 16 '19 at 09: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 Jan 16 '19 at 09:13
  • @poke, thanks for letting me know. – Hooman Bahreini Jan 16 '19 at 09:16

3 Answers3

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;
}
mekb
  • 554
  • 8
  • 22
TanvirArjel
  • 30,049
  • 14
  • 78
  • 114
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

Shahrukh Ahmad
  • 133
  • 1
  • 1
  • 10
  • Hi thanks for the help, do you have screenschots for each steps that you don't mind sharing with me? – Matt Jan 16 '19 at 05:22
0

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

app.UseStaticFiles();
LazZiya
  • 5,286
  • 2
  • 24
  • 37
  • "_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 Jan 16 '19 at 05:38
  • But sometimes it does show itself. Is this something to do with the settings or the codes? – Matt Jan 16 '19 at 06: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 Jan 16 '19 at 06:17