2

我们尝试实现一个 download.aspx 来控制我们的源,例如特定客户端的图像。我们使用download.aspx.cs中的缓冲方法。代码如下所示:

using (var fs = new FileStream(_path, FileMode.Open, FileAccess.Read))
{
    Response.BufferOutput = false;   // to prevent buffering 
    byte[] buffer = new byte[1024];
    int bytesRead = 0;
    if (_file.Extension == ".pdf")
    {
        Response.AddHeader("Content-Disposition", "inline; filename=" + _file.Name);
    }
    else
    {
        Response.AddHeader("Content-Disposition", "attachment; filename=" + _file.Name);
    }
    Response.AddHeader("Content-Length", _file.Length.ToString());
    Response.ContentType = ReturnExtension(_file.Extension.ToLower());

    while ((bytesRead = fs.Read(buffer, 0, buffer.Length)) > 0)
    {
        Response.OutputStream.Write(buffer, 0, bytesRead);
    }
}

下载单个文件时效果很好。但是,在我们的情况下,我们尝试同时加载大约 20 张图像。它变得非常缓慢。以下是捕获的屏幕:-

在此处输入图像描述

我们查不出原因。我们想知道这是一种控制文件的实用方法,或者还有其他更好的方法来实现它。

4

3 回答 3

0

Asp.net 的工作线程数量有限。当你做这样的事情时,你将这些线程上的负载增加了很多倍。

最好让 IIS 处理静态内容。

应该是一个文件请求,现在至少是来自您的屏幕截图的 17 个请求。这种负载会显着降低您的服务器速度。

于 2012-09-10T08:34:29.630 回答
0

我同意上述回复。但是,如果这是您“必须走”的路线。你可以看看以下内容。

您正在使用一个 asp.net 页面,而不是走 Handler 路线,您削减了很多 asp.net 生命周期,这将有助于减少您的图像加载时间。

其次看一下异步 HTTP 处理程序。

您还可以查看缓存响应输出,这将有助于提高性能。 读这个

我希望其中一些信息有所帮助。

于 2012-09-10T08:38:24.847 回答
0

我没有使用它,但是HttpModule类可能会为您提供一种让 IIS 静态地为您的文件提供服务的方法,同时让您以编程方式控制对这些文件的访问:

public class AccessControlModule : IHttpModule
{
    public void Init(HttpApplication context)
    {
        context.BeginRequest +=
            (s, e) =>
            {
                if (!AccessPermitted(context))
                    context.Response.Redirect(AccessDeniedUrl);

                // Otherwise, IIS will serve the file as normal
            };
    }

    //...
}

<httpModules>
   <add name="AccessControlModule" type="MyNamespace.AccessControlModule" />
</httpModules>

请参阅此处获取一些示例。

于 2012-09-11T06:17:50.100 回答