我的 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,而是在响应后立即开始下载)?