I have a form to upload a file:
<form action="/upload" method="post">
<input type="file" name="image">
<input type="submit">
</form>
and I am trying to see what is being submitted. In google chromes inspect element and when I use params.inspect in my back end the only form data that is submitted is {:image => "<submitted file name>"}. How can I get the actual image data? As per this website I should just recieve something in the format of:
{
"image" => {
:type => "image/png",
:head => "Content-Disposition: form-data;
name=\"myfile\";
filename=\"cat.png\"\r\n
Content-Type: image/png\r\n",
:name => "myfile",
:tempfile => #<File:/var/folders/3n/3asd/-Tmp-/RackMultipart201-1476-nfw2-0>,
:filename=>"cat.png"
}
}
I have no idea why that isn't happening. If someone can offer an explanation and correction that would be fantastic.
In order to upload files you need to set the enctype attribute on the form element to multipart/form-data:
<form action="/upload" method="post" enctype="multipart/form-data">
Related
I have next form:
<form action="/upload" enctype="multipart/form-data" method="POST">
<input name="example" type="file">
<input type="submit" value="Upload">
</form>
On server side I see that file of size zero is uploaded:
-----------------------------23370864791729106148808009039
Content-Disposition: form-data; name="example"; filename=""
Content-Type: application/octet-stream
-----------------------------23370864791729106148808009039--
How can I check that nothing was selected by user for this file field?
Add required to your input field to make sure an element has been uploaded.
<form action="/upload" enctype="multipart/form-data" method="POST">
<input required name="example" type="file">
<input type="submit" value="Upload">
</form
You can also check via javascript if there is a content given if you don't want to use the required attribute:
function hasContent() {
let fileValue = document.getElementsByName('example')[0].value;
alert(fileValue !== '' ? "has content" : 'no content');
}
<form enctype="multipart/form-data"method="POST">
<input name="example"type="file">
<input type="button"value="Upload"onclick="hasContent();">
</form>
You can use this code into your php file after submit:
if($_FILES['example']['tmp_name'] != null){
//Actions you needed
}
If it returns true it means the file is selected.
This just recently started happening and I haven't changed anything in my form code to cause a problem, previously when clicking the submit button for the form it would POST the data to the action URL and redirect the user. Instead now it redirects me to https://my.current.domain/my/path/https://action.com
Here's how the form is rendered:
<form
method="post"
action="https://checkout.simplexcc.com/payments/new"
target="_self"
className="hidden-form"
>
{Object.keys(formData).map((key, index) => {
const value = formData[key];
return <input key={key} type="hidden" name={key} value={value} />;
})}
<button
id="submit"
type="submit"
/>
</form>
So for example, when running my dev server I am getting redirected to
http://localhost:8000/https://checkout.simplexcc.com/payments/new
Instead of
https://checkout.simplexcc.com/payments/new
There's an invisible UTF8 character between the quote and the letter h in the snipped you posted.
After the user registration page, web clients will come to this page, which contains only a proceed button. But I want to skip this page and proceed to the payment page. Is there any way to do this action completely in the controller?
<form method="post" action="https://www.ccavenue.com/shopzone/cc_details.jsp">
<input type=hidden name=Merchant_Id value="<%= #Merchant_id %>">
<input type=hidden name=Amount value="<%= #Amount %>">
<input type=hidden name=Order_Id value="<%= #user_id %>">
<input type=hidden name=Redirect_Url value="<%= #redirect_url %>">
<input type=hidden name=Checksum value="<%= #checksum %>">
<input type="submit" class="Register_button" value="Proceed to payment">
</form>
You should do POST request from controller.
uri = URI.parse("https://www.ccavenue.com/shopzone/cc_details.jsp")
http = Net::HTTP.new(uri.host, uri.port)
form_data = {
"Merchant_Id" => #Merchant_id,
"Amount" => #Amount,
"Order_Id" => #user_id,
"Redirect_Url" => #redirect_url,
"Checksum" => #checksum
}
request = Net::HTTP::Post.new(uri.request_uri)
request.set_form_data(form_data)
response = http.request(request)
You can use httparty gem to do post request.
No, not the way you're wanting.
As others have said, you can POST from within your controller, and receive the response, but if you want to have the browser post to the remote page, it has to be through a form. A common solution is to submit the form on load via javascript. e.g.
<body onload="document.forms['myform'].submit();">
<form id="myform">
…
</form>
</body>
This might be years late response, but in case someone is still looking, I found this Repost plugin that could achieve this behaviour.
Could be used just like the normal redirect_to method in controller.
redirect_post('https://external-url.io/endpoint', params: {...})
There's no reason you couldn't post from a controller using uri and net/http in the Ruby standard library and handle the response from the server there.
Some good examples for using net/http can be found here. https://github.com/augustl/net-http-cheat-sheet/blob/master/post_form.rb
Still that's probably not the best way to handle this.
How should i post data to urban airship so it works correctly? I've tried this, but get error that i havent posted any data
<form enctype="application/json" method="POST" action="https://go.urbanairship.com/api/push/broadcast/">
<input type="hidden" name="username" value="my_app_key"/>
<input type="hidden" name="password" value="my_master_secret"/>
<input type="hidden" name="data" value='{
"aps": {
"badge": 15,
"alert": "Hello from Urban Airship!",
"sound": "cat.caf"
},
"exclude_tokens": [
]
}'/>
<button type="submit"/>
</form>
Browsers generally only support enctype "application/x-www-form-urlencoded" and "multipart/form-data". What's probably happening here is the form is not posting valid JSON, but instead url-encoded data where on of the keys is 'data' and the value is the long string of JSON.
Your best bet would be to call a javascript function when you press the submit button which posts the data with proper content-type and formatting. Using something like JQuery.post() http://api.jquery.com/jQuery.post/ would be pretty helpful.
What, precisely is submitted to the webserver in a file upload http request?
ie. If I have the following form,
<form action=%URL% method="POST" enctype="multipart/form-data">
<input name="fileinfo" type="file" /><br />
<input name="submit" type="submit">
</form>
What is the request that will be sent to the server?
will be something like below... just use print_r to see what is going into your script
echo "<pre>";
print_r($_GET);
print_r($_POST);
print_r($_FILES);
[fileinfo] => Array
(
[name] => MyFile.jpg
[type] => image/jpeg
[tmp_name] => /tmp/php/php6hst32
[error] => UPLOAD_ERR_OK
[size] => 98174
)