我有一个显示很多照片的网站,用asp.net和sql server构建,照片存储在服务器的文件夹中,指向存储在sql server中的照片。我使用iis7在我的Windows计算机上托管了该网站,当我从同一台计算机查看该网站时,我在Temp Internet Files文件夹中找不到任何照片。由于网站尚未在线,因此不确定是否使用其他计算机。那么这里发生了什么?我做错了什么或IE没有从localhost下载图片到临时文件夹??
答案 0 :(得分:1)
当我想显示存储在服务器上的另一个文件夹中的图像时,我会这样做。我是新手,所以我所做的可能不是最有效或最好的方式,但它有效,并且可能会让你知道如何更改代码以获得所需的结果。
1)将服务器上的路径添加到appSettings下的web.config。
<configuration>
<appSettings>
<add key="ClientContactBusinessCardImagePath" value="C:\Content\BusinessCards\" />
<add key="SupportLogPDFPath" value="C:\Content\SupportLogPDFs\" />
<add key="NewsAttachmentPath" value="C:\Content\NewsAttachments\" />
</appSettings>
<system.web>
//etc.
</system.web>
</configuration>
2)这是我从服务器上存储的文件夹中显示名片的方法,但不是项目的一部分:
private void showBusinessCard(int setwidth)
{
float fileWidth;
float fileHeight;
float sizeratio;
float calculatedheight;
int roundedheight;
try
{
FileStream fstream = new FileStream(WebConfigurationManager.AppSettings["ClientContactBusinessCardImagePath"] + BusinessCardLabel.Text, FileMode.Open, FileAccess.Read, FileShare.Read);
System.Drawing.Image image = System.Drawing.Image.FromStream(fstream);
fstream.Dispose();
fileWidth = image.Width;
fileHeight = image.Height;
sizeratio = fileHeight / fileWidth;
calculatedheight = setwidth * sizeratio;
roundedheight = Convert.ToInt32(calculatedheight);
imgbusinesscard.Width = setwidth;
imgbusinesscard.Height = roundedheight;
imgbusinesscard.ImageUrl = "ImageHandler.ashx?img=" + BusinessCardLabel.Text;
hlbusinesscard.NavigateUrl = "ImageHandler.ashx?img=" + BusinessCardLabel.Text;
}
catch
{
imgbusinesscard.ImageUrl = "~/images/editcontact/businesscard-noimage.png";
imgbusinesscard.Width = 240;
imgbusinesscard.Height = 180;
}
3)在那里中间调用的ImageHandler看起来像这样:
public void ProcessRequest(HttpContext context)
{
try
{
string imageFileName = context.Request.QueryString["img"];
System.Drawing.Image objImage = System.Drawing.Bitmap.FromFile(Path.Combine(WebConfigurationManager.AppSettings["ClientContactBusinessCardImagePath"], imageFileName));
if (imageFileName != null)
{
MemoryStream objMemoryStream = new MemoryStream();
objImage.Save(objMemoryStream, System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] imageContent = new byte[objMemoryStream.Length];
objMemoryStream.Position = 0;
objMemoryStream.Read(imageContent, 0, (int)objMemoryStream.Length);
objMemoryStream.Dispose();
objImage.Dispose();
context.Response.ContentType = "image/jpeg";
context.Response.BinaryWrite(imageContent);
}
}
catch { }
}