Add file download function

This commit is contained in:
kyg516 2016-09-19 19:57:10 +09:00
parent bffd4d7cad
commit ab00afa513
4 changed files with 428 additions and 380 deletions

View File

@ -6,6 +6,16 @@ class HomeController < ApplicationController
file_directory(@current_dir) file_directory(@current_dir)
end end
def file_download
@file_name = params[:file_name]
puts "file_name: " + @file_name
if File.exist?(@file_name)
#File.delete(@file_name)
puts "file exist!!"
send_file(@file_name)
puts "send success"
end
end
def make_directory def make_directory
current_dir = params[:current_dir] current_dir = params[:current_dir]
@ -15,7 +25,6 @@ class HomeController < ApplicationController
redirect_to '/home/index' redirect_to '/home/index'
end end
def delete_file def delete_file
file_name = params[:file_name] file_name = params[:file_name]
puts "rm #{file_name} -rf" puts "rm #{file_name} -rf"

View File

@ -39,7 +39,6 @@
</div> </div>
</div> </div>
<!-- page content --> <!-- page content -->
<div class="right_col" role="main"> <div class="right_col" role="main">
<div class=""> <div class="">
@ -162,8 +161,12 @@
<a style="cursor: pointer" onclick="change_directory('<%=@current_dir + "/" + t["name"]%>')"> <%=t["name"]%></a> <a style="cursor: pointer" onclick="change_directory('<%=@current_dir + "/" + t["name"]%>')"> <%=t["name"]%></a>
</td> </td>
<% else %> <% else %>
<td><i class="fa fa-file-o"></i> <td>
<a style="cursor: pointer"><%=t["name"]%></a> <form class="form_file_download" data-parsley-validate>
<i class="fa fa-file-o"></i>
<input type="hidden", value="<%=@current_dir + '/' + t['name']%>">
<button type="submit" style="border: none; background: none;"><%=t["name"]%></button>
</form>
</td> </td>
<% end %> <% end %>
<td><%=t["auth"]%></td> <td><%=t["auth"]%></td>
@ -275,7 +278,13 @@
new_tr += "<td style='color:#0d8ade;' class='sorting_1'><i class='fa fa-folder-open-o'></i> "; new_tr += "<td style='color:#0d8ade;' class='sorting_1'><i class='fa fa-folder-open-o'></i> ";
new_tr += "<a style='cursor: pointer' onclick='change_directory(" +'"' + cur + "/" + result.file[i].name +'"'+ ")'>" + result.file[i].name + "</a></td>"; new_tr += "<a style='cursor: pointer' onclick='change_directory(" +'"' + cur + "/" + result.file[i].name +'"'+ ")'>" + result.file[i].name + "</a></td>";
}else{ }else{
new_tr += "<td><i class='fa fa-file-o'></i><a style='cursor: pointer'> " + result.file[i].name + "</a></td>"; new_tr += "<td>";
new_tr += "<form class='form_file_download' data-parsley-validate>";
new_tr += "<i class='fa fa-file-o'></i>";
new_tr += "<input type='hidden', value='" + cur + "/" + result.file[i].name + "'>";
new_tr += "<button type='submit' style='border: none; background: none;'>" + result.file[i].name + "</button>";
new_tr += "</form>";
new_tr += "</td>";
} }
new_tr += "<td>"+result.file[i].auth+"</td>"; new_tr += "<td>"+result.file[i].auth+"</td>";
new_tr += "<td>"+result.file[i].size+"</td>"; new_tr += "<td>"+result.file[i].size+"</td>";
@ -287,13 +296,43 @@
$("#table_div").append(new_table); $("#table_div").append(new_table);
$('#datatable').dataTable(); $('#datatable').dataTable();
right_click(); right_click();
$(".form_file_download").submit(function(){
file_name = $(this).find("input").val();
$.ajax({
type: 'post',
url: '/file_download',
data: {file_name: file_name},
success: function(result){
console.log("post success");
}
});
return false;
})
} }
}) })
} }
</script> </script>
<script>
$("document").ready(function(){
$(".form_file_download").submit(function(){
file_name = $(this).find("input").val();
$.ajax({
type: 'post',
url: '/file_download',
data: {file_name: file_name},
success: function(result){
console.log("post success");
}
});
return false;
})
});
</script>
<!--Test Page specific JS --> <!--Test Page specific JS -->

View File

@ -44,7 +44,7 @@ Rails.application.configure do
# `config.assets.precompile` and `config.assets.version` have moved to config/initializers/assets.rb # `config.assets.precompile` and `config.assets.version` have moved to config/initializers/assets.rb
# Specifies the header that your server uses for sending files. # Specifies the header that your server uses for sending files.
# config.action_dispatch.x_sendfile_header = 'X-Sendfile' # for Apache config.action_dispatch.x_sendfile_header = 'X-Sendfile' # for Apache
# config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect' # for NGINX # config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect' # for NGINX
# Force all access to the app over SSL, use Strict-Transport-Security, and use secure cookies. # Force all access to the app over SSL, use Strict-Transport-Security, and use secure cookies.

View File

@ -14,7 +14,7 @@ Rails.application.routes.draw do
get 'home/index' => 'home#index' get 'home/index' => 'home#index'
post 'home/mkdir' => 'home#make_directory' post 'home/mkdir' => 'home#make_directory'
post 'home/delete' => 'home#delete_file' post 'home/delete' => 'home#delete_file'
post 'file_download' => 'home#file_download'
#Volume #Volume
get 'volume/index' => 'volume#index' get 'volume/index' => 'volume#index'