• ASP.Net MVC 中a标签的onclick时间和href同时存在时候的处理


    问题出现:

    本次项目在用到下载文件、导出文件的时候,需要在下载、导出之前进行判断,最初使用方式一、二,没能解决问题

    方式一:使用href直接跳转controller方法,以下载为例:

    public ActionResult DownLoadFile(string QuoteUrl)
            {
                string path = Server.MapPath("~/FilesServer/" + QuoteUrl);      //获取物理路径
                int index = QuoteUrl.LastIndexOf('/');                          //获取/最后匹配索引
                string str = QuoteUrl.Substring(index + 1);                     //获得后缀
                string s = MimeMapping.GetMimeMapping(str);                     //获取文件类型
                return File(path, s, Path.GetFileName(path));               //下载
            }

    如果在此处下载之前,进行判断空,则无法返回页面提示信息,使用response.write(“文件找不到了”)的话,会完全刷新整个页面,并存在样式变化等bug,(不太清楚)。

    方式二:使用onclick事件,进入JS中,使用ajax传递参数到controller中进行判断是否为空,在success函数中根据resultdata进行提示,以下载为例:

    同参考以上代码,假如文件路径不对,ajax应该返回字符串“文件找不到了”,但是如果文件路径没有问题的话,应该返回的是File()类型,进行下载,这样两种情况返回两种返回值类型,不符合代码设计。再者,使用ajax无法打开下载的对话框(不是很明白)。

    方式三:同时使用onclick和href,根据先执行onclick根据onclick返回结果决定是否继续执行href的特性,onclick判断,href执行。

    以下载为例:

    function download_check(url, aid) {
        var f = false;
        $.ajax({
            async: false,
            cache: false,
            type: "GET",
            url: "/QualifiedSupplierManage/SupplierQuoteManage/DownloadCheck?QuoteUrl=" + url,//去controller中进行判断
            success: function (data) {
                if (data == "") {
                    $("#" + aid).attr("href", "/QualifiedSupplierManage/SupplierQuoteManage/DownloadFile?FileUrl=" + url);//对a标签href赋值
                    f = true;
                } else {
                    $.modalMsg(data, "error"); //页面提示
                    f = false;
                }

            }
        });
        return f;
    }

    这个是controller中的判断方法

    public ActionResult DownloadCheck(string QuoteUrl)
            {
                if (string.IsNullOrEmpty(QuoteUrl))
                {
                    return Content("文件找不到了");
                }
                string path = Server.MapPath("~/FilesServer/" + QuoteUrl);       //获取物理路径
                int index = QuoteUrl.LastIndexOf('/');                           //获得最后/匹配索引
                string str = QuoteUrl.Substring(index + 1);                      //获得后缀
                string s = MimeMapping.GetMimeMapping(str);                     //获得文件类型
                //判断是否存在该文件
                if (!System.IO.File.Exists(path))
                {
                    return Content("文件找不到了");
                }
                return Content("");
            }

    在这里需要注意的是,a标签的href属性,不能在最初就写死,那样的话通过此方式,不论onclick返回true还是false,href都会进行跳转,应该是在onclick函数内,根据ajax返回结果,利用jquery对a标签的href属性进行赋值,并返回false or true,这样就解决了最初的问题。

  • 相关阅读:
    【写一个自己的js库】 5.添加修改样式的方法
    【写一个自己的js库】 4.完善跨浏览器事件操作
    js跑马灯效果
    jquery hover延时
    chart
    【写一个自己的js库】 3.添加几个处理字符串的方法
    【写一个自己的js库】 2.实现自己的调试日志
    django踩坑记录
    单元测试框架unittest-3
    单元测试框架unittest-2
  • 原文地址:https://www.cnblogs.com/4job/p/9474374.html
Copyright © 2020-2023  润新知