I need to download a file from a virtual directory when clicking on a button in sharepoint.

I've tried this:

page.Response.ContentType = "text/vcard";
page.Response.AppendHeader("Content-Disposition", "attachment; filename=" + path);
page.Response.TransmitFile(page.Server.MapPath("~/vcf/" + path));

The problem with this is that I can download the file, but after downloading, I can't do anything on the page. I guess that's the Response.End(), but deleting that line doesn't work because he'll add text to my downloaded file...

Is there an easy solution to this? Or another (working) solution is always welcome...


Change the web part (or whatever) that's rendering the button to include the following javascript into the page:

_spOriginalFormAction = document.forms[0].action;
  • OMG, you're my hero... been looking for this all day! Thanks a lot! – Ruben Herman May 14 '12 at 13:56
  • Per, I'm new in both Asp.Net & Sharepoint. I already have a .js file registered for my WebPart, but I couldn't understand how I must include the code above: should I create a function with that, how I would run this code, etc. Can you help me with this? – tcbrazil Jan 24 '15 at 0:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.