ASP.Net MVC – 从HttpPostedFileBase读取文件,而不保存

我使用文件上传选项上传文件。我直接发送这个文件从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);

相关文章

### 创建一个gRPC服务项目(grpc服务端)和一个 webapi项目(...
一、SiganlR 使用的协议类型 1.websocket即时通讯协议 2.Ser...
.Net 6 WebApi 项目 在Linux系统上 打包成Docker镜像,发布为...
一、 PD简介PowerDesigner 是一个集所有现代建模技术于一身的...
一、存储过程 存储过程就像数据库中运行的方法(函数) 优点:...
一、Ueditor的下载 1、百度编辑器下载地址:http://ueditor....