Rails4 アプリケーションのコントローラーに次のコードがあります。
# resolve the soft links and archive all photos for the current download
archived_photos = archive_current_download_photos(current_downloads_dir)
respond_to do |format|
if !File.exists?(archived_photos)
format.json {
render :nothing => true, :status => 500
}
else
format.json {
send_file(archived_photos, :disposition => "attachment", :filename => "photos.zip", :type => 'application/zip')
}
end
end
そしてJavaScriptでの取り扱い:
download photos request
ajax_download = (valuesToSubmit, download_url, http_method) ->
# AJAX
$.ajax
type: http_method
url: download_url #sumbits it to the given url of the form
data: valuesToSubmit # form
dataType: "json"
error: (error) ->
console.log("DEBUG: error")
return
success: (data) ->
console.log("DEBUG: success")
return
return false
この関数が呼び出されると、ダウンロードは行われませんが、データがダウンロードされていることがわかります。
Chrome に強制的にダウンロードを開始させるにはどうすればよいですか (link_to を使用せずに、応答後すぐにダウンロードを開始するように)。