send_file/send_data 没有发送文件供下载
send_file/send_data not sending file for downloading
我正在使用 rails 3.2.22.4 和 ruby 2.2.4。我正在测试 send_file 功能。我在服务器日志中得到 "file sent" 但我看不到它正在浏览器中下载。单击保存按钮将调用此操作。
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 单击事件调用。
$("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)
我不明白为什么要打第二个电话,从哪里打的。在第一个服务器调用中,它说文件已发送但未下载。在第二次调用中,文件被发送和下载。但没用。参数不正确。我不作为参数发送的真实性令牌。(跳过实际数据查询。)
如有任何帮助,我们将不胜感激。
提前致谢。
我不知道为什么这个方法有效。但符合我的目的。
$("#btn_id").on('click', function () {
window.location = ypur api url;
});
谢谢,
阿吉特
我正在使用 rails 3.2.22.4 和 ruby 2.2.4。我正在测试 send_file 功能。我在服务器日志中得到 "file sent" 但我看不到它正在浏览器中下载。单击保存按钮将调用此操作。
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 单击事件调用。
$("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)
我不明白为什么要打第二个电话,从哪里打的。在第一个服务器调用中,它说文件已发送但未下载。在第二次调用中,文件被发送和下载。但没用。参数不正确。我不作为参数发送的真实性令牌。(跳过实际数据查询。)
如有任何帮助,我们将不胜感激。 提前致谢。
我不知道为什么这个方法有效。但符合我的目的。
$("#btn_id").on('click', function () {
window.location = ypur api url;
});
谢谢,
阿吉特