日期:2014-05-18  浏览次数:20417 次

jquery写下载程序,调用后台方法后没有回应
以前写下载都是用asp.net的内置控件,我想用jquery 的ajax()方法调用后台程序,但是确实是调用到后台了,但是下载不了东西为什么啊,高手大神们指教一下,我把代码贴出来:
前台: <form id="form1" runat="server">
  <div id="slider" >
  </div>
  </form>
</body>
</html>
<script type="text/javascript">
  //等待DOC元素加载完毕,从后台读取图片并加载到前台页面
  $(document).ready(function () {
  $.ajax({
  type: "post",
  url: "../Ws/WebService1.asmx/HelloWorld",
  datatype: "html",
  async: false,
  beforeSend: function (XMLHttpRequest) {
  },
  success: function (msg) {
  $("#slider").append(msg.text)
  },
  error: function () {
  alert('发生错误');
  }
  });
  $('input[type=button]').click(function () {
  var ID = $(this).attr("title");
  $.ajax({
  type: "post",
  url: "WebForm6.aspx",
  data: 'ID=' + ID,
  datatype: "html",
  async: true,
  beforeSend: function (XMLHttpRequest) {
  //如果不是异步读取数据下面则不需要
  //$("#showResult").text("请稍后.....");
  },
  success: function (msg) {

  },
  error: function () {
  alert('发生错误');
  }
  })
  ////////////////////////
  })
  });
</script>

后台的:
public partial class WebForm6 : System.Web.UI.Page
  {
  protected void Page_Load(object sender, EventArgs e)
  {
  if (!IsPostBack)
  {
  if (Request["ID"] != null)
  {
  string ID = Request["ID"];
  string name="";

  if (int.Parse(ID) == 1) { name = "truetrue.mp3"; }
  if (int.Parse(ID) == 2) { name = "不得不爱.mp3"; }
  string filename = "Download/" + name;
  if (filename != "")
  {
  string path = Server.MapPath(filename);
  FileInfo file = new FileInfo(path);
  if (file.Exists)
  {
  Response.Clear();
  Response.AddHeader("Content-Disposition", "attachment;filename=" + Server.UrlEncode(name));
  Response.AddHeader("Content-Length", file.Length.ToString());
  Response.ContentType = "application/octet-stream; charset=gb2312";
  Response.Filter.Close();
  Response.WriteFile(file.FullName);
  Response.End();
  }
  }
  }
  }
  }
  }
请问什么原因, 另外我的表单是在后面拼写的,就是这个HelloWorld()
[WebMe