Can not make fputcsv via ajax call - fputcsv

I am trying to output CSV file using another php file called from ajax from another page. Isn't that possible ? When I tried it didn't output CSV file. But via ajax response could see the proper values for CSV file spelled from the called page. Really getting crazy.

Related

How to Upload Video to Rails Server from App Inventor

My goal is essentially to upload a video file, like this, but with Ruby on Rails instead of PHP. I can successfully send JSON data to my server, but haven't been able to get file uploads working. The end objective is to have the file be in the server's /tmp directory, just like files uploaded via a webpage file_field_tag.
This image:
shows what I've tried so far. The result is that on the server, the parameter list is empty, unlike if you had used a file_field_tag. In the PHP example, they are able to get the contents of the file from the input stream... maybe there is something similar in Rails?
I know my API works, as I was able to successfully make a request using a JavaScript XMLHttpRequest, so I'm led to believe the solution involves working around what App Inventor offers for HTTP requests.
Edit: Removed unsupported header since the PHP example doesn't use headers anyways

Rails app - send_data sending garbled documents from DB

I am using a rails app to display data that is stored in a sql server db. Data entry is all done through an MS Access front end into the sql server db. The rails app is "read only" viewer of information entered via the access app.
In the access front end, I use a bound object frame to let users upload documents of various types (.pdf, .msg, .docx etc.) into the database. The object is stored in a varbinary field on my sqlserver. This works fine when using the Access front end. Docs are properly retrieved and displayed with the right host app.
The problem is when sending those same objects back to be viewed through the rails app.
When I try to send the binary data down through the browser the file as a file, the file received is unreadable by its host app. The file extension is correct for the file sent down, but the applicaiton cant' seem to read them. I'm definitely getting something coming down -- when I examine the files in notepad I see snippets of the text, but something is wrong with the files so they are unreadable by MS Word or PDF Reader, etc.
The code in the controller is pretty simple:
def view_word_doc
a=Attachment.find(params[:id])
send_data a.Document, :filename => 'flibber.docx'
end
(Document is the field int he table that has the varbinary)
This has the expected result in the browser, I am asked if I want to open or save the file, and it attempts use word to open the doc. However word says the document is corrupted. Similar results when I try to open .pdf, .msg files etc.
I also tried using send_file. In that case I get a controller error --
Encoding::UndefinedConversionError in AttachmentsController#view
"\x81" to UTF-8 in conversion from Windows-1252 to UTF-8
I have left the type to default, which I understand is Application/Octet-stream but I'm not so familair with what this means.
As I mentioned, this all works fine with the access front end. Just can't send it properly in rails.
It is possible that you have coded to receive the files as a GET method in your routes.rb so it attempts to display them inline/in the browser or potentially a link used in the view whereas it needs to be a button which uses the POST method by default.

Download multiple files with rails

I'm currently using PRAWN with latest release of rails and I can't figure out how to download multiple files with a single HTTP request. in my controller I have the following code:
Fill PDF with my stuffs...
PDF.render_file "foo.pdf"
send_file("foo.pdf")
And it download correctly the file but - if after that - I put another istance of the same code...it will execute only the last one, foo2
PDF.render_file "foo.pdf"
send_file("foo.pdf")
PDF.render_file "foo.pdf"
send_file("foo2.pdf")
Moreover, If at end...I wish to render another view, I can't do it.
Question is:
How can I download 2 different files in a single action inside controller and, finally, render a view?
You cant unless you zip them and then send back the zip file
You could use pdftk to combine the pdf's on the server, and send ONE pdf.
Other than that, to only way to get multiple file downloads is to have the broswer send multiple AJAX requests to the server, and each one end with send_file.
That would take some JavaScript, and you'd end up with multiple file download dialogs popping up on your screen.

Send csv file through REST in rails

I have to send .csv file to remote server through REST in rails.
I had tried post_form of Net::HTTP and made form as multipart true, by this file get transfer (seen in parameters at both side) but at server side i am not able to read it.
It gives error "No such file or directory."
Please any can tell me is there other way to transfer file.
After finding on this and tried many things i got good solution from that i can send csv file by REST web service.
here is solution...
https://github.com/jwagener/httmultiparty
Also can use REST Client for testing purpose.

rails how to know when send_file done

As it takes some time to prepare the content of the data to be downloaded, I want to show a message "Preparing file to download" when the user submits the request
Then when the file is ready, I use send_file to send the data
Once it's done, I need to clear the message
Thanks a lot
Here is what I would do.
1. Create a prepare_file action
First, I would create an action whose job would just be to create the file and rather than rendering HTML, it would render a JSON object with the name of the file that was created.
2. Use AJAX to call the prepare_file action
On the client, when the user clicks to download the file, you display the message, "Preparing download..." and just do an AJAX request to that action. The response you'll get back via AJAX is the name of the file created.
3. Redirect to the file download
Finally, you can hide the preparing download message and redirect the browser to the file download via JavaScript with the name of the file that was created. You would use send_file in this action.
I know that, in the question, you also wanted to be able to display to the user a message when the file is downloading and another message when it is finished. However, this isn't possible unless you write your own client-side download manager. The browser handles file downloads entirely and the user will see in the browser that the file is downloading and what the progress is. So, I understand where you're coming from, but you shouldn't feel like the user isn't being informed of what's happening.
At least with this solution, you're displaying a message to them when the file is being prepared and then once that message disappears, they'll get the download file dialog from the browser.
If you need help with actual code samples of how to do this, let me know.

Resources