Question

I'm making this website to download subtitles. Right now I have this function to upload:

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();

And this uploads the srtContent to a place in my databse called SubtitleText,

Now I somehow need to be able to download this again. So far I only have a hyperlink to a View that I call Downloader, But that's all I got so far for the downloader.

What I'm missing is a way to take the information of ID given and do some sort of streamwriter or something, and put the info back into a new file where it would be something like

 Model.name + '.srt' 

with all the same text as I originally copied.

Hopefully I made this understandable. All constructive help appriciated.

Was it helpful?

Solution

Given that the information is stored in a database, we're gonna use the System.Data.SqlClient namespace.

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);
}

This is kind of bad code but it roughly gives the idea of what you can do to achieve your goal (As i understood it*). If the ID is int, you can change it to that.

Addendum: English is not my first language so excuse the mistakes.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top