2014-05-14 1 views
1

자막을 다운로드하려면이 웹 사이트를 만들고 있습니다. 지금은 업로드이 기능을 가지고 :데이터베이스 테이블의 정보가있는 .srt 파일을 다운로드하십시오.

using (StreamReader sr = new StreamReader(file.InputStream, Encoding.Default, true)) 
{ 
    string line; 
    while ((line = sr.ReadLine()) != null) 
    { 
     srtContent += line + '\0'; 
    } 
} 

SubtitleFile item = new SubtitleFile(); 
UpdateModel(item); 
item.state = State.Edit; 
item.SubtitleText = srtContent; 
item.name = char.ToUpper(item.name[0]) + item.name.Substring(1); 
repo.AddSubtitle(item); 
repo.Save(); 

ModelState.Clear(); 

을 그리고 이것은

지금 어떻게 든 다시 다운로드 할 수 있어야합니다, SubtitleText라는 내, 데이타베이스 내의 장소로 srtContent을 업로드합니다. 지금까지 나는 Downloader라고 부르는 View에 대한 하이퍼 링크만을 가지고 있습니다, 그러나 그것은 제가 다운로더를 위해 지금까지 가지고있는 전부입니다.

은 내가보고 싶어하는 것은 될 경우

Model.name + '.srt' 

같은 것을 부여 ID의 정보를 가지고 StreamWriter를 또는 무언가의 일종을 새 파일로 다시 정보를 넣어하는 방법입니다 내가 원래 복사 한 것과 같은 텍스트로

나는 이것을 이해할 수있게 되었기를 바란다. 모든 건설적인 도움이 풍부합니다.

답변

1

정보가 데이터베이스에 저장되었다고 가정하면 System.Data.SqlClient 네임 스페이스를 사용합니다.

SqlConnection myConnection = new SqlConnection("your connection string"); 
myConnection.Open(); 
string id = "my_id"; 
string text; 
string fileName; 
SqlCommand query = new SqlCommand(); 
query.CommandText = "SELECT FileName, SubtitleText FROM Subtitles WHERE ID = '@id'"; 
query.Parameters.AddWithValue("@id", id); 
query.Connection = myConnection; 
SqlDataReader data = query.ExecuteReader(); 
while (data.Read()) { 
text = (string)data["SubtitleText"]; 
fileName = (string)data["FileName"]; 
} 
using (FileStream fs = File.Create(file + ".srt")) { 
File.WriteAllText(file, text); 
} 

이것은 나쁜 코드이지만 일종의 목표를 달성하기 위해 할 수있는 아이디어를 대략적으로 제공합니다. ID가 int 인 경우 ID를 int로 변경할 수 있습니다.

부록 : 영어가 제 첫 번째 언어가 아니므로 실수를 용서하십시오.

관련 문제