【问题标题】:How to force download a file when get file path in ajax response在ajax响应中获取文件路径时如何强制下载文件
【发布时间】:2020-02-19 13:47:04
【问题描述】:

我想在我的服务器上下载文件我尝试了以下代码,但文件没有下载。我将多个文件合并为单个文件,然后在合并后我希望下载结果文件。现在文件合并在 result.docx 中,但不会自动下载

我在 ajax 响应中返回完整的文件路径

脚本

        $("#download_specs").click(function(){

            var slug = this.getAttribute('data-id');

            $.ajax({
                url:"{{url('merge-document')}}",
                method: 'POST',
                headers: {
                    'X-CSRF-Token': $('meta[name="csrf-token"]').attr('content')
                },
                data:{slug:slug} ,

                success:function(data){

                var zz=document.createElement('a');
                zz.href = data;
                }
            });
        });

控制器

    public function mergeDocuments(Request $request){

        $project_slug  = $request->input('slug');

        $userID = Auth::user()->id;
        $files = DB::table('project_specs_files')->select('completed_specs')->where('user_id',$userID)->where('project_slug',$project_slug)->get();

        $specs_array = array();
        foreach($files as $file){

            $ext = pathinfo(public_path(). '\\uploads\\complete_specs\\'.$userID.'\\files\\'.$file->completed_specs, PATHINFO_EXTENSION);
            if($ext == 'docx'){
                if (file_exists(public_path(). '\\uploads\\complete_specs\\'.$userID.'\\files\\'.$file->completed_specs)){

                    $specs_array[] =  public_path(). '\\uploads\\complete_specs\\'.$userID.'\\files\\'.$file->completed_specs ;
                }       
            } 
        }
        if(!empty($specs_array)){
            $dir = public_path(). '\\uploads\\complete_specs\\'.$userID.'\\files\\';
            $dm = new DocxMerge();
            $dm->merge($specs_array, $dir.'result.docx' );
        }else{

            return 'Files not supported for merge';
        }

        return $dir.'result.docx';

    }

作为回报,我收到了'D:\xampp\zerodocs\public\uploads\complete_specs\4\files\result.docx'

【问题讨论】:

  • success:function(data){ document.location = data; } 有效,具体取决于服务器 - 但是 - 您的服务器没有响应可从浏览器访问的 URL - 所以这是您必须解决的问题
  • 试过这个解决方案,但没有奏效
  • 这不是一个解决方案,问题在于服务器发回的结果......浏览器无法访问该路径(服务器上运行的浏览器除外)......换句话说 - 服务器正在发送错误的 URL(它甚至没有发送 URL)

标签: javascript jquery ajax laravel file


【解决方案1】:

最简单的解决方案是使用浏览器下载它而不是使用javascript fs,因为它有数据损坏的风险。
这个函数创建一个href标签并点击它来初始化下载。希望有帮助

function _downloadUrlContent(url) {
    const a = document.createElement('a');
    a.href = url;
    a.download = '';
    a.style.display = 'none';
    document.body.appendChild(a);
    a.click();
    document.body.removeChild(a);
  }

【讨论】:

  • 可能需要添加属性download,因为浏览器只能打开文件而不是下载
  • 像这样使用了您的解决方案,但无法正常工作` $("#download_specs").click(function(){ var slug = this.getAttribute('data-id'); $.ajax({ url:“{{url('merge-document')}}”,方法:'POST',标题:{'X-CSRF-Token':$('meta [name =“csrf-token”]')。 attr('content') }, data:{slug:slug} , success:function(data){ alert(data); const a = document.createElement('a'); a.href = data; a.download = ''; a.style.display = 'none'; document.body.appendChild(a); a.click(); document.body.removeChild(a);} }); }); `
  • Ivan is right here 对于某些默认情况下不下载的文件,需要下载属性。另外,阿卜杜拉,我已经更新了代码,希望它现在适合你。
  • window.location = url 在文件链接上使用适当的下载标题就足够了
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-12-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-01
  • 2016-04-24
  • 2011-10-29
相关资源
最近更新 更多