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转换为String数组的常用方法,它首先将文件存储在服务器中,然后从服务器读取数据.无论如何直接从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()); } }