c# - SQL Server 图像列到 html
问题描述
我在 SQL Server 表中有一个包含所有员工图片的表。列是类型Image
我需要以 word 格式下载员工个人资料,其中包含他们的图片。我能够以 HTML 格式导出他们的所有数据,然后我使用以下 C# 代码将该 html 转换为 word 并将其作为下载提供。
public static void HtmlToWordDownload(string HTML, string FileName)
{
lock (LockMulti)
{
string strBody = string.Empty;
strBody = @"<html xmlns:o='urn:schemas-microsoft-com:office:office' " +
"xmlns:w='urn:schemas-microsoft-com:office:word'" +
"xmlns='http://www.w3.org/TR/REC-html40'>" +
"<head><title>Document Title</title>" +
"<!--[if gte mso 9]><xml><w:WordDocument><w:View>Print</w:View><w:Zoom>100</w:Zoom>" +
"<w:DoNotOptimizeForBrowser/></w:WordDocument></xml><![endif]-->" +
"<style> @page Section1 {size:8.27in 11.69in; mso-first-footer:ff1; mso-footer: f1; mso-header: h1; " +
//"border:solid navy 2.25pt; padding:24.0pt 24.0pt 24.0pt 24.0pt; " +
"margin:0.6in 0.6in 0.6in 0.6in ; mso-header-margin:.1in; " +
"mso-footer-margin:.1in; mso-paper-source:0;} " +
"div.Section1 {page:Section1;} p.MsoFooter, li.MsoFooter, " +
"div.MsoFooter{margin:0in; margin-bottom:.0001pt; " +
"mso-pagination:widow-orphan; tab-stops:center 3.0in right 6.0in; " +
"font-size:12.0pt; font-family:'Arial';} " +
"p.MsoHeader, li.MsoHeader, div.MsoHeader {margin:0in; " +
"margin-bottom:.0001pt; mso-pagination:widow-orphan; tab-stops:center " +
"3.0in right 6.0in; font-size:12.0pt; font-family:'Arial';}--></style></head> ";
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Charset = "";
HttpContext.Current.Response.ContentType = "application/vnd.ms-word";
HttpContext.Current.Response.AddHeader("Content-Disposition", "inline;filename=" + FileName + ".doc");
StringBuilder htmlCode = new StringBuilder();
htmlCode.Append(strBody);
htmlCode.Append("<body><div class=Section1>");
htmlCode.Append(HTML);
htmlCode.Append("</div></body></html>");
HttpContext.Current.Response.Write(htmlCode.ToString());
HttpContext.Current.ApplicationInstance.CompleteRequest();
HttpContext.Current.Response.Flush();
}
}
它工作得很好。现在我怎样才能在其中嵌入员工的照片。我应该将该图像转换为什么格式以嵌入 HTML。我尝试了以下查询,但它为所有图片提供了相同的输出。请帮忙。
select
top 30 convert(varchar, convert(binary, i.Photo))
from hrm.hrm_rp_Employee_Image i
输出如下
(No column name)
ÿØÿà
ÿØÿà
ÿØÿà
ÿØÿà
ÿØÿà
ÿØÿà
我的方法是正确的还是需要做其他事情?
更新
我发现以下查询将其转换为 base64。
declare @pic varchar(max)
SELECT @pic = '<img src="data:image/jpg;base64,' + CAST(N'' AS XML).value(
'xs:base64Binary(xs:hexBinary(sql:column("Pic")))'
, 'VARCHAR(MAX)'
)
FROM (
SELECT CAST(Photo AS VARBINARY(MAX)) AS Pic from hrm.hrm_rp_Employee_Image
) AS h;
set @pic = @pic + '" style="height: 100px; width: 100px;" />'
print @pic
它成功地转换并给了我一个字符串,当我在任何在线 html 编辑器上测试它时它工作得很好。
但是下载后我的word文件中没有显示,出现带有红色“X”符号的图像,有人可以告诉我可能是什么问题。
解决方案
推荐阅读
- ios - SF符号未显示在swiftUI中的按钮上
- python - 使用纬度经度坐标从卫星图像中获取最近的像素值
- swiftui - 从数组中的多个特定结构中提取一个属性
- python - 在字符串上找到大写字母并替换它
- ios - 创建没有故事板的 iMessage 扩展?
- c# - 为什么在添加全局授权过滤器时会调用两次授权处理程序?
- node.js - 常驻内存大于 --max-old-space-size 阈值?
- reactjs - react router中使用history.push()时需要刷新的问题
- r - 使用 ggplot 绘制线并拟合线性回归线
- gpu - Jupyter 内核在加载图像进行训练时死机?如何保持纵横比不变?