在网页中打开文件,并且文件内容不可选择,没有工具栏等

string url;
string temp1;
temp1=temp.Replace("//", "////");                    
url = "javascript:window.open('html.ashx?path=' + encodeURIComponent('" + temp1 + "'),/"newwindow/", /"toolbar= no, menubar=no, scrollbars=yes, resizable=yes, location=no, status=no, /")";

hyFolder.NavigateUrl = url;

 

其中hyFolder是一个hyperlink控件,其text是文件名称,

html.ashx中的代码如下:

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

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


public class html : IHttpHandler, IRequiresSessionState
{
   
    public void ProcessRequest (HttpContext context)
    {
        HttpResponse Response = context.Response;
        //绝对路径
        string path = HttpContext.Current.Session["chiefPath"].ToString() + "//" + context.Request.QueryString["path"];
        //Response.Write(HttpUtility.UrlDecode(path,System.Text.Encoding.Default));
        //Response.End();
        //文件名称
        string filename = Path.GetFileName(path);
        string filenameHtml = Path.GetFileNameWithoutExtension(filename) + ".htm";
        string fileexten = Path.GetExtension(filename);//文件扩展名

        //以字符流的形式下载文件
        FileStream fs = new FileStream(path, FileMode.Open);
        byte[] bytes = new byte[(int)fs.Length];
        fs.Read(bytes, 0, bytes.Length);
        fs.Close();

      
            //将 byte[]类型转换成string类型
            string strbytes = System.Text.Encoding.Default.GetString(bytes);
            string body = "";
            if (strbytes.Contains(""))
            {
                strbytes = strbytes.Replace("", body);
            }
            else if (strbytes.Contains(""))
            {
                strbytes = strbytes.Replace("", body);
            }
            byte[] newbytes = new byte[strbytes.Length];
            newbytes = System.Text.Encoding.Default.GetBytes(strbytes);

            Response.ContentType = "application/octet-stream";
            //通知浏览器下载文件而不是打开
            Response.AddHeader("Content-Disposition", "inline;  filename=" + HttpUtility.UrlEncode(filename, System.Text.Encoding.UTF8));
            Response.BinaryWrite(newbytes);
            Response.Flush();
            Response.End();

    }
 
    public bool IsReusable {
        get {
            return false;
        }
    }

}

你可能感兴趣的:(工具,string,hyperlink,byte,path,javascript)