Stream.Read 总是返回 0

Stream.Read always returning 0

我们正在使用 asp.net 的 Asyncfileupload 控件来上传文件,同样不起作用我们已经调试并找到了这段代码:

input.Read(buffer, 0, buffer.Length)

返回 0。

整个代码是这样的:

public static byte[] ReadFully(Stream input)
    {
        byte[] buffer = new byte[input.Length];
        using (MemoryStream ms = new MemoryStream())
        {
            int read;
            while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
            {
                ms.Write(buffer, 0, read);
            }
            return ms.ToArray();
        }
    }

Stream.Read 可能 return 0 如果您位于字节数组的末尾或您的网络连接已断开。您可以尝试这行代码以确保:

byte[] buffer = new byte[input.Length];
input.Position=0;
        using (MemoryStream ms = new MemoryStream())
        {
            int read;
            while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
            {
                ms.Write(buffer, 0, read);
            }
            return ms.ToArray();
        }