[英]Why the Embedded Image sent through the LinkedResource is also attached as an Attachement
[英]Create a LinkedResource of an Image obtained from a URL on the server
我有一个表单,允许用户上传图像,然后通过电子邮件发送。 该图像作为varbinary
存储在数据库中。 我有一个名为image.aspx
的页面。 该页面查找查询字符串,例如image.aspx?id=10022
,然后搜索数据库并找到 ID 为10022
的图像。
一旦我有了bytes
我就在image.aspx
上执行此image.aspx
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = "image/jpeg";
Response.BinaryWrite(bytes);
Response.Flush();
Response.End();
这将生成一个页面,其中仅包含存储在数据库中的图像。 如果您访问的是www.website.come/image.jpg
的网址,则类似,但它看起来像website.com/image.aspx?id=10022
然后在原始上传表单上,我将图像显示在图像Image Control
,该Image Control
在上传时给出了图像 url。
protected void upBtn_Click(object sender, EventArgs e)
{
byte[] image = FileUp.FileBytes;
String name = FileUp.FileName;
int id = db.UploadImage(name, image);
productImg.ImageUrl = "~/image.aspx?id=" + id.ToString();
}
图像立即显示在图像控件中。 我的下一步是我遇到问题的地方。 我试图通过将其写入stream
来创建此图像的LinkedResource
,但是我收到System.ArgumentException: Illegal characters in path.
无论我尝试什么,都会出错。
这就是我尝试创建LinkedResource
。
LinkedResource res = new LinkedResource(GetStreamFromUrl(path), MediaTypeNames.Image.Jpeg));
private Stream GetStreamFromUrl(string url)
{
byte[] imageData = null;
using (var wc = new System.Net.WebClient())
imageData = wc.DownloadData(Server.MapPath(url));//the error happens right here
return new MemoryStream(imageData);
}
在这种情况下,路径将从productImg.ImageUrl
获得,它等于~/image.aspx?id=10022
。 有人可以帮忙解释一下吗
如果图像在数据库中,为什么要从 url 中获取它。 这只是一个额外的步骤。 直接使用字节数组。 这就是我要做的。
byte[] image = yourImageByteArray;
Stream stream = new MemoryStream(image);
LinkedResource res = new LinkedResource(stream, MediaTypeNames.Image.Jpeg);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.