如何在 Http 模块中呈现 image/pdf

How to render image/pdf in Http Module

我有两个网站(Web1 和 Web2)和一个域 (www.web1.com)。域映射到 Web1。

我在 Web1 中注册了一个 http 模块。当Request来到Web1,Web1上存在网页时,就会显示Page。否则在 Web1 上找不到页面(404),然后模块将向 Web2 发送请求并从 Web2 获取响应并写入显示。

这是我的 http 模块代码:

    public class Testmodule : IHttpModule
{
    public void Dispose()
    {
        //clean-up code here.
    }
    public void Init(HttpApplication context)
    {
        context.PreSendRequestContent += context_PreSendRequestContent;
    }
    private void context_PreSendRequestContent(object sender, EventArgs e)
    {
        HttpContext context = HttpContext.Current;
        if (context.Response.StatusCode == 404)//On 404 on Web1 it will pass the the condition.
        {
            string strSite = WebConfigurationManager.AppSettings["Web2SiteURL"];
            string requestURL = ((HttpApplication)sender).Request.Url.ToString();
            string sourceURL = requestURL.Replace("http://" + HttpContext.Current.Request.Url.Authority, strSite);

            HttpWebRequest WebReq = (HttpWebRequest)WebRequest.Create(sourceURL);
            HttpWebResponse WebResp = (HttpWebResponse)WebReq.GetResponse();

            string WebResp_html = string.Empty;
            using (StreamReader sr = new StreamReader(WebResp.GetResponseStream()))
            {
                WebResp_html = sr.ReadToEnd();
            }
            string strCurrentUrl = "http://" + HttpContext.Current.Request.Url.Authority + "/";

            //Replacing the image src Relative URL to Absolute URL in the html source code.
            //If I comment below Replacement code Page is rendering but images are not rendering from Web2 site.
            //Actually  the image/pdf/swg files from html source code with relative path not rendering properly.
            WebResp_html = WebResp_html.Replace(strSite, strCurrentUrl);

            context.Response.Write(WebResp_html);
            context.Response.Flush();
            context.Response.Close();

        }
    }
}

下面是我的 Web2 Page2 代码

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        This is my Page 2 on Web2.
        <br/>
    <p><img width="312" height="312" class="images" alt="" src="Images/Img1.jpg"/></p>
    </div>
    </form>
</body>
</html>

它适用于 html 标签。但无法渲染具有相对路径的图像。如果我将图像 src 设为绝对路径,则它会正确呈现。

如果我在下面评论替换代码页面正在呈现,但图像不是从 Web2 站点(使用相对路径)呈现的。

WebResp_html = WebResp_html.Replace(strSite, strCurrentUrl); //converting image src from relative to absolute path.

Question : How can I render image on web page with relative path? Please help me..

来自网站 2 的页面将是对网站 1 的跨源请求。因此,您必须引用绝对路径才能从 Website2 渲染图像。如果要引用相对路径,这些图像必须是 Website1 的一部分。

我找到了解决方案。对于 StreamReader 的 Images/PDF 设置,我使用了 BinaryReader 和 BinaryWrite

    private void RenderBinaryContent(HttpWebResponse response)
    {
        using (var br = new BinaryReader(response.GetResponseStream()))
        {
            byte[] tempBytes;
            byte[] imageBytes;

            using (MemoryStream ms = new MemoryStream())
            {
                tempBytes = br.ReadBytes(4096);
                while (tempBytes.Length > 0)
                {
                    ms.Write(tempBytes, 0, tempBytes.Length);
                    tempBytes = br.ReadBytes(1024);
                }
                imageBytes = new byte[(int)ms.Length];
                ms.Position = 0;
                ms.Read(imageBytes, 0, imageBytes.Length);
            }

            HttpContext.Current.Response.BinaryWrite(imageBytes);
        }            
    }