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

问题描述

大家好

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

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

推荐答案


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

08-20 13:33
查看更多