2014-12-29 1 views
2

나는 Rails에서 새로운 기능을 제공합니다. JSON 응답을 ZIP 형식으로 압축하고 사용자가 특정 URL을 클릭하면 다운로드하려고합니다. 내 컨트롤러 코드는 아래ZIP에서 JSON 응답 압축 및 다운로드 사용자가 레일에서 h URL을 누를 때

api :GET, '/tv/latest', 'Get latest tvs.' 
    error :code => 401, :desc => "Unauthorized" 
    description "Get latest tvs." 
    example 'curl -i -H "Accept: application/json" -d "auth_token=nu8vh5GqmVxMHurhRyYz" -X GET http://localhost:3000/tv/latest' 

def latest 
    @tvs = Tv.where(:approved_cd => 0).order('tvs.first_air_date DESC').page(params[:page]).per(12) 
    count = Tv.where(:approved_cd=>0).count 

    respond_to do |format| 
    format.json { render :json =>{:tvs=>JSON.parse(@tvs.to_json(request_type: "index")), :count=>count}} 

    end 
    end 

내가이 노력하고 있어요.

def latest 
    @tvs = Tv.where(:approved_cd => 0).order('tvs.first_air_date DESC').page(params[:page]).per(12) 
    count = Tv.where(:approved_cd=>0).count 

    require 'rubygems' 
    require 'zip' 

    folder = "Users/me/Desktop/stuff_to_zip" 
    input_filenames = ['image.jpg', 'description.txt', 'stats.csv'] 

    zipfile_name = "/Users/me/Desktop/archive.zip" 

    Zip::File.open(zipfile_name, Zip::File::CREATE) do |zipfile| 
     input_filenames.each do |filename| 
     # Two arguments: 
     # - The name of the file as it will appear in the archive 
     # - The original file, including the path to find it 
     zipfile.add(filename, folder + '/' + filename) 
     end 
     zipfile.get_output_stream("myFile") { |os| os.write "myFile contains just this" } 
    end 

    respond_to do |format| 
    format.json { render :json =>{:tvs=>JSON.parse(@tvs.to_json(request_type: "index")), :count=>count}} 
    format.zip { send_file file } 
    end 
    end 

그렇게 내가 zip 형식의 @tvs 배열을 얻을 수 있습니다하십시오 수정하고 난

http://localhost:3000/tv/latest 

감사

+0

내가 그렇게 질문했다 많은 어려운 질문 ??? 아무도 아직 안돼 ...... !!!! – Faysal

+0

간단한 유닉스 명령을 사용하여 "tar -zcvf archive.tar.gz directory /"파일을 압축 할 수 있습니다. – RubyOnRails

+0

내 코드를 고칠 수 있습니까 ??? 스카 이프를 줄 수 있니? – Faysal

답변

0

를 쳤을 때 다운로드이 시도 :

def latest 
    @tvs = Tv.where(:approved_cd => 0).order('tvs.first_air_date DESC').page(params[:page]).per(12) 
    count = Tv.where(:approved_cd=>0).count 
    request.env['HTTP_ACCEPT_ENCODING'] = 'gzip' 
    respond_to do |format| 
    format.json { render :json =>{:tvs=>@tvs, :count=>count}} 
    end 
end 
+0

제 경우에는 작동하지 않습니다. – Faysal

관련 문제