ASP.NET C# Document Download from Database
Posted on Sunday, April 24, 2016
|
No Comments
Load document byte from sql server database. save it to temporary storeage , and view in browser.
public void Download() { string fileName = "abc.doc"; string _path = Request.PhysicalApplicationPath + "Temp/" + fileName; byte[] filebyte = new byte[0]; // load from database File.WriteAllBytes(_path, filebyte ); oR.showReportAjax(ResolveUrl("~/Temp/" + fileName), this.GetType(), this, UpdatePanel1); } public void showReportAjax(string pageUrl, Type cType, Page oPage, Control ctrl) { string url = ResolveClientUrl(pageUrl) ; ScriptManager.RegisterStartupScript(ctrl, typeof(string), "redirect", "window.open('" + url + "');", true); }
Method 2
System.Web.HttpResponse response = System.Web.HttpContext.Current.Response;
response.ClearContent();
response.Clear();
response.ContentType = "text/plain";
response.AddHeader("Content-Disposition",
"attachment; filename=" + fileName + ";");
response.TransmitFile(Server.MapPath("FileDownload.csv"));
response.Flush();
response.End();