我需要在我的 asp.net mvc 应用程序中解析和自定义保存(不使用SaveAs
) 。HttpPostedFileBase
我的代码是:
private ReportViewModel LoadFile(Stream stream, string fileName)
{
MapParser mapParser = new MapParser(stream);
try
{
mapParser.Parse();
}
....
stream.position = 0;
SaveFile(memStream2, Server.MapPath("~/Uploads/Maps/" +
(map.Id + "." + ext)));
....
}
public static void SaveFile(Stream inputStream, string filePath)
{
using (Stream file = System.IO.File.Create(filePath))
{
inputStream.CopyTo(file);
}
}
LoadFile(loadMapVM.MapFile.InputStream, loadMapVM.MapFile.FileName)
所以,我的Parse()
方法工作正常,但在该SaveAs
方法之后创建 0 字节的文件。我认为,我应该克隆或重新打开以在Parse()
. 但我该怎么做呢?
编辑
我的 MapParser 代码:
public MapParser(Stream stream)
{
using(stream)
{
IWorkbook workBook = null;
if (POIXMLDocument.HasOOXMLHeader(stream))
workBook = new XSSFWorkbook(stream);
if (POIFSFileSystem.HasPOIFSHeader(stream))
workBook = new HSSFWorkbook(stream);
if (workBook == null)
throw new MapParserException(MapParserError.InvalidFileFormat);
mSheet = workBook.GetSheetAt(0);
}
}
public void Parse()
{
mData = new Map();
mData.UnitCode = mSheet.GetRow(0).GetCell(5).GetSafeValue();
if (mData.UnitCode == "")
throw new MapParserException(MapParserError.NoUnitCode, 0, 5);
....
}