locked
Upload file doesn't work RRS feed

  • Question

  • User-1113963543 posted

    public async Task<IActionResult> UploadPhoto(IFormFile file) { _logger.LogInformation("Upload ---> File: {fileName} Size: {fileSize} bytes", file.FileName, file.Length); using (var stream = file.OpenReadStream()) { using (var md5hash = MD5.Create()) { // Get hash from file content var hash = md5hash.ComputeHash(stream); var fileHash = GetMd5Hash(hash); _logger.LogInformation("Upload ---> File hash: {hash}", fileHash); var files = Directory.GetFiles("images"); _logger.LogInformation("Upload ---> Comparing file hashes..."); foreach (var f in files) { var fileName = Path.GetFileNameWithoutExtension(f); _logger.LogInformation("Upload ---> {filename} ?= {filehash}", fileName, fileHash); if (fileName == fileHash) { // duplicate exists, return return Ok(new { status = "Found", file = fileName }); } } // Otherwise create new file. var newFilePath = $"images/{fileHash}{Path.GetExtension(file.FileName)}"; using (var fileStream = new FileStream(newFilePath, FileMode.Create)) { await file.CopyToAsync(fileStream); } _logger.LogInformation("Upload ---> File saved in: " + newFilePath); return Ok(new { status = "Created", file = fileHash }); } } }

    This works locally but when deployed to azure I get the internal server error (500). I'm a complete newbie to azure and cloud services. 

    Does it mean I can't store files on azure?

    This is the error: Could not find a part of the path 'D:\\home\\site\\wwwroot\\images

    Friday, May 5, 2017 3:28 PM

All replies

  • User-1113963543 posted

    I fixed it by using IHostingEnvironment and getting the WebRootPath from there.

    Friday, May 5, 2017 9:25 PM