Hello,
I am tryinng to download documents from database and pdf is bieng viewed in browser as per the requirements. I was able to tell that where FileExtension column is coming from which table.
Below my code is working for PDF but not for Documents. And when I trace it in F12 using developers mode I can very well tell what its going through.
Have googled a lot but this place has been helpful more.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
namespace DisplayPDF
{
public partial class DisplayPPOM : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
try
{
// sanitize query string by converting it to an int; anything else will cause an exception
int fileID = int.Parse(Request.QueryString[0]);
//int fileID = int.Parse(Request.QueryString["FileID"]);
View(fileID);
}
catch (SystemException ex)
{
string displayError = ConfigurationManager.AppSettings["displayError"].ToString();
if (displayError == "1")
{
Label1.Text += ex.Message;
}
else
{
Label1.Text += "There is no data. An error occurred";
}
}
}
}
protected void View(int fileID)
{
//string embed = "<object id=\"obj1\" data=\"{0}{1}\" type=\"application/pdf\" >";
//embed += "If you are unable to view file, you can download from <a href = \"{0}{1}&download=1\">here</a>";
//embed += " or download <a target = \"_blank\" href = \"http://get.adobe.com/reader/\">Adobe PDF Reader</a> to view the file.";
//embed += "</object>";
ProcessRequest2(HttpContext.Current);
}
public void ProcessRequest2(HttpContext context)
{
int id = int.Parse(context.Request.QueryString[0]);
//int id = int.Parse(context.Request.QueryString["FileID"]);
byte[] bytes;
string constr = ConfigurationManager.ConnectionStrings["ppomConnectionString"].ConnectionString;
string FileName = "";
string fileextension;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Connection = con;
con.Open();
cmd.CommandText = "SELECT FileExtension FROM vsd.FileAttributes WHERE id=@FileId";
cmd.Parameters.AddWithValue("@FileID", id);
//cmd.Parameters["@FileLeafRef"].Direction = ParameterDirection.Output;
//cmd.Parameters["@FileLeafRef"].Value = FileName;
cmd.CommandText = "smms.GetPPOM";
using (SqlDataReader sdr2 = cmd.ExecuteReader())
{
sdr2.Read();
bytes = (byte[])sdr2["Binary"];
fileextension = sdr2["FileExtension"].ToString();
}
con.Close();
//Label1.Text += bytes.Length.ToString() + " bytes from db <br />";
}
}
if (fileextension.Substring(fileextension.IndexOf('.') + 1).ToLower() == "pdf")
{
context.Response.Buffer = true;
context.Response.Charset = "";
context.Response.Cache.SetCacheability(HttpCacheability.NoCache);
context.Response.ContentType = "application/PDF";
context.Response.BinaryWrite(bytes);
context.Response.Flush();
context.Response.End();
}
else
if (fileextension.Substring(fileextension.IndexOf('.') + 1).ToLower() == "doc")
{
context.Response.ContentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
}
else if (fileextension.Substring(fileextension.IndexOf('.') + 1).ToLower() == "docx")
{
context.Response.ContentType = "application/octet-stream";
//Response.ContentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
}
//Label1.Text += "before BinaryWrite <br />";
// the code below was in the original prototype app with the prototype db, left in in case we need it later
//if (context.Request.QueryString["download"] == "1")
//{
// context.Response.AppendHeader("Content-Disposition", "attachment; filename=" + fileName);
//}
context.Response.Cache.SetCacheability(HttpCacheability.NoCache);
//context.Response.ContentType = "application/pdf";
//return bytes.Length.ToString() + " bytes from db";
}
}
}