0

I feel like I've gone in a thousand circles on this and it should be much simpler than it is, but basically I have this simple resume submission page in asp.net using C#. The files are on the server in a folder but it seems every solution I find just does nothing at all when I try to execute it.

Here's some examples of some of the things I've tried:

string folder = "~/App_Data/Resumes/" + Session["ResumeName"].ToString();
string folder2 = Server.MapPath("~/App_Data/Resumes/" + Session["ResumeName"].ToString());

System.Web.VirtualPathUtility.ToAbsolute(folder);
System.Diagnostics.Process.Start(folder2);

I'd never imagined something as simple as opening a document from a server would be so difficult. Is it not possible to just simply have a link to a document and have it opened when the user clicks it? That's all I want to accomplish. Thank you reading my through my frustration.

To clarify, System.Diagnostics.Process.Start(folder2); works locally, but doesn't work on the web server. I want this to work on the web server so that's why that one is out. I've tried to make it possible for the web server to open that file but I don't think I know enough about IIS to make it happen.

RobLife
  • 95
  • 1
  • 9
  • 2
    `Process.Start` will open the document *on the server*. You need to serve the content back to the client, either with an HttpHandler (for WebForms) or a FileResult (MVC). – vcsjones Sep 16 '14 at 16:23
  • 2
    So, just to get it right, you want to open the file at the client side? Or at the web server? – Srikanth Venugopalan Sep 16 '14 at 16:23
  • I just updated, I am trying to open the document from the web server client side. So for instance the resumes reside on the server and I want the user to open them in the corresponding application to view them - Word, adobe, notepad, etc. – RobLife Sep 16 '14 at 16:28
  • 1
    How does opening the files on the server help you? For instance, if you opened a Word file on the server your users wouldn't be able to see it on their client machines. Unless you mean you are processing these files on the server? – Peter Monks Sep 16 '14 at 16:33
  • I don't mean opening on the server, just that the file is located on the server and the user is being directed to it to open on their machine. – RobLife Sep 16 '14 at 17:08
  • Is the `button` or `hyperlink` you are planning to have to open the document is part of a grid or page? – Dennis R Sep 16 '14 at 17:47

3 Answers3

2

App_Data is a protected folder in ASP.NET. IIS will not serve anything from that folder directly to the client-- you will need to build a handler that serves the document directly.

Here are a few samples, though they aren't representative of everything you need to do. Others can feel free to add additional resources. Your handler would have to open the file and write it to the output stream:

Using a c# handler to serve up wav files cuts audio short (only a couple of seconds) ASP.net cache ASHX file server-side

Community
  • 1
  • 1
ps2goat
  • 8,067
  • 1
  • 35
  • 68
  • App_Data didn't even publish to the server from visual studio, I just created the folder because the path was already set to it. Is it still protected in that circumstance? – RobLife Sep 16 '14 at 17:10
  • 1
    Yes, it is based on folder name only, in this case. If you don't mind everyone being able to access the documents, you can just change your folder name to a non-reserved folder name such as `/resumes/`. Other reserved folder names include `App_code`, `bin`, etc. Direct requests for files in those folders will be denied. – ps2goat Sep 16 '14 at 18:33
  • I think the primary issue here was the App_Data thing. I think a lot of this stuff that has failed would work with it being in a normal folder. I just read somewhere that App_Data is a default location that you can put files that the application can access without needing permissions. I may have read that wrong. – RobLife Sep 16 '14 at 19:37
  • You are confusing the application with the end user. Your application will have access, but the end user with his browser will not have access. – ps2goat Sep 16 '14 at 19:41
0
  1. Don't use folders like App_Data to have your downloadable resources .
  2. There is a similar question on stackoverflow which will give you a exact step by step answer - ASP.NET file download from server
Community
  • 1
  • 1
lame_coder
  • 3,085
  • 3
  • 19
  • 21
  • I disagree with #1, because the file can be sensitive. You can pass a hard rule like that. If you want to control which users can view certain documents, App_Data is a great way to do that within your app (not linking to other servers, for example) when used with an `HttpHandler`. – ps2goat Sep 16 '14 at 16:41
0

From the title of your question, If I understand correctly you want to open a .doc or .pdf on a link or button click. This will open the respective document on the client.

<a id="lnkResume" href="#" runat="server" target="_blank">Download Resume</a>

And you can set the dynamic resume name from the code like below

 string fileName = Session["ResumeName"].ToString();
 lnkResume.HRef = Server.MapPath("~/App_Data/Resumes/" + fileName);
Dennis R
  • 3,195
  • 1
  • 19
  • 24