简体   繁体   English

C#将FileStream复制到HttpPostedFileBase

[英]C# Copy FileStream to HttpPostedFileBase

I am having problem to copy FileStream to HttpPostedFileBase. 我在将FileStream复制到HttpPostedFileBase时遇到问题。 My code is something like this. 我的代码是这样的。

HttpPostedFileBase postedFile = null;    
FileStream f = new FileStream(Server.MapPath("Path"), FileMode.Open, FileAccess.Read);
postedFile.InputStream.CopyTo(f);
f.Close();

However I can read file but having error when try to copy f to postedFile. 但是我可以读取文件,但是在尝试将f复制到postedFile时出错。 Please correct me if I am doing wrong or any problem in this code block. 如果我在此代码块中做错了或有任何问题,请纠正我。 Furthermore, I am getting this exception: 此外,我收到此异常:

"System.NullReferenceException: Object reference not set to an instance of an object." “ System.NullReferenceException:对象引用未设置为对象的实例。”

Thanks in advance! 提前致谢!

您的postedFile为null-这就是为什么您会得到null引用异常的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM