如何使用 SQL 数据库服务器在 C# 和 .netcore 3.1 中使用 swagger api(控制器)显示多个图像?
How to display multiple images using swagger api (controller) in C# & .netcore 3.1 using SQL database server?
我已经完成了 logincontroller.cs 和数据库文件的代码。但是 usingdatabase.cs 中的最后一行显示错误。这是我的代码片段:
Usingdatabase.cs
该函数用于从api.
中获取图片格式的文档
public void GetUserDocuments ()
{
using (Con = new SqlConnection(SqlConnectionString.ConnectionString))
{
DataTable ds = new DataTable();
var img="";
try
{
Con.Open();
string sqlquery = "Select * from Auth.UserDocuments where UserId = 2016";
SqlDataAdapter dataAdapter = new SqlDataAdapter(sqlquery, Con);
foreach (DataRow row in ds.Rows)
{
//Get the byte array from image file
byte[] imgBytes = (byte[])row["Image"];
//If you want convert to a bitmap file
TypeConverter tc = TypeDescriptor.GetConverter(typeof(Bitmap));
Bitmap MyBitmap = (Bitmap)tc.ConvertFrom(imgBytes);
string imgString = Convert.ToBase64String(imgBytes);
//Set the source with data:image/bmp
img = String.Format("data:image/jpge;base64,{0}\"", imgString); //img is the Image control ID
}
//LoginController imglogin = new LoginController();
**LoginController.ImageBase64(img);**//THIS LINE IS SHOWING ERROR.
}
finally
{
Con.Close();
}
}
这是logincontroller.cs
[HttpGet]
[Route("GetDocuments")]
public string ImageBase64(string ImagePath)
{
try
{
string filepath = Path.Combine(_webHostEnvironment.WebRootPath + "\Images\UserDocuments", ImagePath);
byte[] imageArry = System.IO.File.ReadAllBytes(filepath.Replace("//", "\"));
string base64ImageRepresantation = Convert.ToBase64String(imageArry);
return "data:image/jpge;base64," + base64ImageRepresantation;
}
catch
{
return "";
}
}
如何从数据库中获取图片并在swagger中显示api?
经过测试,swaggerui无法显示多张图片
您可以使用我的示例代码单独显示您的图像。
提示
删除 data:image/png;base64,
前缀。
代码示例
[HttpGet]
public FileContentResult show()
{
// get strings from db
string base64image = "/9j/2wCEAAkGBxITEhITEhISFRUVFRcVFRUVFRUVFRUVFRUWFxUVFRUYHSggGBolHRUVITEhJSkrLi4uFx8zODMtNygtLisBCgoKDg0OFRAQFSsZFR0tLS0tLSstLS0rLS0rLS0tLS0tKy0tLS0tNysrLTctNy0tKzc3LS0tLS0tNzc3NysrK//AABEIAOEA4QMBIgACEQEDEQH/xAAbAAABBQEBAA...WghBDURCECX/9k=";
byte[] Picture = Convert.FromBase64String(base64image);
return File(Picture, "image/png");
}
我已经完成了 logincontroller.cs 和数据库文件的代码。但是 usingdatabase.cs 中的最后一行显示错误。这是我的代码片段: Usingdatabase.cs 该函数用于从api.
中获取图片格式的文档public void GetUserDocuments ()
{
using (Con = new SqlConnection(SqlConnectionString.ConnectionString))
{
DataTable ds = new DataTable();
var img="";
try
{
Con.Open();
string sqlquery = "Select * from Auth.UserDocuments where UserId = 2016";
SqlDataAdapter dataAdapter = new SqlDataAdapter(sqlquery, Con);
foreach (DataRow row in ds.Rows)
{
//Get the byte array from image file
byte[] imgBytes = (byte[])row["Image"];
//If you want convert to a bitmap file
TypeConverter tc = TypeDescriptor.GetConverter(typeof(Bitmap));
Bitmap MyBitmap = (Bitmap)tc.ConvertFrom(imgBytes);
string imgString = Convert.ToBase64String(imgBytes);
//Set the source with data:image/bmp
img = String.Format("data:image/jpge;base64,{0}\"", imgString); //img is the Image control ID
}
//LoginController imglogin = new LoginController();
**LoginController.ImageBase64(img);**//THIS LINE IS SHOWING ERROR.
}
finally
{
Con.Close();
}
}
这是logincontroller.cs
[HttpGet]
[Route("GetDocuments")]
public string ImageBase64(string ImagePath)
{
try
{
string filepath = Path.Combine(_webHostEnvironment.WebRootPath + "\Images\UserDocuments", ImagePath);
byte[] imageArry = System.IO.File.ReadAllBytes(filepath.Replace("//", "\"));
string base64ImageRepresantation = Convert.ToBase64String(imageArry);
return "data:image/jpge;base64," + base64ImageRepresantation;
}
catch
{
return "";
}
}
如何从数据库中获取图片并在swagger中显示api?
经过测试,swaggerui无法显示多张图片
您可以使用我的示例代码单独显示您的图像。
提示
删除 data:image/png;base64,
前缀。
代码示例
[HttpGet]
public FileContentResult show()
{
// get strings from db
string base64image = "/9j/2wCEAAkGBxITEhITEhISFRUVFRcVFRUVFRUVFRUVFRUWFxUVFRUYHSggGBolHRUVITEhJSkrLi4uFx8zODMtNygtLisBCgoKDg0OFRAQFSsZFR0tLS0tLSstLS0rLS0rLS0tLS0tKy0tLS0tNysrLTctNy0tKzc3LS0tLS0tNzc3NysrK//AABEIAOEA4QMBIgACEQEDEQH/xAAbAAABBQEBAA...WghBDURCECX/9k=";
byte[] Picture = Convert.FromBase64String(base64image);
return File(Picture, "image/png");
}