哈希特殊字符在IFRAME中不起作用

问题描述

| Request.QueryString包含路径 例如。 Temp \\ file#hashName.jpg
protected void Page_Load(object sender,EventArgs e)
{
string filePath = Request.QueryString[\"fileName\"];
iframes.Attributes.Add(\"src\",filePath);
}
它可以去我写过JavaScript代码标记
function ViewFile(filePath) {
    var width = 800;
    var height = 450;
    var left = (screen.width / 2) - (width / 2);
    var top = (screen.height / 2) - (height / 2);
    window.open(\'ViewFile.aspx?fileName=\' + filePath,\'CustomPopUp\',\'width=\' + width + \',height=\' + height + \',toolbar=no,menubar=no,directories=no,resizable=yes,scrollbars=yes,location=no,top=\' + top + \',left=\' + left);
    return false;
}
但是当我跑步时,它会显示我(在新窗口中) \“没有找到您要查的资源。 \” 说明:HTTP404。您正在寻找的资源(或其依赖项之一)可能已被删除名称更改或暂时不可用。请查看以下网址,并确保其拼写正确。 要求的网址:/ Temp / file 一旦遇到哈希,就会中断路径... 我使用了Server.URLEncode和HttpUtility.UrlEncode();其将/和#转换为各自的值... bt显示消息 HTTP错误400-错误的请求。 Javascript Escape()EncodeURI()和EncodeURI组件也无法正常工作。 我该怎么做才能逃脱#?? iframe是否接受URL中的#值???请通过这个指导我。 亲切的问候, 哈迪克     

解决方法

        ViewFile js函数从哪里获取输入filePath?问题可能与此有关。如果filePath中包含#,则可以使用
encodeURIComponent
,例如
\'ViewFile.aspx?fileName=\' + encodeURIComponent(filePath)
但是,如果函数的
filePath
值不正确,则会出现问题,因为其他地方(例如IFrame源)进行了编码问题。每当您在URL / QueryString中使用#时,都可能引起问题-因此,在将其分配给IFrame源时,您也应尝试对其进行转义。