通过 Internet Explorer 从 asp.net 发出 HTML 文件
最近我正在开发一个工具,用户可以在界面上进行更改,然后点击保存,模板将存储在服务器上..作为 HTML 文件..一切都很好...我能够从所有主要浏览器(我检查了 FireFox 和 Chrome),但是当我尝试从 Internet Explorer 保存文件时...
它无法正确发出文件..HTML 格式不正确..因此应用程序崩溃.. 以下是从Firefox
和 Chrome 检查时的正确格式...
<tr>
<td colspan="2" align="center">
<div class="searchDiv">
<span>Leader Board Box: </span><span class="pd2">
<textarea rows="5" cols="52" id="txtLeaderBoardAd" style="width:631px;"></textarea></span><br>
<br>
<span>Site Logo: </span><span style="padding-left: 34px">
<input type="text" size="47" id="txtLogoImage" value=""></span><span>Width: </span><span>
<input type="text" size="15" id="txtLogoImageWidth" value=""></span><span>Height: </span><span>
<input type="text" size="15" id="txtLogoImageHeight" value=""></span><br>
<br>
<span>Link Url: </span><span style="padding-left: 70px">
<input type="text" id="txtLogoLinkUrl" size="100" value=""></span><br>
<br>
</div>
</td>
</tr>
But when tried to save the same format from Internet Explorer... it emit the format like below
<TR sizset="9" sizcache="3">
<TD colSpan="2" align="middle" nodeindex="1" nodeIndex="1">
<DIV class="searchDiv">
<SPAN>Leader Board Box: </SPAN>
<SPAN class="pd2">
<TEXTAREA style="WIDTH: 631px" id="txtLeaderBoardAd" rows="5" cols="52"></TEXTAREA>
</SPAN>
<BR>
<BR>
<SPAN>Site Logo: </SPAN>
<SPAN style="PADDING-LEFT: 34px">
<INPUT id="txtLogoImage" size="47" type="text">
</SPAN>
<SPAN>Width: </SPAN>
<SPAN>
<INPUT id="txtLogoImageWidth" size="15" type="text">
</SPAN>
<SPAN>Height: </SPAN>
<SPAN>
<INPUT id="txtLogoImageHeight" size="15" type="text">
</SPAN>
<BR>
<BR>
<SPAN>Link Url: </SPAN>
<SPAN style="PADDING-LEFT: 70px">
<INPUT id="txtLogoLinkUrl" size="100" type="text">
</SPAN>
<BR>
<BR>
</DIV>
</TD>
</TR>
我不知道理解...它只是从所有属性中删除引号...并且还添加了额外的属性,如 sizset 和 sizcache...我是否需要为 IE 格式化一些额外的内容
请帮助...我被困在这里。 ************* >********这就是我保存文件的方式********** 我正在使用 Ajax Jquery 以 html 格式保存文件...
[WebMethod]
public static string WriteToHTMLFile(string data)
{
using (StreamWriter writer = new StreamWriter(HttpContext.Current.Server.MapPath("/NewsletterTemplate/HTMLPage.htm")))
{
writer.WriteLine(data);
writer.Flush();
}
return "Saved Successfully";
}
然后在 Javascript 中调用上面的函数..如下所示
$.ajax({ type: "POST", url: "AjaxMethods.aspx/WriteToHTMLFile", contentType: "application/json", data: "{'data':'" + $("#dvGetHTML").html() + "'}", success: function (data) {
$("#dvGetHTML").html($("#dvGetHTML").html());
}, error: function (data) { alert("Error in Save.. Please contact your administrator !!!" + data.responseText); }
});
recently i was working on a tool, where user makes changes on the interface and while hitting save, the template gets stored on server.. as an HTML file.. everything was well... i was able to save the file from all the major browsers (i checked on FireFox and Chrome), but when i tried to save the file from internet explorer....
it does not emit file properly.. the HTML is not properly formatted.. because of which the applications crashes...
Below is the proper format when checked from Firefox and Chrome...
<tr>
<td colspan="2" align="center">
<div class="searchDiv">
<span>Leader Board Box: </span><span class="pd2">
<textarea rows="5" cols="52" id="txtLeaderBoardAd" style="width:631px;"></textarea></span><br>
<br>
<span>Site Logo: </span><span style="padding-left: 34px">
<input type="text" size="47" id="txtLogoImage" value=""></span><span>Width: </span><span>
<input type="text" size="15" id="txtLogoImageWidth" value=""></span><span>Height: </span><span>
<input type="text" size="15" id="txtLogoImageHeight" value=""></span><br>
<br>
<span>Link Url: </span><span style="padding-left: 70px">
<input type="text" id="txtLogoLinkUrl" size="100" value=""></span><br>
<br>
</div>
</td>
</tr>
But when tried to save the same format from Internet Explorer... it emit the format like below
<TR sizset="9" sizcache="3">
<TD colSpan="2" align="middle" nodeindex="1" nodeIndex="1">
<DIV class="searchDiv">
<SPAN>Leader Board Box: </SPAN>
<SPAN class="pd2">
<TEXTAREA style="WIDTH: 631px" id="txtLeaderBoardAd" rows="5" cols="52"></TEXTAREA>
</SPAN>
<BR>
<BR>
<SPAN>Site Logo: </SPAN>
<SPAN style="PADDING-LEFT: 34px">
<INPUT id="txtLogoImage" size="47" type="text">
</SPAN>
<SPAN>Width: </SPAN>
<SPAN>
<INPUT id="txtLogoImageWidth" size="15" type="text">
</SPAN>
<SPAN>Height: </SPAN>
<SPAN>
<INPUT id="txtLogoImageHeight" size="15" type="text">
</SPAN>
<BR>
<BR>
<SPAN>Link Url: </SPAN>
<SPAN style="PADDING-LEFT: 70px">
<INPUT id="txtLogoLinkUrl" size="100" type="text">
</SPAN>
<BR>
<BR>
</DIV>
</TD>
</TR>
i dont understand... it just removes Quotes from all attributes.. and also adds extra attributes as sizset and sizcache... do i need to format for something extra for IE
Please help... i am stuck over here.
*********************THIS IS HOW I AM SAVING FILE**********
i am using Ajax Jquery for saving the file in html Format...
[WebMethod]
public static string WriteToHTMLFile(string data)
{
using (StreamWriter writer = new StreamWriter(HttpContext.Current.Server.MapPath("/NewsletterTemplate/HTMLPage.htm")))
{
writer.WriteLine(data);
writer.Flush();
}
return "Saved Successfully";
}
and then just calling the above function in Javascript.. like below
$.ajax({ type: "POST", url: "AjaxMethods.aspx/WriteToHTMLFile", contentType: "application/json", data: "{'data':'" + $("#dvGetHTML").html() + "'}", success: function (data) {
$("#dvGetHTML").html($("#dvGetHTML").html());
}, error: function (data) { alert("Error in Save.. Please contact your administrator !!!" + data.responseText); }
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
啊……
我明白你哪里出了问题:-)
你直接从浏览器 DOM 中拉出 html,对吗?我猜 #dvGetHTML 是一个 div?
在我看来,IE 在某种程度上以与其他浏览器不同的方式在内部表示 HTML,当您将其从 div 中拉出时,这就是您所得到的。
通过实验,尝试使用纯旧文本区域标记,您可以在其中进行编辑,并且格式往往遵循良好的老式预标记规则。
好处是,在文本区域中键入的任何内容都会按原样返回,就好像它是一个输入字段一样,如果您在执行此操作时没有得到奇怪的格式,则问题很可能与预期的一样。
如果您证明这是您的问题,那么我会使用客户端编辑器(例如“Tiny MCE”)来实现您尝试对 div 执行的操作。
ahhhhhh.....
I see where your going wrong :-)
Your yanking the html straight out of the browsers DOM right? and I'm guessing that #dvGetHTML is a div?
It sounds to me like IE is in some way representing the HTML internally in a different way to the other browsers, and when you yank it it out of your div that's what your getting.
By way of an experiment, try using a plain old text area tag, you can edit in that and the formatting tends to obey good old fashioned pre tag rules.
The bonus is that anything typed in a textarea is returned verbatim as is, as though is was an input field, if you don't get the strange formatting when you do this it's likely that the problem is as anticipated.
If you prove that is your issue, then I'd use a client side editor such as "Tiny MCE" to achive what your trying to do with a div.