2

I added this HTML to a page that I render via a REST call:

StringBuilder builder = new StringBuilder();
. . .
builder.Append("<p></p>");
builder.Append("<a href=\"/App_Data/MinimalSpreadsheetLight.xlsx\" download>");
builder.Append("<p></p>");
. . .
return builder.ToString();

My ASP.NET Web API project has a folder named "App_Data" which does contain a file named "MinimalSpreadsheetLight.xlsx"

The download link is indeed rendered on the page, and clicking it does appear, at first, to download the file (it has the Excel icon, and it bears the file name), but beneath that it says "Failed - No file":

enter image description here

Is the problem with my HTML, or the path I'm using, or file permissions, or what?

I've only tested this with Chrome, so far, BTW. IOW, it's not an IE issue.

UPDATE

I tried it with a leading squiggly, too:

builder.Append("<a href=\"~/App_Data/MinimalSpreadsheetLight.xlsx\" download=\"Spreadsheet file\">");

...yet, alas, to no avail.

UPDATE 2

I changed the pertinent line of HTML to this:

builder.Append("<a href=\"App_Data/MinimalSpreadsheetLight.xlsx\" download=\"Minimal Spreadsheet file\">");

...and it displays in the source like so (with some context):

<p>(Invoice Count excludes credits and re-delivery invoices)</p><p></p><p></p><a href="App_Data/MinimalSpreadsheetLight.xlsx" download="Minimal Spreadsheet file">

...but the link does not appear at all.

UPDATE 3

I was misled by this reference, which showed no text being added; I changed the code to this:

builder.Append("<a href=\"App_Data/MinimalSpreadsheetLight.xlsx\" download=\"Minimal Spreadsheet file\">Spreadsheet file</a>");

...(adding "Spreadsheet file" and closing out the anchor tag), and now the link appears; however, I still get the "Failed - No file" msg, and 2-clicking the "downloaded file" does nothing.

UPDATE 4

I tried two other permutations of what's seen in Update 3, namely with the forward whack reintroduced prior to "App_Data":

builder.Append("<a href=\"/App_Data/MinimalSpreadsheetLight.xlsx\" download=\"Minimal Spreadsheet file\">Spreadsheet file</a>");

...and with both the squiggly prepended and the forward whack:

builder.Append("<a href=\"~/App_Data/MinimalSpreadsheetLight.xlsx\" download=\"Minimal Spreadsheet file\">Spreadsheet file</a>");

...but the results are the same in any of these permutations ("Failed - no file").

UPDATE 5

I also tried it without the "App_Data" at all, on the off change that is not needed:

builder.Append("<a href=\"MinimalSpreadsheetLight.xlsx\" download=\"Minimal Spreadsheet download\">Spreadsheet file</a>");

...but the same "Failed - No file" is the result of that attempt, too.

UPDATE 6

Okay, so I tried this, too (single quotes):

builder.Append("<a href='/App_Data/MinimalSpreadsheetLight.xlsx' download='Minimal Spreadsheet file'>Spreadsheet file</a>");

...but no change. The file is there:

enter image description here

...so why is it not seen or accessible?

UPDATE 7

This:

string fullPath = HttpContext.Server.MapPath("~/App_Data/MinimalSpreadsheetLight.xlsx");

... (which I got from here) fails to compile with, "An object reference is required for the non-static field, method, or property 'System.Web.HttpContext.Server.get'

2-clicking the err msg highlights just "Server"

UPDATE 8

This (which I got from the same place as what I tried in Update 7):

string justDataDir = AppDomain.CurrentDomain.GetData("DataDirectory").ToString();
string url2 = string.Format("<a href='{0}/MinimalSpreadsheetLight.xlsx' download='Minimal Spreadsheet file'><button type=\"button\">Spreadsheet file</button></a>", justDataDir);
builder.Append(url2);

...does nothing; clicking the link doesn't even give me a fake/failed download now...

justDataDir is:

C:\Projects\ProActWebReports\ProActWebReports\App_Data

url2 is:

<a href='C:\Projects\ProActWebReports\ProActWebReports\App_Data/MinimalSpreadsheetLight.xlsx' download='Minimal Spreadsheet file'><button type="button">Spreadsheet file</button></a>

UPDATE 9

I noticed on further fine-tooth-combing that url2 had a forward whack in it; I changed it so that all the whacks were back, but it made no difference to Update 8's results: clicking the link does nothing whatsoever.

If somebody solves this, it will definitely get bountified after the fact.

UPDATE 10

Maybe what I really need to do is, instead of the simple html, add some jQuery that will download the file. But the question is, can jQuery access the App_Data folder any better than raw/simple html can?

Community
  • 1
  • 1
B. Clay Shannon-B. Crow Raven
  • 8,547
  • 144
  • 472
  • 862
  • It does change it; previously, I noted that the link action was actually taking place when I clicked a different bunch of text. Now, the download link doesn't appear at all. – B. Clay Shannon-B. Crow Raven Mar 29 '16 at 21:42
  • What the....you still have my email? Shoot me a link to the site if I can see it? Sounds like there's more afoot at first glance. – Chris W. Mar 29 '16 at 22:07
  • Email? This site is only running locally. – B. Clay Shannon-B. Crow Raven Mar 29 '16 at 22:08
  • 1
    Sorry, thought your name was familiar and I had helped you in the past with some photo app. May be mistaken. Anyway if the download link doesn't appear at all my first guess is there's a broken element somewhere with a missing end tag or something. Even if the href was incorrect, anything between and should make an anchor link. A quick right-click->Inspect Element would probably shed light quick. – Chris W. Mar 29 '16 at 22:13
  • 1
    Oh, and I just realized what you're working in. Do `builder.Append("Spreadsheet file");` beyond that it has to be your file path or something else as a culprit. – Chris W. Mar 29 '16 at 22:15
  • @ChrisW. Oh, yes, I remember that now - good memory. I thought your "you still have my email?" meant you had just gotten an email from me, and I was afraid somebody had hacked my email and was spamming you or something. – B. Clay Shannon-B. Crow Raven Mar 29 '16 at 22:27
  • 1
    No worries, so what is the path from your web root to app_data? I venture to guess that is where your problem is. If your file is truly accessible from there, then you should be able to put that exact path in your browser, but I wouldn't be surprised if you get a 404. – Chris W. Mar 29 '16 at 22:31
  • I don't know anything about that (web root); I'm running it locally and can access the data via http://localhost:52194/api/deliveryperformance/Gramps/20160101/20160104 – B. Clay Shannon-B. Crow Raven Mar 29 '16 at 22:41
  • Ok, so what's app_data in relation to /20160104/ path wise? It's still html getting served up amigo, think of it as whatever /api/ directories parent is, is your web root. For it to work as it sits, you should be able to put in as `http://localhost:52194/api/deliveryperformance/Gramps/20160101/20160104/App_Data/MinimalSpreadsheetLight.xlsx` and it would bring up that file, but I'm betting a beer it doesn't because you're just not hitting the correct directory path. :) – Chris W. Mar 29 '16 at 22:52
  • Yeah, those values are args passed to a REST method, so I would think it should be something more like just the first part + the App_Data folder, but that obviously won't work once it goes into production, because it will no longer be localhost then. – B. Clay Shannon-B. Crow Raven Mar 29 '16 at 23:09
  • localhost is just 127.0.0.1 loopback interface with an http daemon on port 52194, it may as well be http://blah.com/path... – Chris W. Mar 29 '16 at 23:20

1 Answers1

2

The app_data folder is used by iis and asp.net as a private area in which to put database files which can only be accessed by code running on the server.

If you try to access the folder directly via your browser you will get a permissions error.

In order to make the files available for download, move them the a folder under 'Content' (if you have an mvc site) and ensure that your web.config allows the .xlsx exention to be downloaded.

It may depend on what version of iis you are using.

Downloading Docx from IE - Setting MIME Types in IIS

Community
  • 1
  • 1
Ewan
  • 1,261
  • 1
  • 14
  • 25
  • Thanks, Ewan! Very useful information; I ended up using a different design, storing the Excel files in SQL Server, and then downloading from there - see http://stackoverflow.com/questions/36345052/how-can-i-download-a-file-to-a-remote-machine-the-same-or-similarly-to-how-im-d for the details. – B. Clay Shannon-B. Crow Raven Apr 02 '16 at 14:20
  • Yes! I have also answered that one! :) think you are doing the right thing – Ewan Apr 02 '16 at 14:50