Post XML file content from client browser to server - ruby-on-rails

I want to get xml file content on the server by Ruby on Rails.
How can i do that without upload file. and send xml file content from client to server in user browser.
exactly what I want to do. writing an app to get gpx file content from a form. user should enter file path in the form and submit.

This is what happens when you upload a file in Rails, i.e. with a form like
<%= form_for #page, multipart: true do |f| %>
<%= f.file_field :name %>
<%= f.submit %>
<% end %>
you get a ActionDispatch::Http::UploadedFile object in params[:page][:name]. This UploadedFile contains among others a File object called tempfile.
You can read and parse the content of the uploaded XML file from this tempfile
Normally you would use a gem like paperclip or carrierwave to handle file uploads.

Related

How to submit a form multiple time in Ruby on Rails

I have a form made with simple_form that accept csv file to be converted it in an xlsx file (operating some operation to manipulate data).
<%= simple_form_for order, url: convert_orders_url, html: {multipart: true} do |f| %>
<%= f.file_field :file %>
<%= f.submit "Convert" %>
<% end %>
In my controller:
def convert
filename = #call to function that return the file path
send_file filename
end
The problem I have is that I need to upload multiple file one by one.
Anyway every time I need to reload the page manually cause I cannot submit the form multiple times and I can't reload the page in the controller because it raise a Double Render Error, because of the "send_file".
Is there a way to submit a form multiple time without reload the page in Ruby on Rails?
You can use remote: true on your form to submit the form using AJAX without reloading the page. However, with file uploads you'll find that remote: true won't work out of the box and you'll need something like remotipart.

Upload file from the specific directory (authomatically) [ruby on rails]

I created some simple tool where you can upload .csv file and display output on the website.
To do it you have press 'Browse', select file, and then press button 'Generate' and it will display output from .csv file (you can delete/edit each record).
My question is it possible to avoid selecting file manually?
I want to have only button 'Generate', after pressing it will automatically upload file and display. Of course file will be stored in some directory (directory and file name once defined won't be changed, only info in .csv could be different).
Any idea?
I found a way to read from file instead of browsing, in index.html.erb:
<%= form_tag import_reports_path, multipart: true do %>
<%= File.open("public/uploadfile/a_ruby_csv.csv") {|f| f.read} %>
<%= submit_tag "Generate report" %>
Before that I have:
<%= file_field_tag :file %>
<%= submit_tag "Generate report" %
How can I connect it with file.path ( ':file' )?
after I use open/read I have error - syntax error, unexpected ':'

How to retrive full path of browse file in rails3

How to get full files path of attached excel file in rails without any gem.
My view is
<%= form_for #hotel, :url => { :action => "create_by_excel_sheet" } do |f| %>
<%= f.file_field :excel_sheet %>
<%= f.submit %>
<%end%>
and when submit the form the attached excel file path I want to receive in create_by_excel_sheet action
I tried
params["hotel"]["excel_sheet"].tempfile.path then it return "/tmp/RackMultipart20130921-3387-1ffc97o" not a file path.
Ex: /file_path/file_name.file_extension
I am not sure but it seems to me that you get really full path and it starts from your system root /. You can easy to check it.
I think it is logical to put not yet saved files (but already uploaded) to system temp folder.

error 406, remote: true Ajax form is still causing a redirect

This has had me stumped all day.
My app provides for the upload of documents, once uploaded it runs create.js.erb and adds the new record to the page.
My HTML looks like this,
<div id="doc_upload">
<%= form_for Document.new, remote: true do |f| %>
<%= f.file_field :file, multiple: true, name: "document[file]" %>
<%= f.hidden_field :plot_id %>
<%= f.submit 'upload' %>
<% end %>
</div>
I have a create.js.erb file in the documents directory. I am presently using JQuery file upload and the drag and drop file upload causes no issues. However when I use this form to manually select a file and upload I get:
a 406 HTTP error
the page redirects to /documents
when i go back, the file has been successfully uploaded and saved.
The headers are as follows.
Request URL:http://localhost:3000/documents
Request Method:POST
Status Code:406 Not Acceptable
Request Headersview source
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8}
my controller is:
def create
#document = Document.create(params[:document])
respond_to do |format|
format.js
end
end
As I said this really has me scratching my head, as the Ajax request doesnt seem to be firing. Does this mean that it may be something to do with the unobtrusive JS no picking up the remote true data type. That is the only reason that I can think of the redirection.
If so how I confirm and fix this?
Any help would be great.
Ross
You have to add :multipart => true if you have file upload in your form.
but you cannot post a multipart form via AJAX

Rails - Using ajax to upload images

I've been trying to upload an image using ajax, but maybe I'm doing it wrong
<%= simple_form_for #note, remote: true do |f|%>
<%= f.text_area :content%>
<%= f.file_field :picture %>
<%= f.submit "save"%>
<%end%>
but every time I hit save... it is not doing the upload, it goes to the show action instead of displaying the alerts that I have in the file create.js.erb (it does have the multipart parameter in the form label.
Is it possible to upload an image using this way? or should I check the jquery-file-upload library?
Thanks in advance
Javier
You can not use Ajax for a file upload. But you can use e.g. the fileupload plugin for jQuery http://blueimp.github.com/jQuery-File-Upload/
I had the same problem --> form_tag with remote: true does not make an ajax request
You can use jquery.form plugin for file upload through ajax. Here is a link for that.
http://jquery.malsup.com/form/

Resources