Skip to content

Instantly share code, notes, and snippets.

@shammelburg
Created November 26, 2015 21:34
Show Gist options
  • Save shammelburg/322056d777e9975fedde to your computer and use it in GitHub Desktop.
Save shammelburg/322056d777e9975fedde to your computer and use it in GitHub Desktop.
Download File from Server using Web API and AngularJS
// This will download the file from the server
// Pass in the filepath of where file is stored and a new file name
private static void GetFileFromServer(string currentFilePath, string newFileName)
{
HttpResponse httpresponse = HttpContext.Current.Response;
httpresponse.Clear();
httpresponse.Charset = "utf-8";
httpresponse.ContentType = "application/pdf"; // mime type to suit file type
httpresponse.AddHeader("content-disposition", string.Format("attachment; filename={0}", newFileName));
httpresponse.BinaryWrite(File.ReadAllBytes(currentFilePath));
httpresponse.End();
}
// This will open the file in the browser
window.open('api/Controller/GetFileFromServer', '_self', '');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment