如何使用 asp.net 核心从数据库的特定行下载图像
How to download an image from specific row from database with asp.net core
我想从数据库中获取图像并在我的 react-native 中显示它app.where我做错了吗?
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();
}
}
}
我修好了。
[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");
}
我想从数据库中获取图像并在我的 react-native 中显示它app.where我做错了吗?
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();
}
}
}
我修好了。
[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");
}