stream.CopyTo - 文件空。 asp.net [英] stream.CopyTo - file empty. asp.net

查看:171
本文介绍了stream.CopyTo - 文件空。 asp.net的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用这个code保存上传图片:

I'm saving an uploaded image using this code:

using (var fileStream = File.Create(savePath))
{
   stream.CopyTo(fileStream);
}

当图像保存到目标文件夹,它是空的,0 KB。什么能可能是错误的吗?我抄袭其不为空之前已经检查了stream.Length。

When the image is saved to its destination folder, it's empty, 0 kb. What could possible be wrong here? I've checked the stream.Length before copying and its not empty.

推荐答案

有什么不对您的code。你说的事实使我怀疑复制之前的流位置的我抄袭其不为空之前检查stream.Length。

There is nothing wrong with your code. The fact you say "I've checked the stream.Length before copying and its not empty" makes me wonder about the stream position before copying.

如果你已经消耗的源流一次,然后虽然流不是长度为零,其位置可能是在流的末尾 - 所以没有什么留下来复制

If you've already consumed the source stream once then although the stream isn't zero length, its position may be at the end of the stream - so there is nothing left to copy.

如果流是可搜索(这将是一个的MemoryStream 的FileStream 和许多其他人),尝试把

If the stream is seekable (which it will be for a MemoryStream or a FileStream and many others), try putting

stream.Position = 0

只是复制前。这将重置流位置的开头,这意味着整个流会被你的code被复制。

just before the copy. This resets the stream position to the beginning, meaning the whole stream will be copied by your code.

这篇关于stream.CopyTo - 文件空。 asp.net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆