拦截请求的文件路径&新标签中的不同源打开文件

发布于 2025-01-22 04:07:34 字数 1873 浏览 0 评论 0原文

我有4.7.2应用程序在锚固标签上我们有文件链接,例如'www.abc.com/a.txt'。如果我单击它,文件将在新选项卡中打开。 但是现在预期的结果是 - 当用户点击该链接时,我想拦截此链接&将其更改为'www.xyz.com/a.txt'&在新标签中返回响应。基本上是在新标签中打开文件(无下载)。

目前,我可以使用以下代码下载文件,但我想在新标签中打开。

Stream stream = null;
            int bytesToRead = 10000;
            byte[] buffer = new Byte[bytesToRead];
            try
            {
                string apiGatewayFilePath = "www.xyz.com/a.txt";
                HttpWebRequest fileReq = (HttpWebRequest)WebRequest.Create(apiGatewayFilePath);
                string fileName = Path.GetFileName(apiGatewayFilePath);
                HttpWebResponse fileResp = (HttpWebResponse)fileReq.GetResponse();

                if (fileReq.ContentLength > 0)
                    fileResp.ContentLength = fileReq.ContentLength;

                stream = fileResp.GetResponseStream();
                var resp = HttpContext.Current.Response;
                resp.ContentType = MediaTypeNames.Application.Octet;
                resp.AddHeader("Content-Disposition", "inline; filename=\"" + fileName + "\"");
                resp.AddHeader("Content-Length", fileResp.ContentLength.ToString());

                int length;
                do
                {
                    if (resp.IsClientConnected)
                    {
                        length = stream.Read(buffer, 0, bytesToRead);
                        resp.OutputStream.Write(buffer, 0, length);
                        resp.Flush();
                        buffer = new Byte[bytesToRead];
                    }
                    else
                    {
                        length = -1;
                    }
                } while (length > 0);
            }
            finally
            {
                if (stream != null)
                {
                    stream.Close();
                }
            }

I have 4.7.2 application where on anchor tag we have file link like 'www.abc.com/a.txt'. If I click it file is opening in new tab.
But now expected result is - When user hit that link, I want to intercept this link & change it to 'www.xyz.com/a.txt' & return response in new tab. Basically open file in new tab (no download).

Currently I'm able to download file using below code but I want to open in new tab.

Stream stream = null;
            int bytesToRead = 10000;
            byte[] buffer = new Byte[bytesToRead];
            try
            {
                string apiGatewayFilePath = "www.xyz.com/a.txt";
                HttpWebRequest fileReq = (HttpWebRequest)WebRequest.Create(apiGatewayFilePath);
                string fileName = Path.GetFileName(apiGatewayFilePath);
                HttpWebResponse fileResp = (HttpWebResponse)fileReq.GetResponse();

                if (fileReq.ContentLength > 0)
                    fileResp.ContentLength = fileReq.ContentLength;

                stream = fileResp.GetResponseStream();
                var resp = HttpContext.Current.Response;
                resp.ContentType = MediaTypeNames.Application.Octet;
                resp.AddHeader("Content-Disposition", "inline; filename=\"" + fileName + "\"");
                resp.AddHeader("Content-Length", fileResp.ContentLength.ToString());

                int length;
                do
                {
                    if (resp.IsClientConnected)
                    {
                        length = stream.Read(buffer, 0, bytesToRead);
                        resp.OutputStream.Write(buffer, 0, length);
                        resp.Flush();
                        buffer = new Byte[bytesToRead];
                    }
                    else
                    {
                        length = -1;
                    }
                } while (length > 0);
            }
            finally
            {
                if (stream != null)
                {
                    stream.Close();
                }
            }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文