我使用文件上传选项上传文件。我直接发送这个文件从View到控制器在POST方法like,
[HttpPost] public ActionResult Page2(FormCollection objCollection) { HttpPostedFileBase file = Request.Files[0]; }
假设,我上传一个记事本文件。如何读取此文件&将此文本附加到字符串构建器,而不保存该文件….
我知道后SaveAs这个文件,我们可以读这个文件。但是如何读取这个文件从HttpPostedFileBase没有保存?
解决方法
这可以使用httpPostedFileBase类返回HttpInputStreamObject根据指定的
here
您应该将流转换为字节数组,然后您可以读取文件内容
请参考以下链接
http://msdn.microsoft.com/en-us/library/system.web.httprequest.inputstream.aspx]
希望这可以帮助
更新:
The stream that you get from your HTTP call is read-only sequential
(non-seekable) and the FileStream is read/write seekable. You will
need first to read the entire stream from the HTTP call into a byte
array,then create the FileStream from that array.
摘自here
// Read bytes from http input stream BinaryReader b = new BinaryReader(file.InputStream); byte[] binData = b.ReadBytes(file.InputStream.Length); string result = System.Text.Encoding.UTF8.GetString(binData);
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。