Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi guys

am trying to display a file(image) in a new tab of a browser when clicked.. I managed to get it opened on a new tab. Now i want that tab title to be the file name is there any way.. am pasting the code i hav done so far here...

C#
public string DownloadAttachment()
        {
            string filepath = new FileInfo(HttpContext.Current.Server.MapPath("Ticket")).FullName +"\\"+ GetPageParams<string>("hdnAttachmentID");          
            string fileExtension = Path.GetExtension(GetPageParams<string>("hdnAttachmentID"));            
            string viewname = "TicketUpdateDetailView";
            try
            {
                HttpContext.Current.Response.Clear();
                HttpContext.Current.Response.WriteFile(filepath);
                switch (fileExtension)
                {


                    case ".jpg":
                    case ".jpeg":
                        {
                            HttpContext.Current.Response.ContentType = "image/jpeg";
                            HttpContext.Current.Response.AppendHeader("Content-disposition", "inline;filename=" + Path.GetFileName(filepath));
                            break;
                        }
                    case ".bmp":
                        {
                            HttpContext.Current.Response.ContentType = "image/bmp";
                            HttpContext.Current.Response.AppendHeader("Content-disposition", "inline;filename=" + Path.GetFileName(filepath));
                            break;
                        }
                    case ".gif":
                        {
                            HttpContext.Current.Response.ContentType = "image/gif";
                            HttpContext.Current.Response.AppendHeader("Content-disposition", "inline;filename=" + Path.GetFileName(filepath));
                            break;
                        }
                    case ".png":
                        {
                            HttpContext.Current.Response.ContentType = "image/png";
                            HttpContext.Current.Response.AppendHeader("Content-disposition", "inline;filename=" + Path.GetFileName(filepath));
                            break;
                        }
                    default:
                        {
                            HttpContext.Current.Response.ContentType = "application/octet-stream";
                            HttpContext.Current.Response.AppendHeader("content-disposition", "attachment;filename=" + Path.GetFileName(filepath));
                            break;
                        }
                }
                //HttpContext.Current.Response.ContentType = "application/octet-stream";
                //HttpContext.Current.Response.AppendHeader("content-disposition", "attachment; filename=" + Path.GetFileName(fileFullPath));                               
                //var pageTitle = (Page)HttpContext.Current.Handler;                
                //pageTitle.Title=Path.GetFileName(fileFullPath);
                HttpContext.Current.Response.End();
                viewname = "TicketUpdateDetailView";
            }
            catch (System.Threading.ThreadAbortException ex1)
            {

            }
            catch (Exception ex)
            {

            }
            return viewname;
        }


If there is anything wrong with my code please do correct me ....


Thanks in advance
Posted
Updated 1-Aug-12 23:54pm
v2

1 solution

As per your condition, the title of the browser tab will be the name of the image only. Try checking the name of your image file.
If you are generating any HTML page dynamically and then trying to display the image in that page, you'll have to set the Title in that case.

--Amit
 
Share this answer
 
Comments
Arjun Menon U.K 2-Aug-12 6:06am    
how can i set title Amit ??? Am amateur in this field
Arjun Menon U.K 2-Aug-12 6:07am    
can u provide some code
_Amy 2-Aug-12 6:13am    
Page.Title="My Title";
Arjun Menon U.K 2-Aug-12 6:17am    
Page represents an aspx page know? and am creating a html page so ???
_Amy 2-Aug-12 6:23am    
Can you give me the code for creation HTML page? Or just try to append Title in head tag while creating the HTML.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900