How to display the excel files

Posted by Chaithragm under C# on 2/13/2013 | Points: 10 | Views : 2496 | Status : [Member] | Replies : 5
i have uploaded the excel files,pdf files to server
im using handler to display the images ,only pdf images are displaying if i try to view the excel file the handler itself is downloading insted of image
Here is the handler

<%@ WebHandler Language="C#" Class="Handler" %>

using System;
using System.Web;
using System.Data.SqlClient;
using System.Data;
using System.Net;


public class Handler : IHttpHandler {

public void ProcessRequest (HttpContext context) {
SqlConnection con = con_manager.getcon();

string sql = "SELECT file_stream FROM image_upload " +
"WHERE stream_id=@ScanID";
SqlCommand cmd = new SqlCommand(sql, con);
cmd.Parameters.Add("@ScanID", SqlDbType.VarChar).Value = context.Request.QueryString["id"];

SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read()) //yup we found our image
{
context.Response.ContentType = "application/pdf";
context.Response.BinaryWrite((byte[])dr["file_stream"]);

}
con.Close();
}

public bool IsReusable {
get {
return false;
}
}

}




Responses

Posted by: aswinialuri-19361 on: 2/14/2013 [Member] Starter | Points: 25

Up
0
Down
hii

In content type you must mention the extension of excel then you will get it(i.e, .xlsx or .xls)





Thanks






Mark as Answer if it helps you
Thanks&Regards
Aswini Aluri

Chaithragm, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Kusumas on: 2/14/2013 [Member] Starter | Points: 25

Up
0
Down
Hi,

you cab try with below code it will help you...


DownloadSpreadsheet.ashx:

<%@ WebHandler Language="C#" Class="DownloadSpreadsheetHandler" %>

using System;
using System.Web;
using System.IO;

public class DownloadSpreadsheetHandler: IHttpHandler {

public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
string path = context.Server.MapPath("~/Book1.xlsx");
using (FileStream spreadsheet = File.OpenRead(path))
{
CopyStream(spreadsheet, context.Response.OutputStream);
}
}

public bool IsReusable {
get {
return false;
}
}

private static void CopyStream(Stream input, Stream output)
{
byte[] buffer = new byte[32768];
while (true)
{
int read = input.Read(buffer, 0, buffer.Length);
if (read <= 0)
return;
output.Write(buffer, 0, read);
}
}

}

///////////////////////////////or////////////////////////////////

Response.Buffer = true;
Response.Clear();
Response.ClearContent();
Response.ClearHeaders();
switch (FileExtension.ToLower())
{
case "pdf":
Response.ContentType = "application/pdf";
break;
case "doc":
Response.ContentType = "application/msword";
break;
case "docx":
Response.ContentType = "application/vnd.ms-word.document.12";
break;
case "xls":
Response.ContentType = "application/vnd.ms-excel";
break;
case "xlsx":
Response.ContentType = "application/vnd.ms-excel.12";
break;
default:
Response.ContentType = "image/jpeg";
break;
}
Response.BinaryWrite(buffer);

Chaithragm, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Chaithragm on: 2/14/2013 [Member] Starter | Points: 25

Up
0
Down
i have used this handler but xlxs files are not displaying.insted the handler is downloading
i am using Iframe for displaying of images.

Chaithragm, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Kusumas on: 2/14/2013 [Member] Starter | Points: 25

Up
0
Down
here

before you write FileExtension.ToLower();
first you find the file extension like below mentioned code....

string FileExtension= Path.GetExtension(myFilePath);
switch(FileExtension.ToLower())
{
//...............................write your code
}

if this will help you mark as a ressolved...

Chaithragm, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Chaithragm on: 2/14/2013 [Member] Starter | Points: 25

Up
0
Down
This is Handler Im using but still doc and .xlsx files are nt displaying.

<%@ WebHandler Language="C#" Class="Handler" %>

using System;
using System.Web;
using System.Data.SqlClient;
using System.Data;
using System.Net;


public class Handler : IHttpHandler {
//string FileExtension;
public void ProcessRequest (HttpContext context) {
SqlConnection con = con_manager.getcon();

string sql = "SELECT * FROM file_upload " +
"WHERE row_id=@ScanID";
SqlCommand cmd = new SqlCommand(sql, con);
cmd.Parameters.Add("@ScanID", SqlDbType.VarChar).Value = context.Request.QueryString["id"];

SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read()) //yup we found our image
{
// context.Response.ContentType = "application/pdf";


context.Response.Buffer = true;
context.Response.Clear();
context.Response.ClearContent();
context.Response.ClearHeaders();
string temp=System.IO.Path.GetExtension(dr["file_name"].ToString());
switch (temp)
{
case "pdf":
context.Response.ContentType = "application/pdf";
break;
case "doc":
context.Response.ContentType = "application/msword";
break;
case "docx":
context.Response.ContentType = "application/vnd.ms-word.document.12";
break;
case "xls":
context.Response.ContentType = "application/vnd.ms-excel";
break;
case "xlsx":
context.Response.ContentType = "application/vnd.ms-excel.12";
break;
default:
context.Response.ContentType = "image/jpeg";
break;
}
// context.Response.BinaryWrite(buffer);
// context.Response.ContentType = "application/vnd.openxmlformats-officedocument.spre";
context.Response.BinaryWrite((byte[])dr["imagedata"]);
}
con.Close();
}

public bool IsReusable {
get {
return false;
}
}

}

Chaithragm, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response