public ActionResult Import(HttpPostedFileBase currencyConversionsFile)
{
string filename = "CurrencyConversion Upload_" + DateTime.Now.ToString("dd-MM-yyyy") + ".csv";
string folderPath = Server.MapPath("~/Files/");
string filePath = Server.MapPath("~/Files/" + filename);
currencyConversionsFile.SaveAs(filePath);
string[] csvData = System.IO.File.ReadAllLines(filePath);
//the later code isn't show here
}
我知道将httppostedfilebase转换为字符串数组的常用方法,它会先将文件存储在服务器中,然后从服务器读取数据。无论如何,是否可以直接从 httppostedfilebase 获取字符串数组,而不将文件存储到服务器中?
最佳答案
您可以从 Stream
中逐行读取文件,如下所示:
List<string> csvData = new List<string>();
using (System.IO.StreamReader reader = new System.IO.StreamReader(currencyConversionsFile.InputStream))
{
while (!reader.EndOfStream)
{
csvData.Add(reader.ReadLine());
}
}
关于asp.net-mvc - 如何将 httppostedfilebase 转换为字符串数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29227000/