当文件名有空格时,如何获取href链接以正确下载文件。 [英] When a file name has spaces how do I get the href link to download the file correctly.

查看:117
本文介绍了当文件名有空格时,如何获取href链接以正确下载文件。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

除非文件名中包含空格,否则代码可以正常工作。最初链接只列出服务器和共享名称,并删除文件名,因为它有空格。这是通过使用path.replace来解决的。所以现在链接显示正确,但是当点击链接时,文件将无法正确下载,除非%20被转换回空格。



那么如何更换带有%20的空格,以便链接正确显示但是当用户点击链接时,%20会被替换回空格?



Ex。 \\Server\Share\my file.docx会导致链接仅显示file:// Server / Share,这会导致链接无法使用。



添加替换部分导致链接显示文件://Server/Share/my%2520file.docx但是当链接显示时我收到一条无法显示的消息点击。如果我手动将%2520替换回空格,则按预期显示打开/保存对话框。



再次,如果文件名没有空格,则进程正常。



仅供参考,这是在内联网上,我目前正在使用IE浏览器。



我尝试过:



< pre> string Path =\\\\\\\\\\\\\+ ddlDept.SelectedValue +\\+ ddlWI.SelectedValue; 
if(File.Exists(Path))
{
Path = Path.Replace(,%20);
Label1.Text =点击此链接检索您的文件,< a href =+ Path +> ClickMe< / a>;
}

解决方案

也许这太明显了,但是没有规则说明链接的路径和文本显示必须相同(或彼此有任何关系)。



因此,当您用人类标记链接时,创建带有链接值的链接 - 可读版本。



用引号括住属性值:

 string Path = \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ 
if(File.Exists(Path))
{
Label1.Text =点击此链接检索你的文件,< a href = \+ Path +\\ \\ > ClickMe< / A> 中;
}



注意:如果用户与网络服务器位于同一网络,则用户将只能访问该链接


The code works unless the filename has spaces in it. Originally the link would only list the server and share name and would drop the filename since it had spaces. This was fixed by utilizing the path.replace. So now the link displays correctly, however when the link is clicked the file will not download correctly unless the %20 is converted back to spaces.

So how can I replace the spaces with %20 so the link displays correctly yet have the %20 be replaced back to spaces when the user clicks the link?

Ex. \\Server\Share\my file.docx would cause the link to only display file://Server/Share which renders the link unusable.

Adding the replace portion caused the link to display file://Server/Share/my%2520file.docx but I receive a cannot be displayed message when the link is clicked. If I manually replace the %2520 back to a space then the open/save dialog box appears as expected.

Again, the process works fine if the filename has no spaces.

FYI, this is on an intranet and I am currently using IE.

What I have tried:

<pre> string Path = "\\\\server\\share\\" + ddlDept.SelectedValue + "\\" + ddlWI.SelectedValue;
            if (File.Exists(Path))
            {
                Path = Path.Replace(" ", "%20");
                Label1.Text = "Click on this link to retrieve your file, <a href=" + Path + ">ClickMe</a>";
            }

解决方案

Perhaps this is too obvious, but there's no rule that says the path for a link and the text it displays have to be identical (or have anything to do with one another).

So, create the links with the link value while you label the link with the human-readable version.


Surround the attribute value with quotes:

string Path = "\\\\server\\share\\" + ddlDept.SelectedValue + "\\" + ddlWI.SelectedValue;
if (File.Exists(Path))
{
    Label1.Text = "Click on this link to retrieve your file, <a href=\"" + Path + "\">ClickMe</a>";
}


NB: The user will only be able to access the link if they are on the same network as the web server.


这篇关于当文件名有空格时,如何获取href链接以正确下载文件。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆