1

So I have this function that's downloads a file from a specific path and the path is in a complete different folder path than the wwwroot. The folder is in a shared network folder. Nothing is downloading when it returns but i get no errors and it does say the file exists. When i change the return from PhysicalFile to File its says the files does not exist.

 public FileResult DownloadFileFromFolder()
    {
        
        IEnumerable results = DataLayer.GetData(111);
        foreach (Objects things in results)
        {
            if (things.ID == num)
            {
                filepath = things.FilePath;
                filepath = filepath+"/copy.xlsx";
                break;
            }
        }
        FileInfo myFile = new FileInfo(@filepath);
        bool exists = myFile.Exists;
                            
        return PhysicalFile( myFile.FullName,"application/octet-stream",
                    "Returned_file.xlsx");
    }

I try to use static folder but i dont think im doing it right

 public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            app.UseExceptionHandler("/Home/Error");
        }
        app.UseStaticFiles();

        app.UseRouting();
        app.UseCors("Policy");
      
        app.UseStaticFiles(new StaticFileOptions()
        {
            FileProvider = new PhysicalFileProvider("//path/to/folder/"),
            RequestPath = new PathString("/folder"),
        });
        
        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllerRoute(
                name: "default",
                pattern: "{controller=Home}/{action=Login}/{id?}");
        });
    }
  • 1
    Are you using IIS to host the web application? What account is the web site running under in IIS? app pool identity? Ensure that your app pool identity has access to the folder that you are trying to get the file from – Jonathan Oct 01 '21 at 00:22
  • How do you check if the app pool identity has access to the folder? – user17047507 Oct 01 '21 at 18:39
  • check the permissions on the folder and see if the app pool is in there. by default, the app pool identity will show as the name of the web application – Jonathan Oct 01 '21 at 20:24

0 Answers0