从 File.OpenRead() 返回流
我正在编写一个 WCF 服务,该服务将允许 ASP.Net 网站检索文件(基于 这篇文章)。我的问题是,当我返回流时,它是空白的。
为简单起见,我将代码隔离到一个简单的 winforms 应用程序中,以尝试找出返回流的问题所在,这就是代码:
private Stream TestStream()
{
Stream fs = File.OpenRead(@"c:\testdocument.docx");
return fs;
}
// This method converts the filestream into a byte array so that when it is
// used in my ASP.Net project the file can be sent using response.Write
private void Test()
{
System.IO.MemoryStream data = new System.IO.MemoryStream();
System.IO.Stream str = TestStream();
str.CopyTo(data);
byte[] buf = new byte[data.Length];
data.Read(buf, 0, buf.Length);
}
此代码的结果是 buf
是 12,587 字节long(文件的正确长度),但它只包含 0。
如果我尝试打开 Word 文档,则不会出现任何问题,我是否遗漏了一些明显的内容?
I'm in the process of writing a WCF service that will allow an ASP.Net web site to retrieve files (based on this article). My problem is that when I return the stream, it's blank.
For simplicity, I've isolated the code into a simple winforms app to try and find what the problem is with returning a stream and this is the code:
private Stream TestStream()
{
Stream fs = File.OpenRead(@"c:\testdocument.docx");
return fs;
}
// This method converts the filestream into a byte array so that when it is
// used in my ASP.Net project the file can be sent using response.Write
private void Test()
{
System.IO.MemoryStream data = new System.IO.MemoryStream();
System.IO.Stream str = TestStream();
str.CopyTo(data);
byte[] buf = new byte[data.Length];
data.Read(buf, 0, buf.Length);
}
The result of this code is that buf
is 12,587 bytes long (the correct length of the file) but it just contains 0's.
The Word document opens without problems if I try it, am I missing something obvious?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
尝试将您的代码更改为:
Try changing your code to this:
您忘记了
Seek
:You forgot to
Seek
:选项:
data.Seek
使用更简单的
Position
属性:使用
MemoryStream
中的ToArray
调用让您的生活更简单首先:第三个选项是我的首选方法。
请注意,您应该有一个
using
语句来自动关闭文件流(以及可选的MemoryStream
),并且我会为System 添加一个 using 指令。 IO
使代码更简洁:您可能还想在 Stream 上创建一个扩展方法,以便在一个地方为您执行此操作,例如
请注意,这不会 关闭输入流。
Options:
data.Seek
as suggested by ken2kUse the somewhat simpler
Position
property:Use the
ToArray
call inMemoryStream
to make your life simpler to start with:The third option would be my preferred approach.
Note that you should have a
using
statement to close the file stream automatically (and optionally for theMemoryStream
), and I'd add a using directive forSystem.IO
to make your code cleaner:You might also want to create an extension method on
Stream
to do this for you in one place, e.g.Note that this doesn't close the input stream.
您忘记重置内存流的位置:
更新:
还有一件事需要注意:不忽略方法的返回值通常是值得的。更健壮的实现应该检查调用返回后已读取了多少字节:
You forgot to reset the position of the memory stream:
Update:
There is one more thing to note: It usually pays not to ignore the return values of methods. A more robust implementation should check how many bytes have been read after the call returns:
由于您
的
Test()
方法正在模仿客户端,因此它应该Close()
或Dispose()
的str< /代码> 流。内存流也是如此,只是不符合原则。
You need
And since your
Test()
method is imitating the client it ought toClose()
orDispose()
thestr
Stream. And the memoryStream too, just out of principal.