你可以在不编码的情况下使用Response.Redirect吗?

huangapple go评论75阅读模式
英文:

Can I use Response.Redirect without encoding?

问题

我想实现通过浏览器使用C#中的response.redirect函数打开由URL或网络驱动器描述的文件 (N:\ESSBP1-Office\Imagenes\Check list.xlsx)。<br/>
但是: 只有当路径不包含空格时,这才对我有效。

我如何避免编码?如果我在InternetExplorer中键入相同的路径,带有空格的地址会被正确解析,并打开文件。

英文:

I want to achieve that a file that is described by URL or network drive (N:\ESSBP1-Office\Imagenes\Check list.xlsx) can be opened through a browser by using the resonse.redirect function in c#. <br/>
BUT: This only works for me if the path doesn't contain a space.

How can I avoid the encoding? If I type that same path into the InternetExplorer the address - with space - is resolved correctly and the file is opened.

答案1

得分: 0

我没有成功避免使用response.redirect函数的编码。Server.UrlEncode()也没有做到这一点,因为它只会添加另一种不需要的编码,而我希望文本框/标签的确切内容成为传递给浏览器URL栏的字符串,然后按回车键 - 这应该通过代码完成。

最终,我通过遵循@Richard Housham的建议,使用Javascript实现了这一点:

标记:

&lt;asp:TextBox id=&quot;TB_ExternalLink&quot; runat=&quot;server&quot;/&gt;
&lt;asp:LINKButton ID=&quot;LB_Open&quot; runat=&quot;server&quot; OnClick=&quot;OpenLink&quot;&gt;Open Link&lt;/asp:LINKButton&gt;
&lt;input type=&quot;hidden&quot; runat=&quot;server&quot; id=&quot;hiddenExternalFile&quot;/&gt;

代码:

protected void OpenLink(object sender, EventArgs e)
{
    string url = TB_ExternalLink.Text.Replace(@&quot;\\&quot;, @&quot;\\&quot;);
    hiddenExternalFile.Value = url;
    Page.ClientScript.RegisterStartupScript(this.GetType(), &quot;CallMyFunction&quot;, &quot;windowOpen()&quot;, true);
}

Javascript:

function windowOpen() { 
        Window = window.open(document.getElementById(&#39;hiddenExternalFile&#39;).value, &quot;_blank&quot;); 
}

在新标签页中打开任何以以下格式输入到文本框中的链接:

http://www.url.com
\\corp.root.int\INT-Host$\USR03\XXX\W10\Desktop\Kaizen MRO\RMA_Part.png
N:\ESSBP1-Office\Imagenes\Check list.xlsx

无论空格或特殊ASCII字符如何。

英文:

I didn't manage to avoid the encoding with the response.redirect function. Server.UrlEncode() didn't do that neither, as it just adds another unwanted encoding, while I wanted the exact content of a TextBox/Label to be the string passed to the browser's URL-bar, followed by hitting the enter key - which should be done through code.

I finally achieved that, following @Richard Housham´s suggestion using Javascript:

Mark-up:

&lt;asp:TextBox id=&quot;TB_ExternalLink&quot; runat=&quot;server&quot;/&gt;
&lt;asp:LINKButton ID=&quot;LB_Open&quot; runat=&quot;server&quot; OnClick=&quot;OpenLink&quot;&gt;Open Link&lt;/asp:LINKButton&gt;
&lt;input type=&quot;hidden&quot; runat=&quot;server&quot; id=&quot;hiddenExternalFile&quot;/&gt;

Code:

protected void OpenLink(object sender, EventArgs e)
{
    string url = TB_ExternalLink.Text.Replace(@&quot;\\&quot;, @&quot;\\&quot;);
    hiddenExternalFile.Value = url;
    Page.ClientScript.RegisterStartupScript(this.GetType(), &quot;CallMyFunction&quot;, &quot;windowOpen()&quot;, true);
}

Javascript:

function windowOpen() { 
        Window = window.open(document.getElementById(&#39;hiddenExternalFile&#39;).value, &quot;_blank&quot;); 
}

Opens any link that is typed into the textbox in these formats in a new tab:

http://www.url.com
\\corp.root.int\INT-Host$\USR03\XXX\W10\Desktop\Kaizen MRO\RMA_Part.png
N:\ESSBP1-Office\Imagenes\Check list.xlsx

regardless of spaces or special ASCI characters

huangapple
  • 本文由 发表于 2020年1月3日 22:10:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/59580012.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定