3
I have a project Asp.Net MVC in .Net Framework 4.0
, with Entity Framework 5.0
.
I get multiple files in one method POST
and I need to turn them into byte[]
to save to Database (VARBINARY(MAX)
- SQL Server) and then read its contents.
I’m using the BinaryReader
to read the bytes
of the entire file and has worked perfectly if I use it before the StreamReader
, but when trying to use the StreamReader
then to read the lines of the file is as if it had been given a Dispose()
in the postedFile.InputStream
.
The interesting thing is that if I reverse the order, reading the lines before with StreamReader
and then trying to use the BinaryReader
, then the latter also does not work on account of the same reason.
I know you both somehow implement the interface IDisposable
, and are using the same postedFile.InputStream
(which is also Disposable
), but what I don’t see the point is to give a Dispose()
also in the InputStream
and not only on the object itself that is using it.
Follows excerpt from the code:
for (var i = 0; i < Request.Files.Count; i++)
{
var postedFile = Request.Files[i];
var fileExtension = Path.GetExtension(postedFile.FileName).ToLower();
string fileName = Path.GetFileName(postedFile.FileName);
// Ler bytes do arquivo
byte[] fileBytes;
using (var reader2 = new BinaryReader(postedFile.InputStream))
{
fileBytes = reader2.ReadBytes(postedFile.ContentLength); // Funciona perfeitamente e retorna o resultado correto.
}
// Ler linhas do arquivo
using (var reader = new StreamReader(postedFile.InputStream))
{
var header = reader.ReadLine(); // Erro nessa linha! header = null.
// Código continua...
}
// Código continua...
}
Actually it didn’t work, the Length of Inputstream is perfect when I enter the Binaryreader, but when I exit it is 0. As if I had given a Dispose() even, but I think that’s exactly what happens. The solution is in the . Net Framework 4.5, but I’m using 4.0: https://msdn.microsoft.com/en-us/library/gg712952(v=VS.110). aspx
– Jedaias Rodrigues