문제

ASP.NET MVC 웹 사이트에서 ';'에 의해 분리 된 이름과 이메일이있는 txt 파일을 읽어야합니다. 그 후이 txt 파일의 각 줄을 데이터베이스에 저장해야합니다.

인터넷 검색, 나는 몇 가지 스 니펫을 찾았지만 모두 TXT 파일 경로를 사용해야합니다.

그러나 어떻게이 길을 얻을 수 있습니까? 이 파일은 사용자 컴퓨터의 어느 곳에도있을 수 있습니다!

감사!!

도움이 되었습니까?

해결책

업로드 된 파일의 전체 경로를 얻을 수 없습니다. 파일을 업로드 한 사용자의 개인 정보 위반입니다.

대신 업로드 된 request.files를 읽어야합니다. 예를 들어:

HttpPostedFile file = Request.Files[0];
using (StreamReader reader = new StreamReader(file.InputStream))
{
    while ((string line = reader.ReadLine()) != null) 
    {
        string[] addresses = line.Split(';');
        // Do stuff with the addresses
    }
}

다른 팁

ASP.NET 웹 페이지 모델에 있다면 Server.MapPath("~/") 사이트의 루트를 얻으려면 필요한 경로를 전달하십시오. 전화해야 할 수도 있습니다

HttpContext.Current.Server.MapPath("~/");

예를 들어 텍스트 파일이 저장된 폴더입니다.

string directoryOfTexts = HttpContext.Current.Server.MapPath("~/txtdata/");

일단 그것을 읽으려면 그것을 읽으려면 그것을 스트리밍 할 수 있습니다.

string directoryOfTexts = HttpContext.Current.Server.MapPath("~/txtdata/");
string path = directoryOfTexts + "myfile.txt";
string alltextinfile = "";
if (File.Exists(path)) 
{
    using (StreamReader sr = new StreamReader(path)) 
    {
       //This allows you to do one Read operation.
       alltextinfile = sr.ReadToEnd());
    }
}

이것이 바탕 화면 응용 프로그램 인 경우 ApplCation 클래스에는이 모든 정보가 있습니다.

http://msdn.microsoft.com/en-us/library/system.windows.forms.application.startuppath.aspx

Application.StartupPath

모든 속성은 다른 AppData 폴더 및 물건을 나열하지만 실행 파일에 대한 응용 프로그램 경로가 있으면 다음과 같은 컨텍스트를 제공합니다. Application.LocalUserAppDataPath.

http://msdn.microsoft.com/en-us/library/system.windows.forms.application_properties.aspx

콘텐츠가 충분히 작 으면 HashTable 또는 일반 List<String> 데이터베이스에 저장하기 전에.

var hpf = Request.Files[file] as HttpPostedFile; 

HTML의 양식에는 있어야합니다 enctype="mulitipart/form-data"

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top