<%@ Page Language="C#" %>
<!DOCTYPE html>
<script runat="server">
void Page_Load(object sender, EventArgs e)
{
if (Request["mode"] == "download")
{
if (Request["err"] == "1")
//To simulate failed download
Response.Write("<html><body>Failed to Download File!</body></html>");
else
{
Response.AddHeader("content-disposition",
"attachment;filename=text.txt");
Response.ContentType = "application/octet-stream";
Response.Write("Darkthread Test");
}
Response.End();
}
}
</script>
<html>
<head runat="server">
<title>Download Test</title>
<script type="text/javascript"
src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.6.1.js"></script>
<script type="text/javascript">
$(function () {
$(":button").click(function () {
var dlLink = "Download.aspx?mode=download&err=" +
$("#err:checked").length;
switch (this.id) {
case "b1":
window.open(dlLink);
break;
case "b2":
location.href = dlLink;
break;
case "b3":
var $ifrm = $("<iframe style='display:none' />");
$ifrm.attr("src", dlLink);
$ifrm.appendTo("body");
$ifrm.load(function () {
//if the download link return a page
//load event will be triggered
$("body").append(
"<div>Failed to download <i>'" + dlLink + "'</i>!");
});
break;
}
});
});
</script>
<style type="text/css">
.btn input
{
display: block;
width: 200px; height: 30px;
margin: 10px;
text-align: center;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<input type="checkbox" id="err" />Failed Download
<div class='btn'>
<input type="button" id="b1" value="Download with window.open" />
<input type="button" id="b2" value="Download with location.href" />
<input type="button" id="b3" value="Download with hidden iframe" />
</div>
<div id="msg"></div>
</form>
</body>
</html>