SitePoint Sponsor

User Tag List

Results 1 to 20 of 20
  1. #1
    SitePoint Zealot
    Join Date
    Jan 2009
    Posts
    176
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    how user can view the .jpg,.doc,.xls file...

    I m working on visualstudio2005 asp.net 2.0 using c# and having database as oracle.

    I have a form in which user is creating one incident for service or help and submitting the information which are required and also attachment can be done. Here m allowing user to add multiple attachment. And the limit to add attachment is 3. And only .jpg,.doc,.xls file can be attached.

    I did till uploading the file and data is also stored in database.

    Now i need a method to allow users to view records and then the ability to view these images,doc file and excel files.

    I have taken hyperlink button in which i want to display the name of file.
    For example.. image1.jpg

    and when the user click on this hyperlink button the image should be display.
    sameway if document file is there then it should display documentname.doc and after clicking the data should be opened or saved in word.

    How to do this?

    The document table is this.
    Document_id 1
    DOCUMENT_NAME srs
    DOCUMENT_TYPE application/msword doc
    DOCUMENT_EXT doc
    DOCUMENT (ORABLOB)

    Please help.

  2. #2
    SitePoint Addict kunalraj's Avatar
    Join Date
    Nov 2008
    Posts
    200
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hello,

    for this you can get data from database.
    like
    String sql = "select doc,docext,docname from tablename where ticno='"+ticnum+"' ";

    cmd = new SqlCommand(sql, con);
    dr = cmd.ExecuteReader();
    while (dr.Read())
    {
    ext = dr.GetString(1);//here getstring 1 is field no of docext
    String docname=dr.GetString(2);
    if (ext.Equals("jpg") || ext.Equals("jpeg") || ext.Equals("JPG") || ext.Equals("JPEG"))
    {
    Response.AddHeader("content-disposition", "attachment;filename=" +"+docname+"+ ".jpg");
    Response.ContentType = "image/jpeg";
    Response.BinaryWrite((byte[])dr["doc"]);//[doc] is field blob.

    }
    }

    you can get all data on administrator`s page in table and give hiperlink button or hiperlink and call the page by clicking on it and in which you have to write above code.
    The above bold code you have to write for download or view attachment.

    if you can`t understand than ask me?

    thanks,
    kunalraj

  3. #3
    SitePoint Addict kunalraj's Avatar
    Join Date
    Nov 2008
    Posts
    200
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    if your doc extension is .xls than you have to add in that code like


    else if (ext.Equals("xls") || ext.Equals("XLS") )
    {
    Response.AddHeader("content-disposition", "attachment;filename=" +"+docname+"+ ".xls");
    Response.ContentType = "image/xls";// you can either write Response.ContentType = "blob/xls";
    Response.BinaryWrite((byte[])dr["doc"]);//[doc] is field blob.
    }

  4. #4
    SitePoint Enthusiast
    Join Date
    Mar 2009
    Location
    England
    Posts
    37
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Exclamation

    I suggest using IHttpHandler, if your using your own IHttpFactory or IHttpModule thats overriding everything then you will have to attach using your own code otherwise standard is to add to your Web.Config file (Goes in the <HttpHandlers> section)

    The base of the IHttpHandler is...

    Code Csharp:
    	public class MediaHandler : IHttpHandler
    	{
    		public void ProcessRequest(HttpContext Context)
    		{
     
    // Your code to open a connection to your datasource goes here, you will need to expose an open StreamReader to your database's Binary Object, we'll call it ORABLOB and its of type StreamReader
     
     
    			if(ORABLOB != null)
    			{
    				byte[] baData = new byte[1024];
    				int iBytesRead;
    				bool bFirstBlock = true;
    				while((iBytesRead = ORABLOB.BaseStream.Read(
    					baData, 0, baData.Length)) > 0)
    				{
    					if(bFirstBlock)
    					{
    						//	Signatures:-
    						//		jpg = 0xffd8ffe0
    						//		gif = 0x474946
    						//		png = 0x89504e470d0a1a0a
    						Context.Response.ContentType = 
    							(baData[0] == 0x89 && baData[1] == 0x50 && baData[2] == 0x4e &&
    							baData[3] == 0x47 && baData[4] == 0x0d && baData[5] == 0x0a &&
    							baData[6] == 0x1a && baData[7] == 0x0a) ? "image/png" : "image/jpg";
    						bFirstBlock = false;
    					}
    					Context.Response.OutputStream.Write(baData, 0, iBytesRead);
    				}
    				Context.Response.Flush();
    			}						}
     
    //  Close your own database connection and dispose of everything. DON'T HAVE A MEMORY LEAK
    			}
    		}
     
    		public bool IsReusable
    		{
    			get { return false; }
    		}
    	}

    The bit you'll need to change is either add more signatures (Excel to the list) or use another field in your data record to return the right ContentType for the type of Data Blob it is.
    Last edited by SirKilljoy; Mar 16, 2009 at 04:59. Reason: Typo

  5. #5
    SitePoint Zealot
    Join Date
    Jan 2009
    Posts
    176
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by kunalraj View Post
    if your doc extension is .xls than you have to add in that code like


    else if (ext.Equals("xls") || ext.Equals("XLS") )
    {
    Response.AddHeader("content-disposition", "attachment;filename=" +"+docname+"+ ".xls");
    Response.ContentType = "image/xls";// you can either write Response.ContentType = "blob/xls";
    Response.BinaryWrite((byte[])dr["doc"]);//[doc] is field blob.
    }
    ok kunalraj i vl try this..thanks.

  6. #6
    SitePoint Addict kunalraj's Avatar
    Join Date
    Nov 2008
    Posts
    200
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I am doing this by gridview and in gridview i have taken hiperlink.
    when administrator click on this the page call in which i have written that code.








    thanks,
    kunalraj

  7. #7
    SitePoint Zealot
    Join Date
    Jan 2009
    Posts
    176
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by kunalraj View Post
    I am doing this by gridview and in gridview i have taken hiperlink.
    when administrator click on this the page call in which i have written that code.

    thanks,
    kunalraj

    but i have not taken gridview....so i m taking hyperlink button in my table.
    Is there any problem if i m taking this in table?

  8. #8
    SitePoint Addict kunalraj's Avatar
    Join Date
    Nov 2008
    Posts
    200
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by yogita View Post
    but i have not taken gridview....so i m taking hyperlink button in my table.
    Is there any problem if i m taking this in table?
    i don`t think so there should be any problem.


    thanks.

  9. #9
    SitePoint Zealot
    Join Date
    Jan 2009
    Posts
    176
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    while (reader.Read())

    {
    //order like the select query

    lblAssetNo.Text = reader[0].ToString();
    lblDepartment.Text = reader[1].ToString();
    lblApplication.Text = reader[2].ToString();
    lblCategory.Text = reader[3].ToString();
    lblSubcategory.Text = reader[4].ToString();
    lblPriority.Text = reader[5].ToString();
    lblDescription.Text = reader[6].ToString();



    string docname = reader.GetString(7);
    string ext = reader.GetString(8);
    hyperlinkAttachment.Text = docname+"."+ ext;

    if (ext.Equals("jpg") || ext.Equals("jpeg") || ext.Equals("JPG") || ext.Equals("JPEG"))
    {
    Response.AddHeader("content-disposition", "attachment;filename=" + "+docname+" + ".jpg");
    Response.ContentType = "image/jpeg";
    Response.BinaryWrite((byte[])reader["doc"]);//[doc] is field blob.
    }


    else if (ext.Equals("xls") || ext.Equals("XLS") )
    {
    Response.AddHeader("content-disposition", "attachment;filename=" +"+docname+"+ ".xls");
    Response.ContentType = "image/xls";// you can either write Response.ContentType = "blob/xls";
    Response.BinaryWrite((byte[])reader["doc"]);//here m getting error indexoutofrange exception was unhandled by user code.
    }


    else if (ext.Equals("doc") || ext.Equals("doc"))
    {
    Response.AddHeader("content-disposition", "attachment;filename=" + "+docname+" + ".doc");
    Response.ContentType = "image/doc";
    Response.BinaryWrite((byte[])reader["doc"]);

    }

    m writing this code but index out of range exception error is coming when i m downloading the srs.xls.
    error is:-here m getting error indexoutofrange exception was unhandled by user code.

  10. #10
    SitePoint Addict kunalraj's Avatar
    Join Date
    Nov 2008
    Posts
    200
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hyperlinkAttachment.Text = docname+"."+ ext;
    Also create session for id(imageid which is autogenerated) and and ticketno.

    now
    onclick you have to call another page and in that page load event
    you have to write connection code
    after that you have to write query like
    "select doc,docext from tablename where id='"+num+"' and ticno='"+ticnum+"' ";
    here num and ticnum has value of session.
    now you have to write
    if (ext.Equals("jpg") || ext.Equals("jpeg") || ext.Equals("JPG") || ext.Equals("JPEG"))
    {
    Response.AddHeader("content-disposition", "attachment;filename=" + "+docname+" + ".jpg");
    Response.ContentType = "image/jpeg";
    Response.BinaryWrite((byte[])reader["doc"]);//[doc] is field blob.
    }


    thanks,
    kunalraj

    else if (ext.Equals("xls") || ext.Equals("XLS") )
    {
    Response.AddHeader("content-disposition", "attachment;filename=" +"+docname+"+ ".xls");
    Response.ContentType = "image/xls";// you can either write Response.ContentType = "blob/xls";
    Response.BinaryWrite((byte[])reader["doc"]);//here m getting error indexoutofrange exception was unhandled by user code.
    }


    else if (ext.Equals("doc") || ext.Equals("doc"))
    {
    Response.AddHeader("content-disposition", "attachment;filename=" + "+docname+" + ".doc");
    Response.ContentType = "image/doc";
    Response.BinaryWrite((byte[])reader["doc"]);

    }

  11. #11
    SitePoint Zealot
    Join Date
    Jan 2009
    Posts
    176
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by kunalraj View Post
    hyperlinkAttachment.Text = docname+"."+ ext;
    Also create session for id(imageid which is autogenerated) and and ticketno.

    now
    onclick you have to call another page and in that page load event
    you have to write connection code
    after that you have to write query like
    "select doc,docext from tablename where id='"+num+"' and ticno='"+ticnum+"' ";
    here num and ticnum has value of session.
    now you have to write
    if (ext.Equals("jpg") || ext.Equals("jpeg") || ext.Equals("JPG") || ext.Equals("JPEG"))
    {
    Response.AddHeader("content-disposition", "attachment;filename=" + "+docname+" + ".jpg");
    Response.ContentType = "image/jpeg";
    Response.BinaryWrite((byte[])reader["doc"]);//[doc] is field blob.
    }


    thanks,
    kunalraj

    else if (ext.Equals("xls") || ext.Equals("XLS") )
    {
    Response.AddHeader("content-disposition", "attachment;filename=" +"+docname+"+ ".xls");
    Response.ContentType = "image/xls";// you can either write Response.ContentType = "blob/xls";
    Response.BinaryWrite((byte[])reader["doc"]);//here m getting error indexoutofrange exception was unhandled by user code.
    }


    else if (ext.Equals("doc") || ext.Equals("doc"))
    {
    Response.AddHeader("content-disposition", "attachment;filename=" + "+docname+" + ".doc");
    Response.ContentType = "image/doc";
    Response.BinaryWrite((byte[])reader["doc"]);

    }
    Read commented line.

    string docname = reader.GetString(7);
    string ext = reader.GetString(8);
    hyperlinkAttachment.Text = docname+"."+ ext;//here m getting the docname and ext both like srs and .xls


    else if (ext.Equals("xls") || ext.Equals("XLS") )
    {
    Response.AddHeader("content-disposition", "attachment;filename=" +"+docname+"+ ".xls");//but now here m not getting the docname
    Response.ContentType = "image/xls";// you can either write Response.ContentType = "blob/xls";
    Response.BinaryWrite((byte[])reader["doc"]);//so here m getting error indexoutofrange exception was unhandled by user code.
    }

  12. #12
    SitePoint Zealot
    Join Date
    Jan 2009
    Posts
    176
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In which event u r telling?
    I have only 1 form named ManageIncidentDetail.
    In this page i have taken hyperlinkbutton(where i m displaying the srs.xls)
    and for displaying this i have written the code in this (manageincidnetdetail)pageload event itself.

  13. #13
    SitePoint Addict kunalraj's Avatar
    Join Date
    Nov 2008
    Posts
    200
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by yogita View Post
    Read commented line.

    string docname = reader.GetString(7);
    string ext = reader.GetString(8);
    hyperlinkAttachment.Text = docname+"."+ ext;//here m getting the docname and ext both like srs and .xls


    else if (ext.Equals("xls") || ext.Equals("XLS") )
    {
    Response.AddHeader("content-disposition", "attachment;filename=" +"+docname+"+ ".xls");//but now here m not getting the docname
    Response.ContentType = "image/xls";// you can either write Response.ContentType = "blob/xls";
    Response.BinaryWrite((byte[])reader["doc"]);//so here m getting error indexoutofrange exception was unhandled by user code.
    }
    Response.AddHeader("content-disposition", "attachment;filename=" +"+docname+"+ ".xls"); it is wrong

    for that you have to write query which i have told u before and arite like

    Response.AddHeader("content-disposition", "attachment;filename=" +docname+"+".xls");

  14. #14
    SitePoint Addict kunalraj's Avatar
    Join Date
    Nov 2008
    Posts
    200
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    For that you don`t have to write in same page

    you have to take another page. dnt take tention it will not show to user.
    it is only for open and download functionality.

    take another page write the code in it`s page load event.
    and on hyperlinkbutton you give url of that new page.
    first do this.


    thanks,
    kunalraj

  15. #15
    SitePoint Zealot
    Join Date
    Jan 2009
    Posts
    176
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by kunalraj View Post
    For that you don`t have to write in same page

    you have to take another page. dnt take tention it will not show to user.
    it is only for open and download functionality.

    take another page write the code in it`s page load event.
    and on hyperlinkbutton you give url of that new page.
    first do this.


    thanks,
    kunalraj
    ok let me try(

  16. #16
    SitePoint Zealot
    Join Date
    Jan 2009
    Posts
    176
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using BusinessLogic;
    using System.Data.OracleClient;

    public partial class _Default : System.Web.UI.Page
    {
    BusinessLogic.BLManageIncidentDetail objMID;
    BusinessLogic.ClsAllocatedIncident objCls;
    protected void Page_Load(object sender, EventArgs e)
    {
    BLManageIncidentDetail objMID = new BLManageIncidentDetail();
    ClsAllocatedIncident objCls = new ClsAllocatedIncident();

    string ticketid;
    string ext;
    string docname;
    string doc;
    ticketid = Session["TICKETID"].ToString();
    ext = Session["EXT"].ToString();
    docname = Session["DocumentName"].ToString();
    doc = Session["Doc"].ToString();
    OracleDataReader reader1 = objMID.GetDocument(ticketid);
    while (reader1.Read())
    {

    //hyperlinkAttachment.Text = docname;

    if (ext.Equals("jpg") || ext.Equals("jpeg") || ext.Equals("JPG") || ext.Equals("JPEG"))
    {
    Response.AddHeader("content-disposition", "attachment;filename=" + "+ docname +" + ".jpg");
    Response.ContentType = "blob/jpeg";
    Response.BinaryWrite((byte[])reader1["doc"]);//[doc] is field blob.
    }
    else if (ext.Equals("xls") || ext.Equals("XLS"))
    {
    Response.AddHeader("content-disposition", "attachment;filename=" + "+ docname +" + ".xls");
    Response.ContentType = "blob/xls";// you can either write Response.ContentType = "blob/xls";
    Response.BinaryWrite((byte[])reader1["doc"]);
    }
    else if (ext.Equals("doc") || ext.Equals("doc"))
    {
    Response.AddHeader("content-disposition", "attachment;filename=" + "+ docname +" + ".doc");
    Response.ContentType = "blob/doc";
    Response.BinaryWrite((byte[])reader1["doc"]);

    }
    }
    }
    }



    This is my new page which gets open when v click on hyperlink button.
    But still facing the same problem..Same error is cuming.. I dont know how to do this?
    I m passing this query in getdocument function.

    select document,document_ext,document_id,document_name from ims_document where incident_id='TIC085'.
    please help.

  17. #17
    SitePoint Addict kunalraj's Avatar
    Join Date
    Nov 2008
    Posts
    200
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    on your main page take one link button
    in that property postbackurl in this you select your new page.

    ok after in that page in pageload event you have to write code

  18. #18
    SitePoint Addict kunalraj's Avatar
    Join Date
    Nov 2008
    Posts
    200
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    protected void Page_Load(object sender, EventArgs e)
    {
    SqlConnection con = null;
    SqlCommand cmd = null;
    SqlDataReader dr = null;
    String ticnum = Session["TICKETID"].ToString();
    // also get docname by session. docname
    String ext;

    String connectionString = connection string
    con = new SqlConnection(connectionString);

    con.Open();
    String sql = "select doc,docext from demo where ticno='"+ticnum+"' ";
    cmd = new SqlCommand(sql, con);
    dr = cmd.ExecuteReader();
    while (dr.Read())
    {
    ext = dr.GetString(1);
    if (ext.Equals("jpg") || ext.Equals("jpeg") || ext.Equals("JPG") || ext.Equals("JPEG"))
    {
    Response.AddHeader("content-disposition", "attachment;filename=" + docname + ".jpg");
    Response.ContentType = "blob/jpeg";
    Response.BinaryWrite((byte[])dr["doc"]);
    }

    else if (ext.Equals("doc") || ext.Equals("DOC"))
    {
    Response.AddHeader("content-disposition", "attachment;filename=" +docname + ".doc");
    Response.ContentType = "blob/doc";
    Response.BinaryWrite((byte[])dr["doc"]);
    }

    else if (ext.Equals("XLS") || ext.Equals("xls"))
    {
    Response.AddHeader("content-disposition", "attachment;filename=" +docname + ".xls");
    Response.ContentType = "blob/xls";
    Response.BinaryWrite((byte[])dr["doc"]);
    }

    }
    dr.Close();
    }


    you have to write like this.

    ok still can`t understand ask me.

    thanks,
    kunalraj
    Last edited by kunalraj; Mar 19, 2009 at 21:43.

  19. #19
    SitePoint Addict kunalraj's Avatar
    Join Date
    Nov 2008
    Posts
    200
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If still u can`t understand ask me.






    thanks,
    kunalraj

  20. #20
    SitePoint Zealot
    Join Date
    Jan 2009
    Posts
    176
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by kunalraj View Post
    If still u can`t understand ask me.






    thanks,
    kunalraj
    thanks kunalraj..now it's working perfectly.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •