gpt4 book ai didi

ruby-on-rails - send_file/send_data 不发送文件下载

转载 作者:数据小太阳 更新时间:2023-10-29 08:11:18 27 4
gpt4 key购买 nike

我正在使用 rails 3.2.22.4 和 ruby​​ 2.2.4。我正在测试 send_file 功能。我在服务器日志中收到“文件已发送”,但我看不到它正在浏览器中下载。单击保存按钮将调用此操作。

def save_trunk_logs
device_id = params['device_id']
trunk_name = params['trunk_name'] ||= "test"
data =""
file = "#{trunk_name}.txt"
trunk_logs = TrunkStatusLog.select([:LogType, :LogMessage, :EpochLoggedAtTime]).where(DeviceID: device_id)
File.open(file, "w+") do |aFile|
aFile.write("Trunk Name : #{trunk_name}\n")
aFile.write("*"*100)
aFile.write("\n")
aFile.write("Time Stamp"+"\t"+"Log Message\n")
trunk_logs.each do |msg|
data =format_log_messages msg
aFile.write("#{data}\n")
end
end
send_file file, :type => 'text/plain; charset=UTF-8', :disposition => 'attachment'
end

此操作由 button_to click 事件调用。

$("input[value='Download']").on('click', function () {
$.ajax({
type: "POST",
url: "/dm/trunk_test_tools/save_trunk_logs?device_id="+selectedTrunkDeviceId+"&trunk_name="+selectedTrunkDeviceName,
}
})
});

每次我点击下载按钮时都会进行 2 次调用。服务器端日志粘贴在下面

Started POST "/X/Y/save_trunk_logs?device_id=9&trunk_name=SIP_1" for      10.198.2.208 at 2016-10-24 12:58:58 +0530
Processing by ABCController#save_trunk_logs as */*
Parameters: {"device_id"=>"9", "trunk_name"=>"SIP_1"}
Sent file SIP_1.txt (0.0ms)
Completed 200 OK in 98.0ms (ActiveRecord: 62.0ms)

Started POST "/X/Y/save_trunk_logs" for 10.198.2.208 at 2016-10-24 12:58:59 +0530
Processing by TrunkTestToolsController#save_trunk_logs as HTML
Parameters: {"authenticity_token"=>"3HSxduOUwccwo="}
sent file test.txt (0.0ms)
Completed 200 OK in 17.0ms (ActiveRecord: 3.0ms)

我不明白为什么要打第二个电话以及从哪里打的。在第一个服务器调用中,它说文件已发送但未下载。在第二次调用中,文件被发送和下载。但没用。参数不正确。我不作为参数发送的真实性 token 。(跳过实际数据查询。)

任何帮助将不胜感激。提前致谢。

最佳答案

我不知道为什么这个方法有效。但符合我的目的。

$("#btn_id").on('click', function () {
window.location = ypur api url;
});

谢谢,

阿 git

关于ruby-on-rails - send_file/send_data 不发送文件下载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40132864/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com