ASP.Net 目录中的文件列表 +链接到文件

发布于 2024-11-07 11:42:58 字数 481 浏览 0 评论 0 原文

我正在创建一个 Web 表单,它将显示目录中的异常文件列表。文件显示正常,但是链接不起作用。我已经快速搜索了一个解决方案,唯一的问题是大多数解决方案要求我设置一个虚拟目录,但是这些文件所在的服务器不是 Web 服务器。这是列出文件的代码:

var exDir = @"\\Server\folder\Exception";
        exLabel.Text = "";
        foreach (string exFile in Directory.GetFiles(exDir))
        {
            exLabel.Text += @"<a href='file:"+exFile+"'> "+exFile+" </a><br/>";
        }

问题出在我的“href”中。有没有什么方法可以设置此链接而无需设置虚拟目录?或者,如果我必须设置一个,可以通过 IIS Express 进行吗?

I am creating a web form which will display a list of exception files from a directory. The files display properly, however, the link doesn't work. I've done some quick searches for a solution, only issue is most solutions request I setup a virtual directory, however the server these files are located on is not a web server. Here is the code which lists the files:

var exDir = @"\\Server\folder\Exception";
        exLabel.Text = "";
        foreach (string exFile in Directory.GetFiles(exDir))
        {
            exLabel.Text += @"<a href='file:"+exFile+"'> "+exFile+" </a><br/>";
        }

The issue lies within my "href". Is there some way to setup this link without having to setup a virtual directory? Or if I have to setup one, do so via IIS Express?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

不羁少年 2024-11-14 11:42:58

如果文件与 Web 服务器不在同一服务器上,则如果没有虚拟目录,则无法执行此操作。这些文件需要通过网络服务器提供给客户端。

虽然您可以使用 IIS Express 创建虚拟目录 - 请查看此讨论主题。您可能还需要启用对 IIS Express 的外部访问 (WebMatrix 上的这篇文章 在这方面应该会有所帮助)。注意:使用虚拟目录时,您的 URL 需要使用 http:https: 方案,而不是 file:

另一种方法是将要共享的文件上传到 Web 服务器上的某个位置,并从 Web 服务器提供它们。

You cannot do this without a virtual directory if the files do not reside on the same server as the web server. The files need to be served to clients via a web server.

While you can use IIS Express to create virtual directories - have a look at this discussion thread. You may also need to enable external access to IIS Express (this post on WebMatrix should be helpful in this regard). Note: when using a virtual directory, your URLs will need to use the http: or https: scheme instead of file:.

Another approach would be to upload the files you want to share to a location on the web server and serve them from web server.

各空 2024-11-14 11:42:58

如果引用本地文件系统,则需要按如下格式设置超链接:

file:///c:/myfile.txt

If referencing the local file system, you need to format hyperlinks as follows:

file:///c:/myfile.txt

煮酒 2024-11-14 11:42:58

我认为您可以使用下载器服务器端来实现此目的,该服务器端可以为您访问文件,然后通过 http 提供它们。

一个 httphandler,其 ProcessRequest 方法可以(非常简化)如下所示:

public void ProcessRequest(HttpContext context)
{
    if (context.Request.Params["file"] != null)
    {   
            string filename = context.Request.Params["file"].ToString();

        context.Response.Clear();
        context.Response.ClearContent();
        context.Response.ClearHeaders();
        context.Response.Buffer = true;

        FileInfo fileInfo = new FileInfo(filename);

        if (fileInfo.Exists)
        {
            context.Response.ContentType = /* your mime type */;
            context.Response.AppendHeader("content-disposition", string.Format("attachment;filename={0}", fileInfo.Name));
            context.Response.WriteFile(filename);
        }

        context.Response.End();
    }   
}

然后您将构建链接以指向您的处理程序,并将文件作为参数:

var exDir = @"\\Server\folder\Exception";
DirectoryInfo dir = new DirectoryInfo(exDir);

foreach (FileInfo exFile in dir.GetFiles())
{
    exLabel.Text += @"<a href='downloader.ashx?file="+ exFile.Name + "'> "+exFile.FullName+" </a><br/>";
}

记住在 web.config 中设置处理程序:(

<system.web>
    <httpHandlers>
        ...
        <add verb="*" path="downloader.ashx" type="YourNamespace.downloader"/>
    </httpHandlers>
</system.web>

当然这个示例非常简单,我认为充满错误,但只是为了澄清方法)

I think you can achieve this using a downloader server side, that can access the files for you then serve them through http.

An httphandler, which ProcessRequest method could be (very semplified) like this:

public void ProcessRequest(HttpContext context)
{
    if (context.Request.Params["file"] != null)
    {   
            string filename = context.Request.Params["file"].ToString();

        context.Response.Clear();
        context.Response.ClearContent();
        context.Response.ClearHeaders();
        context.Response.Buffer = true;

        FileInfo fileInfo = new FileInfo(filename);

        if (fileInfo.Exists)
        {
            context.Response.ContentType = /* your mime type */;
            context.Response.AppendHeader("content-disposition", string.Format("attachment;filename={0}", fileInfo.Name));
            context.Response.WriteFile(filename);
        }

        context.Response.End();
    }   
}

then you'll build the link to point you handler with the file as param:

var exDir = @"\\Server\folder\Exception";
DirectoryInfo dir = new DirectoryInfo(exDir);

foreach (FileInfo exFile in dir.GetFiles())
{
    exLabel.Text += @"<a href='downloader.ashx?file="+ exFile.Name + "'> "+exFile.FullName+" </a><br/>";
}

Remember to setup the handler in the web.config:

<system.web>
    <httpHandlers>
        ...
        <add verb="*" path="downloader.ashx" type="YourNamespace.downloader"/>
    </httpHandlers>
</system.web>

(Of course this sample is very simple and I think full of errors, but is just to clarify the way)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文