I'm having an issue with my FileResult method not finding a text file in my wwwroot. The method is currently working up until the return statement. The text file is being created in wwwroot with the correct information in it. The issue now is when I click
the button to "download the deck" it pops up an error about not being able to find the file. It makes no sense because if I copy the path from the error message into my file explorer it opens up the file! I'll post the relevant code and error message. Any
help would be appreciated!
public FileResult DownloadDeck(int deckId)
{
var deck = _deckRepository.Decks.Where(d => d.DeckId == deckId).FirstOrDefault();
var fileName = deck.DeckName + ".txt";
string filePath = _hostingEnvironment.WebRootPath + "\\DeckText\\" + fileName;
var memory = new MemoryStream();
using (StreamWriter sw = new StreamWriter(filePath))
{
sw.WriteLine("//Main Deck");
foreach (KeyValuePair<string, int> entry in CurDeck.GetCardDictionary(deck.MainDeckCards))
{
sw.WriteLine(entry.Value + " (" + entry.Key + ")");
}
sw.WriteLine("//Extra Deck");
foreach (KeyValuePair<string, int> entry in CurDeck.GetCardDictionary(deck.ExtraDeckCards))
{
sw.WriteLine(entry.Value + " (" + entry.Key + ")");
}
sw.Close();
}
return File(filePath, "text/plain","Download"+fileName);
}
Error Message
System.IO.FileNotFoundException: Could not find file: C:\Users\chunk\OneDrive\Documents\ChronoclashWebsite\CC Code\ChronoClashDatabase\ChronoClashDeckBuilder\ChronoClashDeckBuilder\wwwroot\DeckText\Test full deck.txt
File name: 'C:\Users\chunk\OneDrive\Documents\ChronoclashWebsite\CC Code\ChronoClashDatabase\ChronoClashDeckBuilder\ChronoClashDeckBuilder\wwwroot\DeckText\Test full deck.txt'
at Microsoft.AspNetCore.Mvc.Infrastructure.VirtualFileResultExecutor.ExecuteAsync(ActionContext context, VirtualFileResult result)
at Microsoft.AspNetCore.Mvc.VirtualFileResult.ExecuteResultAsync(ActionContext context)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeResultAsync(IActionResult result)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.ResultNext[TFilter,TFilterAsync](State& next, Scope& scope, Object& state, Boolean& isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeNextResultFilterAsync[TFilter,TFilterAsync]()
--- End of stack trace from previous location where exception was thrown ---
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResultExecutedContextSealed context)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.ResultNext[TFilter,TFilterAsync](State& next, Scope& scope, Object& state, Boolean& isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeResultFilters()
--- End of stack trace from previous location where exception was thrown ---
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeNextResourceFilter>g__Awaited|24_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResourceExecutedContextSealed context)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeFilterPipelineAsync()
--- End of stack trace from previous location where exception was thrown ---
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope)
at Microsoft.AspNetCore.Routing.EndpointMiddleware.<Invoke>g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger)
at Microsoft.AspNetCore.Session.SessionMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.Session.SessionMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)
Thanks, making it a virtual path seems to have fixed it. Now I just need to work on it downloading through the browser as opposed to actually opening up the browser in the page.
Edit:
Below is the code that has this working properly. Thanks again for the nudge in the right direction!
Member
1 Points
7 Posts
FileResult method is not finding text file in wwwroot to download
May 14, 2020 07:15 PM|Zami77|LINK
Good Afternoon!
I'm having an issue with my FileResult method not finding a text file in my wwwroot. The method is currently working up until the return statement. The text file is being created in wwwroot with the correct information in it. The issue now is when I click the button to "download the deck" it pops up an error about not being able to find the file. It makes no sense because if I copy the path from the error message into my file explorer it opens up the file! I'll post the relevant code and error message. Any help would be appreciated!
Error Message
All-Star
53041 Points
23627 Posts
Re: FileResult method is not finding text file in wwwroot to download
May 14, 2020 08:27 PM|mgebhard|LINK
The ControllerBase.File() method expects a virtual path not absolute.
https://docs.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.mvc.controllerbase.file?view=aspnetcore-3.1
Member
1 Points
7 Posts
Re: FileResult method is not finding text file in wwwroot to download
May 15, 2020 12:57 AM|Zami77|LINK
Thanks, making it a virtual path seems to have fixed it. Now I just need to work on it downloading through the browser as opposed to actually opening up the browser in the page.
Edit:
Below is the code that has this working properly. Thanks again for the nudge in the right direction!