C# 为了保持Request.InputStream的活动状态,是否让BinaryReader对象处于未处置状态?

C# 为了保持Request.InputStream的活动状态,是否让BinaryReader对象处于未处置状态?,c#,asp.net,inputstream,C#,Asp.net,Inputstream,我有两种方法可以使用Request.InputStream来处理图像的保存。我有两个共享HttpContext的扩展。在我的一个方法中,我使用BinaryReader读入内容并进行处理。但是,在处理BinaryReader时,它会根据请求关闭InputStream属性。我的第二个方法使用相同的输入流来创建缩略图 基本上,在第一个方法中处理读取器之后,我需要一种保持Request.InputStream属性活动的方法。这可能吗?以下是我的两种方法。首先调用SaveImageStream(),然后调

我有两种方法可以使用Request.InputStream来处理图像的保存。我有两个共享HttpContext的扩展。在我的一个方法中,我使用BinaryReader读入内容并进行处理。但是,在处理BinaryReader时,它会根据请求关闭InputStream属性。我的第二个方法使用相同的输入流来创建缩略图

基本上,在第一个方法中处理读取器之后,我需要一种保持Request.InputStream属性活动的方法。这可能吗?以下是我的两种方法。首先调用SaveImageStream(),然后调用GenerateThumbnail()

public static void SaveImageStream(此HttpContextBase ctx,字符串文件名)
{
var config=ObjectFactory.GetInstance();
使用(var reader=newbinaryreader(ctx.Request.InputStream))
{
var bandImagesPath=config.GetSetting(“BandImagePath”);
var path=path.Combine(ctx.Server.MapPath(bandImagesPath),文件名);
byte[]file=reader.ReadBytes((int)ctx.Request.InputStream.Length);
使用(var outputStream=System.IO.File.Create(路径2048))
{
常量int chunkSize=2*1024;//2KB
字节[]缓冲区=新字节[chunkSize];
int字节读取;
ctx.Request.InputStream.Position=0;
而((bytesRead=ctx.Request.InputStream.Read(buffer,0,buffer.Length))>0)
{
写入(缓冲区,0,字节读取);
}
}
}
}
公共静态void GenerateThumbnail(此HttpContextBase ctx,字符串文件名)
{
var config=ObjectFactory.GetInstance();
int size=config.GetSetting(“ThumbSize”);
var thumbPath=Path.Combine(ctx.Server.MapPath(config.GetSetting(“thumbPath”)),文件名);
var image=System.Drawing.image.FromStream(ctx.Request.InputStream);
var thumb=image.GetThumbnailImage(大小,大小,null,IntPtr.Zero);
保存(thumbPath,System.Drawing.Imaging.ImageFormat.Png);
}
您可以使用“decorator”模式包装输入流。看看这篇文章末尾的例子:


通过从一个方法调用另一个方法,您可以使用语句在
中执行所有操作。我还想知道这句话:

byte[] file = reader.ReadBytes((int)ctx.Request.InputStream.Length);
您没有在任何地方使用
文件
变量,该变量将整个请求流驻留在内存中。如果您不小心,这将成为拒绝服务攻击的途径。但在解决方案上

将缩略图方法更改为如下所示:

public static void SaveImageStream(this HttpContextBase ctx, string filename)
{
    var config = ObjectFactory.GetInstance<IConfig>();

    using (var reader = new BinaryReader(ctx.Request.InputStream))
    {
        var bandImagesPath = config.GetSetting<string>("BandImagePath");
        var path = Path.Combine(ctx.Server.MapPath(bandImagesPath), filename);

        using (var outputStream = System.IO.File.Create(path, 2048))
        {
            const int chunkSize = 2 * 1024; // 2KB
            byte[] buffer = new byte[chunkSize];
            int bytesRead;
            ctx.Request.InputStream.Position = 0;
            while ((bytesRead = ctx.Request.InputStream.Read(buffer, 0, buffer.Length)) > 0)
            {
                outputStream.Write(buffer, 0, bytesRead);
            }
        }

        ctx.Request.InputStream.Position = 0;
        ctx.GenerateThumbnail(filename);
    }
}
public static void SaveImageStream(此HttpContextBase ctx,字符串文件名)
{
var config=ObjectFactory.GetInstance();
使用(var reader=newbinaryreader(ctx.Request.InputStream))
{
var bandImagesPath=config.GetSetting(“BandImagePath”);
var path=path.Combine(ctx.Server.MapPath(bandImagesPath),文件名);
使用(var outputStream=System.IO.File.Create(路径2048))
{
常量int chunkSize=2*1024;//2KB
字节[]缓冲区=新字节[chunkSize];
int字节读取;
ctx.Request.InputStream.Position=0;
而((bytesRead=ctx.Request.InputStream.Read(buffer,0,buffer.Length))>0)
{
写入(缓冲区,0,字节读取);
}
}
ctx.Request.InputStream.Position=0;
ctx.GenerateThumbnail(文件名);
}
}

或者,您可以在
文件
属性周围使用MemoryStream,并将其发送到
GenerateThumbnail
扩展方法。

在-
NonClosingStreamRapper
或类似方法中也有一个完全实现的装饰程序。
public static void SaveImageStream(this HttpContextBase ctx, string filename)
{
    var config = ObjectFactory.GetInstance<IConfig>();

    using (var reader = new BinaryReader(ctx.Request.InputStream))
    {
        var bandImagesPath = config.GetSetting<string>("BandImagePath");
        var path = Path.Combine(ctx.Server.MapPath(bandImagesPath), filename);

        using (var outputStream = System.IO.File.Create(path, 2048))
        {
            const int chunkSize = 2 * 1024; // 2KB
            byte[] buffer = new byte[chunkSize];
            int bytesRead;
            ctx.Request.InputStream.Position = 0;
            while ((bytesRead = ctx.Request.InputStream.Read(buffer, 0, buffer.Length)) > 0)
            {
                outputStream.Write(buffer, 0, bytesRead);
            }
        }

        ctx.Request.InputStream.Position = 0;
        ctx.GenerateThumbnail(filename);
    }
}