拦截请求的文件路径&新标签中的不同源打开文件
我有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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论