Conditional file download



We have our website developed on-EPiServer-CMS-6.

On our website we have many case studies that people can download (a link to download PDF file).  I would like to make it a condition that people must provide, there name, email address and contact phone number to receive the case study.

If there is any way, then can anyone please provide me the solution?

Feb 03, 2014 5:38

Is this download supposed to start when you hit the submit button on the contact information form, or when would you supply the link?

Instead of the link directly to the PDF file, you could have the server write the content of it as the response after you've validated your form data. I guess you could make it work for clicking a link as well with some semi-ugly hacks, but guess it's easier with a submit button.

Here is a piece of code from an old project, we had the server fetching PDF files from a remote location feeding them to the visitor (in a HttpHandler hooked up on certain routs like /myPDF/the_file.pdf); you would have to change the way of getting the binary data.

var format = context.Request.Browser.Browser == "IE" ? "inline;filename={0}.pdf" : "inline;filename*=UTF-8''{0}.pdf";
var contentDisposition = string.Format(format, HttpUtility.UrlEncode(filename));
context.Response.AddHeader("Content-Disposition", contentDisposition);
context.Response.ContentType = "application/pdf";

byte[] fileData;
using (var webClient = new WebClient())
    fileData = webClient.DownloadData(streamUrl);


Feb 03, 2014 7:32

Hi Mathias,

Thank you very much for your reply.

I’m little new to EpiServer, I couldn’t really get your point. If you don’t mind could you please explain a little bit more (If possible could you please provide the code, and then I can have a look)?

What I was thinking is in a form, make name, email, and contact number mandatory and after they click the submit button, I want to display the link to download the file (currently the link is visible to anyone). Is this possible?

Or if you have any easy possible way, can you please provide me the solution?

Sorry for bothering you again.

Thanks again.

Feb 03, 2014 23:45

Hey Shatheesh!

No problem at all :)

I think an important question is whether it is the link that should be protected or the PDF-file itself. When the visitor clicks the submit button, and gets the link, is it OK if they can use it multiple times or save it and send it to other people? Or is there a need for preventing the file from being downloaded if the contact information is not provided? The first option is quite a bit easier; I was pitching ideas for the second option in my previous answer, so if the first one is alright then just ignore it.

I'll see if I can find a little time to write a sample for you for the first one in a bit.



Feb 04, 2014 7:26

I pushed a small sample to Github for you:

It's just a couple of server controls with validators, and a click event on a submit button checking the information and showing the link. Was that what you were after?


Feb 04, 2014 8:14

Hi Mathias,

Thank you very much for your reply.

Yes, I’m looking for a similar solution that you post.

Thank you very much for your help, much appreciated.



Feb 05, 2014 1:03

You're welcome :) Glad it helped.

Feb 05, 2014 7:41
This thread is locked and should be used for reference only. Please use the Legacy add-ons forum to open new discussions.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.