• 使用JavaScript调用aspx后台代码


    方法1:js同步调用

    触发:

    onclick="javascript:share('<%# Eval("id_File") %>')"

    页面函数:

     function share(id_File) {
                var a = '<%= ShareDoc("' + id_File + '") %>';
                alert(a);
    
            }

    后台cs函数:

      public string ShareDoc(string id_File)
            {
                return "后台函数[" + id_File + "]";
            }

    方法2:JQuery ajax异步调用

    触发:

    onclick="javascript:download('<%# Eval("FileNewName") %>','<%# Eval("FileExtName") %>')"

    前台函数:

       function download(p1, p2) {
               
                $.ajax({
                    url: 'MyDocList.aspx/DownloadDoc',
                    contentType: 'application/json; charset=utf-8',
                    datatype: 'json',
                    type: 'POST',
                    data: '{FileNewName:"' + p1 + '",FileExtName:"' + p2 + '"}', //参数
                    success: function(result) {//成功后执行的方法
                        alert(result.d); // 后台返回值
                    },
                    error: function(result) {//失败执行的方法
                        alert("error:" + result.responseText);
                    }
                });
             
            }

    后台cs函数:

      [WebMethod]
            public static string DownloadDoc(string FileNewName, string FileExtName)
            {
    
                string filePath = Server.MapPath(string.Format("~/FileUpload/{0}.{1}", FileNewName, FileExtName));
                if (System.IO.File.Exists(filePath))
                {
                    FileInfo file = new FileInfo(filePath);
                    Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8"); //解决中文乱码
                    Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(file.Name)); //解决中文文件名乱码    
                    Response.AddHeader("Content-length", file.Length.ToString());
                    Response.ContentType = "appliction/octet-stream";
                    Response.WriteFile(file.FullName);
                    Response.End();
                }
                return filePath;
            }

    注意:下载功能是错误的。但是函数可以这样调用。

  • 相关阅读:
    No-3.Linux 终端命令格式
    No-2.常用 Linux 命令的基本使用
    No-1.文件和目录
    No-7.运算符
    No-6.If语句
    No-5.变量的命名
    YOLOv4详细分析 | 细数当前最佳检测框架小细节(附论文及源码下载)
    案例】S7-200SMART 实时时钟如何在MCGS触摸屏上显示并写入
    卡尔曼滤波:从入门到精通
    mmdetection最小复刻版(七):anchor-base和anchor-free差异分析
  • 原文地址:https://www.cnblogs.com/cocoulong/p/3167448.html
Copyright © 2020-2023  润新知