Javascript 在ajax响应中获取文件路径时如何强制下载文件

Javascript 在ajax响应中获取文件路径时如何强制下载文件,javascript,jquery,ajax,laravel,file,Javascript,Jquery,Ajax,Laravel,File,我想在我的服务器上下载文件,我尝试了以下代码,但文件未下载。我把多个文件合并成一个文件,合并后我想下载结果文件。现在文件合并到result.docx中,但不会自动下载 我在ajax响应中返回完整的文件路径 剧本 $("#download_specs").click(function(){ var slug = this.getAttribute('data-id'); $.ajax({ url:"

我想在我的服务器上下载文件,我尝试了以下代码,但文件未下载。我把多个文件合并成一个文件,合并后我想下载结果文件。现在文件合并到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'

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

function\u下载url内容(url){
常量a=document.createElement('a');
a、 href=url;
a、 下载='';
a、 style.display='none';
文件.正文.附件(a);
a、 单击();
文件.body.removeChild(a);

}
success:function(data){document.location=data;}
可以工作,这取决于服务器-尽管-您的服务器没有使用可从浏览器访问的URL进行响应-因此,这就是您必须解决的问题。尝试此解决方案但workedit不是解决方案,问题在于服务器返回的结果。。。浏览器无法访问该路径(服务器上运行的浏览器除外)。。。换句话说,服务器发送了错误的URL(甚至没有发送URL),可能需要添加一个属性
download
,因为浏览器可以打开文件而不是像这样下载使用您的解决方案但不工作“$”(“#download_specs”)。单击(function(){var slug=this.getAttribute('data-id'))$.ajax({url:{url('merge-document')}}),方法:'POST',标题:{X-CSRF-Token':$('meta[name=“CSRF-Token”]')).attr('content')},数据:{slug:slug},成功:函数(数据){alert(数据);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就在这里,对于某些默认情况下不下载的文件,需要“下载”属性。另外,Abdullah,我已经更新了代码,希望它现在能为您工作。
window.location=url
在文件链接上有适当的下载头就足够了