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();
}
}
}
CodePudding user response:
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");
}