Net Core web app that has a utility where users can upload a photo. It's using a API Controller to do the actual uploading. I created a folder in the Solution called ImageStorage
where the folder path looks like this:
G:\GameDevelopment\DungeonMaxer\MainApp\wwwroot\ImageStorage
and I also created a folder under ImageStorage called uploads.
I am trying to test it out locally(IIS Express) and I added this line to the controller:
[HttpPost("upload")]
public async Task<IActionResult> PostAsync(IFormFile file)
{
// parent folder for storage
var serverStoragePath = Server.MapPath(@"~/ImageStorage");
// uploads folder inside the parent
var fileRoute = Path.Combine(serverStoragePath, "uploads");
// get file extension
string extension = System.IO.Path.GetExtension(theFile.FileName);
// create a new name for storage
string name = Guid.NewGuid().ToString().Substring(0, 8) + extension;
// get the link to the file
string link = Path.Combine(fileRoute, name);
using (FileStream writerFileStream = System.IO.File.Create(link))
{
await stream.CopyToAsync(writerFileStream);
writerFileStream.Dispose();
}
// Return the file path as json
Hashtable imageUrl = new Hashtable();
imageUrl.Add("link", "/uploads/" + name);
return Json(imageUrl);
}
But when I run the project locally and put a break point on the line, var serverStoragePath = Server.MapPath(@"~/ImageStorage");
, I get this error:
System.NullReferenceException: 'Object reference not set to an instance of an object.'
I also tried wwwroot\ImageStorage
but that gave me the same error.
Is there a way to access this path ?
thanks!