[英]Read file.inputstream twice
I need to read csv file twice. 我需要两次读取csv文件。 but after first reading: 但在第一次阅读后:
using (var csvReader = new StreamReader(file.InputStream))
{
fileFullText += csvReader.ReadToEnd();
file.InputStream.Seek(0, SeekOrigin.Begin);
csvReader.Close();
}
using file in enother function: 在另一个函数中使用文件:
public static List<string> ParceCsv(HttpPostedFileBase file)
{
//file.InputStream.Seek(0, SeekOrigin.Begin);
using (var csvReader = new StreamReader(file.InputStream))
{
// csvReader.DiscardBufferedData();
// csvReader.BaseStream.Seek(0, SeekOrigin.Begin);
string inputLine = "";
var values = new List<string>();
while ((inputLine = csvReader.ReadLine()) != null)
{
values.Add(inputLine.Trim().Replace(",", "").Replace(" ", ""));
}
csvReader.Close();
return values;
}
}
The file.Length is 0. Can anybody help? file.Length是0.有人可以帮忙吗?
The reason is that SteramReader
's Dispose()
method also closes the underlying stream; 原因是SteramReader
的Dispose()
方法也关闭了底层流; In your case file.InputStream
. 在你的情况下file.InputStream
。 The using
statement calls Dispose()
implicitly. using
语句隐式调用Dispose()
。 Try to replace using with disposes of both your StreamReaded
-s after you finished both read operations. 在完成两个读取操作后,尝试替换使用处理两个StreamReaded
-s。 As I remember some stream classes have a bool option to leave underlying stream open after dispose. 我记得有些流类有一个bool选项,可以在处理后打开底层流。
.NET 4.5 fixed this issue by introducing leaveOpen
parameter in SteamReader
constructor. .NET 4.5通过在SteamReader
构造函数中引入leaveOpen
参数来解决此问题。 See: MSDN 请参阅: MSDN
public StreamReader(
Stream stream,
Encoding encoding,
bool detectEncodingFromByteOrderMarks,
int bufferSize,
bool leaveOpen
)
One more thing. 还有一件事。 You do not need to close SteramReader
yourself (the line with csvReader.Close();
) when you wrap it in using
statement, thus Dispose()
and Close()
are the same in case of StreamReader
. 当你将它包装在using
语句中时,你不需要自己关闭SteramReader
(与csvReader.Close();
),因此在StreamReader
情况下, Dispose()
和Close()
是相同的。
if your using HttpPostedFileBase you need to clone it first, 如果你使用HttpPostedFileBase你需要先克隆它,
use the code this git here 在这里使用这个git的代码
or just add this as a class in your namespace: 或者只是将其添加为命名空间中的类:
public static class HttpPostedFileBaseExtensions
{
public static Byte[] ToByteArray(this HttpPostedFileBase value)
{
if (value == null)
return null;
var array = new Byte[value.ContentLength];
value.InputStream.Position = 0;
value.InputStream.Read(array, 0, value.ContentLength);
return array;
}
}
now you can read the HttpPostedFileBase like so: 现在你可以像这样读取HttpPostedFileBase:
private static void doSomeStuff(HttpPostedFileBase file)
{
try
{
using (var reader = new MemoryStream(file.ToByteArray()))
{
// do some stuff... say read it to xml
using (var xmlTextReader = new XmlTextReader(reader))
{
}
}
}
catch (Exception ex)
{
throw ex;
}
}
after using this you can still write in your main code: 使用此功能后,您仍然可以在主代码中写入:
file.SaveAs(path);
and it will save it to the file. 它会将其保存到文件中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.