Javascript 如何在asp.net中单击按钮下载html文件?
我正在VS2015中开发一个程序。我已经动态创建了一个abc.html文件。现在我想要一个功能,当用户点击按钮时,Html文件应该在浏览器中打开或保存。我怎么做? 动态生成Html文件的代码如下所示: 客户方如下所示:Javascript 如何在asp.net中单击按钮下载html文件?,javascript,c#,html,asp.net,internet-explorer-11,Javascript,C#,Html,Asp.net,Internet Explorer 11,我正在VS2015中开发一个程序。我已经动态创建了一个abc.html文件。现在我想要一个功能,当用户点击按钮时,Html文件应该在浏览器中打开或保存。我怎么做? 动态生成Html文件的代码如下所示: 客户方如下所示: <asp:button ID="BtnGenrateHTML" runat="server" text=" Generate HTML " OnClick="btnAddnew_Click" /> 代码隐藏如下 protected void TestT
<asp:button ID="BtnGenrateHTML" runat="server" text=" Generate HTML " OnClick="btnAddnew_Click" />
代码隐藏如下
protected void TestThisHTML(object sender, EventArgs e)
{
string sFileFullName;
string sFilePath;
string sFileName;
string strHTMLGrid = "";
strHTMLGrid = strHTMLGrid + "Dear Customer,<BR><BR> Please provide below OTP to complete registration <BR><BR> ";
strHTMLGrid = strHTMLGrid + "<BR><BR> This OTP is valid for 15 minutes.";
strHTMLGrid = strHTMLGrid + "<BR><BR> With Best Regards - Indiefy";
strHTMLGrid = strHTMLGrid + "<BR><BR> Hi My name is Basant Gera";
sFilePath = Server.MapPath("");
sFileName = "abc.html";
sFileFullName = sFilePath + "\\" + sFileName;
if (!Directory.Exists(sFileFullName))
{
Directory.CreateDirectory(sFilePath);
}
// if it exist than to delete it.
if (System.IO.File.Exists(sFileFullName))
{
System.IO.File.Delete(sFileFullName);
}
// If it deleted than we need to create it again
FileStream fs = new FileStream(sFileFullName, FileMode.Create);
using (StreamWriter w = new StreamWriter(fs, Encoding.UTF8))
{
w.WriteLine(strHTMLGrid);
}
fs.Close();
Page.ClientScript.RegisterStartupScript(this.GetType(), "", "fncpopup();", true);
}
protectedvoid TestThisHTML(对象发送方,事件参数e)
{
字符串sFileFullName;
字符串路径;
字符串sFileName;
字符串strHTMLGrid=“”;
strHTMLGrid=strHTMLGrid+“尊敬的客户,
请提供以下OTP以完成注册
”;
strHTMLGrid=strHTMLGrid+“
此OTP有效期为15分钟。”;
strHTMLGrid=strHTMLGrid+“
致以最诚挚的问候-独立”;
strHTMLGrid=strHTMLGrid+“
你好,我的名字叫Basant Gera”;
sFilePath=Server.MapPath(“”);
sFileName=“abc.html”;
sFileFullName=sFilePath+“\\”+sFileName;
如果(!Directory.Exists(sFileFullName))
{
CreateDirectory(sFilePath);
}
//如果存在,则删除它。
if(System.IO.File.Exists(sFileFullName))
{
System.IO.File.Delete(sFileFullName);
}
//如果它被删除了,我们需要重新创建它
FileStream fs=newfilestream(sFileFullName,FileMode.Create);
使用(StreamWriter w=newstreamwriter(fs,Encoding.UTF8))
{
w、 WriteLine(strHTMLGrid);
}
fs.Close();
Page.ClientScript.RegisterStartupScript(this.GetType(),“”,“FNCPOUP();”,true);
}
现在我的abc.Html文件工作正常。。。
现在,我想点击按钮,将这个Html文件保存到浏览器上,并在浏览器上询问您想打开它还是将其保存到特定位置
<asp:button ID="BtnGenrateHTML" runat="server" text=" Generate HTML " OnClick="btnAddnew_Click" />
保存Html文件的位置-->我已使用mappath.server将其保存在当前目录中
如果可能,请将其保存在我们电脑目录中的下载文件夹中。您是否尝试发送最后一个响应:
{...
....
fs.Close();
Response.ContentType = "application/octet-stream";
Response.AppendHeader("Content-Disposition","attachment; filename=abc.html");
Response.TransmitFile( sFileFullName );
Response.End();
....
}
您是否尝试发送最后一个响应:
{...
....
fs.Close();
Response.ContentType = "application/octet-stream";
Response.AppendHeader("Content-Disposition","attachment; filename=abc.html");
Response.TransmitFile( sFileFullName );
Response.End();
....
}
它的工作真棒…我已经应用了它,但对于知识库,我可以知道什么是应用程序/八位字节流和内容配置“,”附件;filename=abc.html…请告诉我know@BasantGera在服务器和客户端之间的每个请求和响应中,都有HTTP头,其中包含有关客户端浏览器、请求页面、此处的服务器、,其中添加
应用程序/octet流
作为内容类型,以通知浏览器响应中存在流文件,因此如果将其替换为文本/html
浏览器将理解这是一个html文件。对于内容处置“,”附件;filename=abc.html
我们正在为http头指定文件名(abc.html)并将其作为附件发送(在浏览器中打开下载提示窗口)。您可以查看有关http头的信息。如果这对您很有帮助,请检查已解决的答案。您非常感谢。您已经非常满意…Thanxs:->不客气!请将答案标记为已解决()其工作非常棒…我已经应用了它,但对于知识库,我可以知道什么是应用程序/八位字节流和内容配置“,”附件;filename=abc.html…请告诉我know@BasantGera在服务器和客户端之间的每个请求和响应中,都有HTTP头,其中包含有关客户端浏览器、请求页面、此处的服务器、,其中添加应用程序/octet流
作为内容类型,以通知浏览器响应中存在流文件,因此如果将其替换为文本/html
浏览器将理解这是一个html文件。对于内容处置“,”附件;filename=abc.html
我们正在为http头指定文件名(abc.html)并将其作为附件发送(在浏览器中打开下载提示窗口)。您可以查看有关http头的信息。如果这对您很有帮助,请检查已解决的答案。您非常感谢。您已经非常满意…Thanxs:->不客气!请将答案标记为已解决()