-1

I want to fetch an image from the database and show it in my react-native app.where I did go wrong?

public IActionResult DownloadFile(int id)
{
    DataTable table = new DataTable();
    string query = @"select image from mydb.courses where id=@id";
    string sqlDataSource = _configuration.GetConnectionString("UsersAppCon");
    MySqlDataReader myReader;
    using (MySqlConnection mycon = new MySqlConnection(sqlDataSource))
    {
        mycon.Open();
        using (MySqlCommand myCommand = new MySqlCommand(query, mycon))
        {
            myReader = myCommand.ExecuteReader();
            table.Load(myReader);
            var fs = new FileStream(myReader, FileMode.Open);
            return File(fs, "application/octet-stream");
            myReader.Close();
            mycon.Close();
        }
    }
}
3
  • 1
    return will prevent the close() from being called. To return an image look at: stackoverflow.com/questions/39177576/… Commented Oct 9, 2021 at 11:08
  • Hi @Ali Mahmoudi, What is the error message? Could you please share your react component? Commented Oct 11, 2021 at 8:40
  • Hi Greg and @Rena, I fixed it Commented Oct 11, 2021 at 9:17

1 Answer 1

-1

I fixed it.

[HttpGet("{id}")]
public IActionResult DownloadFile(int id)
{
    string pathImage = "";
    DataTable table = new DataTable();
    string query = @"select image from mydb.courses where id=@id";
    string sqlDataSource = _configuration.GetConnectionString("UsersAppCon");
    MySqlDataReader myReader;
    using (MySqlConnection mycon = new MySqlConnection(sqlDataSource))
    {
        mycon.Open();
        using (MySqlCommand myCommand = new MySqlCommand(query, mycon))
        {
            myCommand.Parameters.AddWithValue("@id", id);
            pathImage = (string)myCommand.ExecuteScalar();
            mycon.Close();
        }
    }
    var path = @$"{pathImage}";
    var fs = new FileStream(path, FileMode.Open);
    return File(fs, "image/jpeg");
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.