I can't figure out how to upload files to the remote server in Mojolicious Lite. Here's some code, first, html form:
<form method='post' action='add_photo'>
<input type="file" name="upload" enctype="multipart/form-data">
<button type="submit" class="btn btn-default">Submit</button>
</form>
and here's an add_photo testing route:
post '/add_photo' => sub {
my $self = shift;
my %params;
my $file = $self->param('upload');
$params{filename} = $file->filename;
$params{filesize} = $file->size;
$params{worknamne} = $self->param('name');
$params{stone} = $self->param('stone');
$params{cat} = $self->param('cat');
$self->stash(params => \%params);
$self->render('test');
};
And here's the error message I recive:
Can't locate object method "filename" via package "name_of_file.jpg" (perhaps you forgot to load "name_of_file.jpg"?) at sv line 31
Thanks in advance!
The encoding type goes in the form tag, not the file input:
<form method="post" action="add_photo" enctype="multipart/form-data">
For a detailed example, just look at: Mojolicious::Lite #File uploads
Also, this question/answer has similar information: How Upload file using Mojolicious?
Related
Current version V2 reCaptcha returns a "POST" in CGI:params. Not exactly what the old V2 version did.
The up side is that any HTML form contents and the reCaptcha response come in at the same time. The down side is that the "g-recaptcha-response" value is a blob that requires local validation.
The "g-recaptcha-response" value looks something like this:
03AJzQf7OU1j33-wm7I73BItJg-l2COD-YwSjesvfej_5vy5c0r_LUhaDU1KsvU0BV0Rc-MHRbR4L17TNya1CqFtCJGulzvwTpKCfjwWcwqj2e3nFiqeropkXnYzwE78Eydr0jGi3OjZCKK71rmhOXZr0OA_nC8Cpd6aPaexqkrfLXdiXFPE7pQqc-qixYzVklb2MIuPyxw414kVbyHsbDr5p-pitK9cXvvPYK1Td7T_z6xnMUIuNN5zY3ArYzlAGexsSffucQRrzSKT_779In1QzwQppASux3-Z_xPLQgCLnRsJlwcby7gFFWXHSfAxV2ErpsiGWQcGa1
How can one decipher this response in Perl to be sure it is a genuine acknowledgement that the Turing test was solved correctly?
Do you know the module Captcha::reCAPTCHA::V2? I have succesfully used it and it is a great time saver. In plain CGI, the server side code that does the validation would look like:
my $rc = Captcha::reCAPTCHA::V2->new;
my $result = $rc->verify('<your secret key here>', param('g-recaptcha-response'), remote_host);
# Check the result
if( !$result->{success} ){
# The check failed, ignore the POST
next;
}
I assume that you have already created your reCAPTCHA keys, have your HTML form and an idea of how to create the rest of the server side code.
To help other people that may seek how to do this, this is a minimum form for the client side (this is based on bootstrap):
<form id="contact-form" method="post" action="contact">
<div class="row">
<div class="col-md-12">
<div class="form-group">
<label for="message">Comments</label>
<textarea id="message" type="text" name="message" class="form-control" placeholder="Your comments..." rows="4" required></textarea>
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<div align="center">
<button
class="g-recaptcha"
data-sitekey="<your site key here>"
data-callback="onSubmit">
Enviar mensaje
</button>
</div>
</div>
</div>
</form>
<script>
function onSubmit(token) {
document.getElementById("contact-form").submit();
}
</script>
Then, on the server side you would need something like (this is based on FastCGI; for CGI you would not use the loop):
use CGI::Fast qw(:standard);
use Captcha::reCAPTCHA::V2;
...
while(my $query = new CGI::Fast) {
...
if( request_method eq 'POST' ) {
my $rc = Captcha::reCAPTCHA::V2->new;
my $result = $rc->verify('<your secret key here>', param('g-recaptcha-response'), remote_host);
# Check the result
if( !$result->{success} ){
# The check failed, ignore the POST
next;
}
# Do something with the form
}
...
}
i've got the following form
<form action="http://test.mysideurl.de/upload.php" method="post" enctype="multipart/form-data" target="upload_target" id="MyUploadForm" >
<input name="file[]" type="file" class="file" multiple>
This form should send multiple files (if selected) to upload.php which looks like follows:
foreach($_FILES['file']['tmp_name'] as $key => $tmp_name){
$file_name = $key.$_FILES['file']['name'][$key];
$tmp_name = $key.$_FILES['file']['tmp_name'][$key];
$doc_id=process_image($file_name,$tmp_name,$doc_id);
}
console of firefox says:
Warning: Invalid argument supplied for foreach() in ...myurl... on line 15
Has anyone an idea, why it doesn't receive the files array?
Best regards Daniel
I am newbie to Perl. I am working on integration of Braintree Payment gateway to an existing project. I'm following guides given n Braintre, its so clear and simple but I can't get it done. I have downloaded the Sample project from Github through the link: https://github.com/braintree/braintree_perl_guide
I am getting server errors while executing the app.pl file,
Steps followed:
Created a Sandbox account, got the Merchant id, Public key, Private key and the Configuration code.
Installed necessary modules needed to run the Perl script
Edited the app.pl file given and given my Merchant id and the associated keys.
Edited the form.tt file and given the Configuration code.
In terminal executed the command Perl app.pl, while pressing enter I got,
Dancer 1.3124 server 79859 listening on locahost:3000
== Entering the development dance floor ...
From the browser connected to localhost:3000/ and have got the Braintree Credit Card Transaction Form.
Entered the details and clicked on submit button and suddenly got Error 500. "Unable to process your query".
How to resolve this please need your assistance.
My app.pl file
use strict;
use warnings;
use Dancer;
use Template;
use Net::Braintree;
my $config = Net::Braintree->configuration;
$config->environment("sandbox");
$config->merchant_id("wbjnmbzfnvs6zt56");
$config->public_key("smbbnwfhybb3h5ty");
$config->private_key("****");
get '/' => sub {
template 'form'
};
post '/create_transaction' => sub {
my $result = Net::Braintree::Transaction->sale({
amount => "1000.00",
credit_card => {
number => param("number"),
expiration_month => param("month"),
expiration_year => param("year"),
cvv => param("cvv")
},
options => {
submit_for_settlement => 1
}
});
my $success = $result->is_success ? "true" : "false";
if ($result->is_success) {
return "<h1>Success! Transaction ID: " . $result->transaction->id . "</h1>"
} else {
return "<h1>Error: " . $result->message . "</h1>"
}
};
Dancer->dance;
and my Form.tt file
<html>
<head>
</head>
<body>
<h1>Braintree Credit Card Transaction Form</h1>
<div>
<form action="/create_transaction" method="POST" id="braintree-payment-form">
<p>
<label>Card Number</label>
<input type="text" size="20" autocomplete="off" data-encrypted-name="number" />
</p>
<p>
<label>CVV</label>
<input type="text" size="4" autocomplete="off" data-encrypted-name="cvv" />
</p>
<p>
<label>Expiration (MM/YYYY)</label>
<input type="text" size="2" data-encrypted-name="month" /> / <input type="text" size="4" data-encrypted-name="year" />
</p>
<input type="submit" id="submit" />
</form>
</div>
<script type="text/javascript" src="https://js.braintreegateway.com/v1/braintree.js"></script>
<script type="text/javascript">
var braintree = Braintree.create('MIIBCgKCAQEA1E9xWJbLZeJVM9VuITkFTLbYhbxERQ64hKqDL495BTwJBJaTz+Y29U555ekRaAGeOzuRAP7wgsOhyKsvKn3w7i3JVakdPYJSLMKgjqrQOTxSxUaUn+qpb+etJiALC3lsckmh04Io/x8B16hZAKhjQTB1XUZtuEcT8Pe0ObPlnZpWPXayMGElyBQnS/AaLWE7VZiq7ezqiRl5atp7RatAUACvfTkpRdlBAw9XuaEpgKPLPWtj8CQCJk3LDYWjrLvwGKQ/LW/uPoBpfVmqbbSVe1sAaZcdMcPyqL0viYn3QSIkiLhz8SvCJExo4XaMBSGOENg2bCbBWNHSNiJzrdZA4wIDAQAB');
braintree.onSubmitEncryptForm('braintree-payment-form');
</script>
</body>
</html>
Per comments, the error was actually caused by a proxy issue. Bypassing the proxy resolved the problem.
I'm migrating from codeigniter to Fat-Free (F3) and trying to get my head around the quirks.
Regarding the following form:
<form ACTION = "<?php echo $_SERVER['PHP_SELF'];?>" METHOD="POST">
<input type="text" name="theirName" value="" required="required">
...
In standard PHP I get the POST value like this:
$name = $_POST['theirName'];
Or in codeignitor:
echo form_open('someclass/some_method_of_someclass');
$name = $this->input->post('name');
How do I get data from a form in a view in f3/fatfree?
You can get it from the Hive:
$name = $f3->get('POST.name');
I am used to get the echo message of the php script using the jquery ajax success function now I use a plugin but I dont know how to use it
my approach is this but definitely do not work how should the right way? It just go directly to my php script
$(function() {
$('form').ajaxForm(function() {
success: function(data){
$('.new-profile-pic').html(data);
}
});
});
HTML:
<p class="new-profile-pic">
<!--Image should be here-->
</p>
<form enctype="multipart/form-data" action="upload-image.php" method="post">
<input type="hidden" name="MAX_FILE_SIZE" value="100000">
<input class="profile-pic-name" name="uploadedfile" type="file">
<input type="submit">
</form>
PHP:
<?php
$target_path = "uploads/";
$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
echo '<img alt="" src="'.$target_path.'">';
} else{
echo "There was an error uploading the file, please try again!";
}
?>
You need to prevent the default action from occuring.
I believe this post has the answer you are looking for:
How do I submit a form using jquery (ajaxForm plugin) to an external site but not reload to a different page?
You need to use .preventDefault() on the submit event, or get it to return false on the form submit, e.g.:
<form enctype="multipart/form-data" action="upload-image.php" method="post" onsubmit="return false;">