todo按钮的ajax请求的一个小案例

发布于 2024-11-13 17:34:58

按钮的ajax请求

前端
// 下载作品源文件    
$(document).on("click", ".btn-downloadwork", function () {
        var that = this;
        var id = $(this).data("id");
        var type = $(this).data("type");

       CMS.api.ajax({
            data: $(this).data()
        }, function (data, ret) {
          
            $("#_sourcefileUrl").attr("href",ret.data.sourcefileUrl);
            $("#_sourcefileClick").click();
         
            return false;
        }, function () {
            return false;
        });
    });


后台
public function downloadwork()    {
        $id = (int)$this->request->post("id");
        if (!$id) {
            $this->error(__('Operation failed'));
        }
        $archives = ArchivesModel::get($id, ['model']);
     
        if (!$archives || ($archives['user_id'] != $this->auth->id && $archives['status'] != 'normal') || $archives['deletetime']) {
            $this->error(__('No specified article found'));
        }

        $table = $archives->getRelation('model')->getData('table');
        \think\Db::name($table)->where('id', $id)->setInc('downloads');
        $other = \think\Db::name($table)->where('id', $id)->find();     
        //下载,返回下载路径。
        $http_type = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') || (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')) ? 'https://' : 'http://';  
        $http_front = $http_type.$_SERVER['HTTP_HOST'];       
        $sourcefileUrl = $http_front.$other['sourcefile'];

        $this->success(__('Operation completed'), null,['sourcefileUrl' => $sourcefileUrl]);//注意这是要返回给前端的内容
    }
0 条评论

发布
问题