英文:
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实现了这一点:
标记:
<asp:TextBox id="TB_ExternalLink" runat="server"/>
<asp:LINKButton ID="LB_Open" runat="server" OnClick="OpenLink">Open Link</asp:LINKButton>
<input type="hidden" runat="server" id="hiddenExternalFile"/>
代码:
protected void OpenLink(object sender, EventArgs e)
{
string url = TB_ExternalLink.Text.Replace(@"\\", @"\\");
hiddenExternalFile.Value = url;
Page.ClientScript.RegisterStartupScript(this.GetType(), "CallMyFunction", "windowOpen()", true);
}
Javascript:
function windowOpen() {
Window = window.open(document.getElementById('hiddenExternalFile').value, "_blank");
}
在新标签页中打开任何以以下格式输入到文本框中的链接:
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:
<asp:TextBox id="TB_ExternalLink" runat="server"/>
<asp:LINKButton ID="LB_Open" runat="server" OnClick="OpenLink">Open Link</asp:LINKButton>
<input type="hidden" runat="server" id="hiddenExternalFile"/>
Code:
protected void OpenLink(object sender, EventArgs e)
{
string url = TB_ExternalLink.Text.Replace(@"\\", @"\\");
hiddenExternalFile.Value = url;
Page.ClientScript.RegisterStartupScript(this.GetType(), "CallMyFunction", "windowOpen()", true);
}
Javascript:
function windowOpen() {
Window = window.open(document.getElementById('hiddenExternalFile').value, "_blank");
}
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论