Я пытаюсь извлечь jpeg-файлы, хранящиеся в SQL Server, и представить их в формате html, используя img src = "filepath", но мой код C # возвращает фактическое изображение, а не URL-путь к изображению, и это просто отображает крошечное изображениеполе с буквой х.
Как я могу отобразить изображение непосредственно как переменную изображения.Вот HTML-страница с Razor C #:
@{
int iFileID = 8;
string sPhotoDesc = "";
Image iPhotoImage = null;
}
<form>
<fieldset>
<legend>FileInput</legend>
<input type="file" id="fileinput" />
<input type='button' id='btnLoad' value='Load' onclick="@{iPhotoImage =
PhotoLibraryApp.PhotoData.SelectPhoto(iFileID, out sPhotoDesc); }">
<div id="editor"></div>
</fieldset>
<img src="@iPhotoImage" width="500" height="377">
Вот соответствующий код C # в отдельном файле PhotoData.cs:
using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Drawing;
using System.IO;
using System.Transactions;
namespace PhotoLibraryApp
{
public class PhotoData
{
private const string ConnStr =
"Data Source=.;Integrated Security=True;Initial
Catalog=PhotoLibrary;";
public static Image SelectPhoto(int photoId, out string desc)
{
const string SelectTSql = @"
SELECT
Description,
Photo.PathName(),
GET_FILESTREAM_TRANSACTION_CONTEXT()
FROM PhotoAlbum
WHERE PhotoId = @PhotoId";
Image photo;
string serverPath;
byte[] serverTxn;
using (TransactionScope ts = new TransactionScope())
{
using (SqlConnection conn = new SqlConnection(ConnStr))
{
conn.Open();
using (SqlCommand cmd = new SqlCommand(SelectTSql, conn))
{
cmd.Parameters.Add("@PhotoId", SqlDbType.Int).Value = photoId;
using (SqlDataReader rdr = cmd.ExecuteReader())
{
rdr.Read();
desc = rdr.GetSqlString(0).Value;
serverPath = rdr.GetSqlString(1).Value;
serverTxn = rdr.GetSqlBinary(2).Value;
rdr.Close();
}
}
photo = LoadPhotoImage(serverPath, serverTxn);
}
ts.Complete();
}
return photo;
}
private static Image LoadPhotoImage(string filePath, byte[] txnToken)
{
Image photo;
using (SqlFileStream sfs =
new SqlFileStream(filePath, txnToken, FileAccess.Read))
{
photo = Image.FromStream(sfs);
sfs.Close();
}
return photo;
}
}
}