如何根据文件名设置浏览器的标签名 [英] how to set the tab name of a browser based on file name

查看:150
本文介绍了如何根据文件名设置浏览器的标签名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好

我试图在单击时在浏览器的新选项卡中显示文件(图像).我设法在新选项卡上将其打开.现在,我希望该选项卡标题成为文件名,..正在粘贴到目前为止已完成的代码...

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...

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

推荐答案

根据您的条件,浏览器选项卡的标题将仅是图像的名称.尝试检查图像文件的名称.
如果要动态生成任何HTML页面,然后尝试在该页面中显示图像,则必须在这种情况下设置Title.

--Amit
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


这篇关于如何根据文件名设置浏览器的标签名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆