我使用文件上传选项上传文件。我直接发送这个文件从View到控制器在POST方法like,
[HttpPost] public ActionResult Page2(FormCollection objCollection) { HttpPostedFileBase file = Request.Files[0]; }
假设,我上传一个记事本文件。如何读取此文件&将此文本附加到字符串构建器,而不保存该文件….@H_404_5@
我知道后SaveAs这个文件,我们可以读这个文件。但是如何读取这个文件从HttpPostedFileBase没有保存?@H_404_5@
解决方法
这可以使用httpPostedFileBase类返回HttpInputStreamObject根据指定的
here
您应该将流转换为字节数组,然后您可以读取文件内容@H_404_5@
http://msdn.microsoft.com/en-us/library/system.web.httprequest.inputstream.aspx]@H_404_5@
希望这可以帮助@H_404_5@
更新:@H_404_5@
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.@H_404_5@
// 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);