0

我有一个显示大量照片的网站,使用 asp.net 和 sql server 构建,照片存储在服务器的文件夹中,并且指向照片的指针存储在 sql server 中。我使用 iis7 在我的 Windows 计算机上托管了该网站,当我从同一台计算机查看该网站时,我在 Temp Internet Files 文件夹中找不到任何照片。不确定是否要使用另一台计算机,因为该网站尚未在线。那么这里发生了什么?我做错了什么还是 IE 没有从 localhost 将图片下载到临时文件夹中?

4

1 回答 1

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 { }
}
于 2012-06-19T06:12:44.150 回答