내가 만드는 비즈니스 앱에서 관리자는 특정 데이터가 포함 된 CSV 파일을 Google 데이터베이스에 업로드하고 데이터베이스에 입력 할 수 있습니다 (모든 적절한 오류 처리가 발생하고 있습니다.).파일 업로드 : MemoryStream 대 파일 시스템
.NET 4.5로 업그레이드 할 때,이 코드의 몇 가지 측면을 업데이트해야만했습니다. 그렇게하고있는 동안 MemoryStream을 사용하여 업로드 된 파일을 처리하는 사람이 this answer이었습니다. 일시적으로 파일 시스템에 저장. 저를 변화시켜야 할 진짜 이유가 없습니다. (그리고 어쩌면 나쁜 것도 있습니다.) 조금 배우기위한 기회를 원했습니다. 그래서, 나는 빨리 (때문에 다른 메타 데이터의 업로드에 강력한 형식의 모델)이 코드를 스왑 아웃이에
HttpPostedFileBase file = model.File;
var fileName = Path.GetFileName(file.FileName);
var path = Path.Combine(Server.MapPath("~/App_Data/Uploads"), fileName);
file.SaveAs(path);
CsvParser csvParser = new CsvParser();
Product product = csvParser.Parse(path);
this.repository.Insert(product);
this.repository.Save();
return View("Details", product);
:이 작업을 수행 할 때
using (MemoryStream memoryStream = new MemoryStream())
{
model.File.InputStream.CopyTo(memoryStream);
CsvParser csvParser = new CsvParser();
Product product = csvParser.Parse(memoryStream);
this.repository.Insert(product);
this.repository.Save();
return View("Details", product);
}
불행히도 상황이 휴식 - 내 모든 데이터가 null 값으로 나오고 실제로 MemoryStream에 아무것도없는 것 같습니다 (비록 내가 이것에 대해 긍정적이지는 않지만). 나는 이것이 장거리일지도 모른다는 것을 알고 있지만, 내가 여기 없거나 더 잘 디버깅하기 위해 할 수있는 일이 무엇인가?
정답 : 몇 초 남았습니다. :) –