how to download zip folder in ruby on rails?

时间:2016-02-03 04:10:26

标签: ruby-on-rails

I have download zip file in tmp folder now i have downloaded zip file but i am not downloaded zip file. My Code this type=>

=> Download folder

def download_folder

    @patient_id = params[:patient_id]
    @photovideos = Photovideo.where('patient_id = ?', User.encrypt(@patient_id))
    @time_filename = Time.now.strftime("%Y%m%d%H%M%S").to_s << ".zip"
    tmp_filename = "#{Rails.root}/tmp/"+ @time_filename

    Zip::File.open(tmp_filename, Zip::File::CREATE) do |z|
        @photovideos.each do |photovideo|
            item = User.decrypt(photovideo[:patient_file])
            file = "#{Rails.root}/public/patients/#{@patient_id}/"+ item
            if File.exists?(file)
                t = File.open(file)
                z.add(item, t)
            end
            end
    end
    File.chmod(0777, tmp_filename)
           send_file tmp_filename, :type=>"application/zip", :x_sendfile=>true

    tmp_filename.delete() #To remove the tempfile
    render json: { 'response' => 'Add files.'}

end

2 个答案:

答案 0 :(得分:0)

def download_folder
        @patient_id = params[:patient_id]
        @photovideos = Photovideo.where('patient_id = ?', User.encrypt(@patient_id))
        @time_filename = Time.now.strftime("%Y%m%d%H%M%S").to_s << ".zip"
        tmp_filename = "#{Rails.root}/tmp/"+ @time_filename

        Zip::File.open(tmp_filename, Zip::File::CREATE) do |z|
            @photovideos.each do |photovideo|
                item = User.decrypt(photovideo[:patient_file])
                file = "#{Rails.root}/public/patients/#{@patient_id}/"+ item
                File.chmod(0777, file)
                if File.exists?(file)
                    t = File.open(file)
                    z.add(item, t)
                end
                end
        end
        File.chmod(0777, tmp_filename)
        http_path = "http://"+"#{request.host}"+"/testfixirapi/tmp/"+"#{@time_filename}"
        render json: { 'response' =>  http_path }

    end

答案 1 :(得分:-1)

function download_patient_folder(patient_id){
    var $url = 'docsnaphomes/download_folder';
    $.post($url, {patient_id: patient_id}, function(data){
        window.location = data.response;
    });
}