9 Replies - 457 Views - Last Post: 20 February 2019 - 02:45 PM Rate Topic: -----

#1 andrewsw   User is online

  • quantum multiprover
  • member icon

Reputation: 6775
  • View blog
  • Posts: 27,938
  • Joined: 12-December 12

Excel download cannot find file

Posted 10 January 2019 - 08:57 AM

I have an ASP.NET Core Angular application (with Kendo UI).

I am creating and downloading an Excel file (using a package called NPOI). It correctly creates and saves Excel files in my wwwroot/excel folder.

If I download the file directly with a download link as follows it works fine:

success: function (result, status, xhr) {
          console.log('success');
          var fileDownload = "/excel/" + result.type + result.stamp + ".xlsx"
          window.location.href = fileDownload;
        },

So with static files it correctly reads wwwroot and recognises the file as an Excel file when downloaded.

Instead, I want to veer the download to my controller action:

        [Route("Download")]
        public FileResult Download(string type, string timestamp)
        {
            var contentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";

            var fileName = $"{type}{timestamp}.xlsx";

            var webRoot = _env.WebRootPath;
            var excelPath = webRoot + "/excel";
            var filePath = Path.Combine(excelPath, fileName);

            return File(filePath, contentType, "ContactsExport.xlsx");
        }

with the revised download code as:

        success: function (result, status, xhr) {
          if (result.stamp.length > 0)
            window.location.href = "api/Export/Download?type=" + result.type + "&timestamp=" + result.stamp;
        },
        error: function (xhr, status, error) {
          console.log('error ' + error);
        }

This gives me an error

Quote

System.IO.FileNotFoundException: Could not find file: C:\SVN Projects\People\People\wwwroot/excel\Contacts100119154619.xlsx
File name: 'C:\SVN Projects\People\People\wwwroot/excel\Contacts100119154619.xlsx'
at Microsoft.AspNetCore.Mvc.Infrastructure.VirtualFileResultExecutor.ExecuteAsync(ActionContext context, VirtualFileResult result)
at Microsoft.AspNetCore.Mvc.VirtualFileResult.ExecuteResultAsync(ActionContext context)

I can clearly see that the file is named correctly, and that that is the correct location.

Do you have an idea why this is not working? Is there some setting that I am missing in my Action to enable the download?

Is This A Good Question/Topic? 0
  • +

Replies To: Excel download cannot find file

#2 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14924
  • View blog
  • Posts: 59,575
  • Joined: 12-June 08

Re: Excel download cannot find file

Posted 10 January 2019 - 09:02 AM

Is that errant forward slash in the path causing a problem?
Was This Post Helpful? 1
  • +
  • -

#3 andrewsw   User is online

  • quantum multiprover
  • member icon

Reputation: 6775
  • View blog
  • Posts: 27,938
  • Joined: 12-December 12

Re: Excel download cannot find file

Posted 10 January 2019 - 09:04 AM

Posted Image

View Postmodi123_1, on 10 January 2019 - 04:02 PM, said:

Is that errant forward slash in the path causing a problem?

It has worked either way round. I've been using the forward slash throughout. I'll perform a quick test again, though.
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14924
  • View blog
  • Posts: 59,575
  • Joined: 12-June 08

Re: Excel download cannot find file

Posted 10 January 2019 - 09:05 AM

09    var excelPath = webRoot + "/excel";

Sure.. but if you flip this to a backslash does the code react the same way?

--
derp.. didn't catch the update to the post.
Was This Post Helpful? 0
  • +
  • -

#5 Dormilich   User is offline

  • 痛覚残留
  • member icon

Reputation: 4224
  • View blog
  • Posts: 13,389
  • Joined: 08-June 10

Re: Excel download cannot find file

Posted 10 January 2019 - 09:07 AM

Quote

I can clearly see that the file is named correctly, and that that is the correct location.

However, .NET disagrees with you about that.

Does .NET understand paths with forward slashes?
Was This Post Helpful? 1
  • +
  • -

#6 andrewsw   User is online

  • quantum multiprover
  • member icon

Reputation: 6775
  • View blog
  • Posts: 27,938
  • Joined: 12-December 12

Re: Excel download cannot find file

Posted 10 January 2019 - 09:08 AM

Tested, it makes no odds

Quote

An unhandled exception occurred while processing the request.
FileNotFoundException: Could not find file: C:\SVN Projects\People\People\wwwroot\excel\Contacts100119160512.xlsx

Was This Post Helpful? 0
  • +
  • -

#7 Dormilich   User is offline

  • 痛覚残留
  • member icon

Reputation: 4224
  • View blog
  • Posts: 13,389
  • Joined: 08-June 10

Re: Excel download cannot find file

Posted 10 January 2019 - 09:08 AM

It could also be a timing problem in creating the file.
Was This Post Helpful? 0
  • +
  • -

#8 andrewsw   User is online

  • quantum multiprover
  • member icon

Reputation: 6775
  • View blog
  • Posts: 27,938
  • Joined: 12-December 12

Re: Excel download cannot find file

Posted 10 January 2019 - 09:15 AM

It works with a direct download link. I use the same sequence for both. That is, the file is created with an Ajax call; on success, I then download the file, so I am doubtful that it is a timing problem. I could double check by interrupting it.

I notice that the File() method calls the first argument a virtualPath, whereas WebRootPath returns an absolute path. I'll check this...
Was This Post Helpful? 0
  • +
  • -

#9 andrewsw   User is online

  • quantum multiprover
  • member icon

Reputation: 6775
  • View blog
  • Posts: 27,938
  • Joined: 12-December 12

Re: Excel download cannot find file

Posted 10 January 2019 - 09:26 AM

It was the virtual/absolute issue! :)

I modified to this in the Action:

            //var webRoot = _env.WebRootPath;
            var excelPath = "excel";           //webRoot + "/excel";
            var filePath = Path.Combine(excelPath, fileName);


I borrowed the Export action from an existing ASP.NET MVC project where the path was constructed with "C:\SVN Projects\ExportFolder\". In ASP.NET Core, though, static files are automatically mapped to wwwroot. So I only need "excel" for it to look in "wwwroot/excel".

Thanks both.



A little additional info. When creating the Excel file I use a FileSteam, which does use the absolute path given by WebRootPath. But when downloading the file the ControllerBase.File uses a virtual path.
Was This Post Helpful? 1
  • +
  • -

#10 LandonEvans   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 20-February 19

Re: Excel download cannot find file

Posted 20 February 2019 - 02:45 PM

you have to try https://zetexcel.com/. i am sure it will help you. it will also help you with your cross-platform application.

This post has been edited by andrewsw: 21 February 2019 - 01:47 AM
Reason for edit:: removed redundant quote, just use the Reply button

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1