Grails and form input multiple - grails

I didn't find the part about this in the documentation, so I will be very happy if someone can help me =)
I have this form on my page to upload multiple pictures, using multiple for my input:
<g:uploadForm controller="photo" action="add" autocomplete="off">
<label for="files">Files to upload:</label>
<input type="file" id="files" name="files" multiple="multiple" />
<input type="hidden" id="MAX_FILE_SIZE" name="MAX_FILE_SIZE" value="300000" />
<g:submitButton name="add" class="save button medium" value="ADD" />
</g:uploadForm>
And now, I don't know how to "separate" files in my controller.
It is ok for one file, using request.getFile(..), but how can I handle the "multiple" property of my field ?
Thanks for reading,
Alexandre

You can do this within your controller:
List fileList = request.getFiles('files') // 'files' is the name of the input
fileList.each { file ->
println 'filename: ' + file.getOriginalFilename()
}
request.getFiles(<param>) returns a list of CommonsMultipartFile objects. You can use these objects to get the file names (like in the example) or the file content (file.getInputStream())

You got the answer, but this just for a record
request.multiFileMap?.each { name, map ->
//do the logic
}

Related

Copy and paste url parts in forms

How can I get a part of a string from the specific starting position to end. Like for example I want to copy v=mQUr2RkjykU from the given url:
http://www.youtube.com/watch?v=mQUr2RkjykU
If url is writen into form, how can i copy and paste that part.
And write it mysite.com/watch?xxxxxxxxxxx in a link thats generated.
<input id="Form" name="Form" type="text" /><input id="Button" type="button" value="" />
Thank you
You can use substr() and strpos()
In your case , you can check the position of '?' in your URL>>
$oldUrl = 'http://www.youtube.com/watch?v=mQUr2RkjykU';
$newUrl = substr($oldUrl, 1, strpos($oldUrl, '?')-1);
$newUrl would be your answer,
anyway , try to search more before asking question here :)

Automatically search website field and get resulting URL

I want to automatically search a string at http://www.drugbank.ca/ and get the resulting URL (the search field is at the top of the page). The website can't be searched just by manipulating the URL. Is there a server-based way to do this? I want to create my own webpage with an input field and button to "Search DrugBank for X and get the URL".
Thanks.
You need to get the contents of:
http://www.drugbank.ca/search?query=searchstring
You can't do this with javascript it's not allowed by the browser to query sites of different domains (due to: http://en.wikipedia.org/wiki/Same_origin_policy).
I would do it with php and create a file like searchDrugBank.php:
<?php
$urlContent = file_get_contents('http://www.drugbank.ca/search?query=' . $_GET['q']);
// process $urlContent however you want
?>
And then you put on your site:
<form method="get" action="searchDrugBank.php">
<input type="text" name="q" />
<input type="submit" value="Search drugbank"/>
</form>
(Since you asked)
To find what URL I was gonna query I went to the site, and looked at the form that was submitted when I pressed search (look at the source, or in it's easy to do "inspect element" on for example the searchbox or searchbutton).
I find that the form is:
<form accept-charset="UTF-8" action="/search" method="get"><div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="✓"></div>
<strong>Search:</strong>
<input id="query" name="query" placeholder="Search DrugBank" size="30" type="search">
<input name="commit" type="submit" value="Search">
Help / Advanced
</form>
Which means that when you press search, exactly what happends is that you will do a GET request, since method="get" and get request means to ask for an url, and if parameters is required, they should be in the URL (http://en.wikipedia.org/wiki/Query_string#Web_forms).
The URL that will be queried is /search since action is action="/search" the rest of the url will then be built using provided parameters here it's just:
<input id="query" name="query" placeholder="Search DrugBank" size="30" type="search">
And there you can see that name of the parameter that should be provided to do a search, namely "query"!

Amazon S3 Upload via Post and ASP.NET MVC

I've been trying in vain for 2 days now to do a simple upload to my Amazon S3 Bucket. Below is my rendered form:
<form action="http://s3.amazonaws.com/MYBUCKETNAME" method="post" enctype="multipart/form-data">
<input type="hidden" name="AWSAccessKeyId" value="MYACCESSKEY" />
<input type="hidden" name="acl" value="private" />
<input type="hidden" name="key" value="UserUploads/TestUser/${filename}" />
<input type="hidden" name="success_action_redirect" value="http://WWW.MYURL.COM/" />
<input type="hidden" name="policy" value="POLICY" />
<input type="hidden" name="signature" value="SIGNATURE" />
<div>
Please specify a file, or a set of files:
<input type="file" name="file" size="100" />
</div>
<input type="submit" value="Upload" />
</form>
and my policy document looks like this:
{
expiration = "2011-12-08T12:00:00.000Z",
conditions = [
["eq","bucket","MYBUCKETNAME"],
["eq","acl","private"],
["starts-with","$key","UserUploads/TestUser/"],
["eq","success_action_redirect", "HTTP://WWW.MYURL.COM/"]
]
}
I get the following error:
Code: AccessDenied,
Message: Invalid according to Policy: Policy Condition failed: ["eq", "bucket", "MYBUCKETNAME"]
Does anyone have any ideas please, I'm grabbing at straws here. Also not sure if my Bucket Policy and ACL is correct.
According to http://doc.s3.amazonaws.com/proposals/post.html#Access_Control
Matching a Particular Value
Description: There are certain fields that you want to match a
particular value, such as matching a bucket name or requiring that an
object is uploaded using the public-read access control policy. The
field value is case sensitive, but the name is not.
Syntax: There are two ways to require that the field fieldname matches
the string value. The value is case sensitive. If the value starts
with a dollar sign ($), the dollar sign must be escaped with a
backslash (\$)
[ "eq", "$fieldname", "S" ],
(Note the $ prefix).
So your policy should have $ in sets where you using "eq" format
Why not do this in C# with the S3 SDK (http://aws.amazon.com/sdkfornet/), it's a bit more secure.
Here's my blog post with more detail: http://bradoyler.com/post/3614362044/uploading-an-image-with-aws-sdk-for-net-c.
Cheers and Good luck.
I was getting the following error:
Invalid according to Policy: Policy Condition failed: [\"eq\", \"$bucket\"
After many hours, I learned that you can't have a bucket with uppercase letters. Changing the bucket to lowercase fixed it.

List Box Multiple Selected values

how to pass multiple selected list box values which are in a session in one ascx page to another ascx page
Code is
NameObjectCollection eeSearchString=new NameObjectCollection();
for(i = 0;i <= LbStatus.Items.Count - 1){
if LbStatus.Items(i).Selected {
eeSearchString.Add(new NameObjectPair("status", LbStatus.SelectedItem(i).Text));
}
}
And in another page can i access the status like this?
_status=Convert.ToString(eeSearchString.ItemValue("status"));
Pls let me know?? or do i need to use loop again???
I don't know about aspx but in HTMl you can declare an array for some elements.
For example:
Group XPTO
In the server, in my case, in PHP I only receive the Value of the ones that are selected inside the array.
Image you have this:
<input type="checkbox" name="group[]" value="1" /> Group 1 <br />
<input type="checkbox" name="group[]" value="2" /> Group 2 <br />
<input type="checkbox" name="group[]" value="3" /> Group 3 <br />
Imagine you've selected the Group 1 and 3. In the server you'll receive the array with the values of the group1 and 3.
group(1, 3);
Hope this helps with you're question =)
Regards,
Elkas
make use of session variable or make use of properties of the control, i.e create property and assign value.

Redirect user to directory based on form input

I know enough about the coding end of web design to be embarrassed by what I don't know. What I want to do is to have various print promotions in newspapers and what not along the lines of: for more information please visit www.mysite.com/2345.
If the visitor doesn't enter the entire url in the nav bar and ends up at the main index, I want to have a text field there so they can enter "2345", hit enter or submit, then be redirected to www.mysite.com/2345 wherein the folder's index page will load.
I usually search and find the coding info I'm looking for, but I can't figure out a concise way to search this particular problem. Can anyone help with this or point me in the right direction for help elsewhere?
Thanks.
Pretty simple with JavaScript, here's a working example:
<form onsubmit="location.href='http://www.mysite.com/' + document.getElementById('myInput').value; return false;">
<input type="text" id="myInput" />
<input type="submit" />
</form>
You can do it using javascript. Here's a terribly ugly example but should give you an idea.
<form>
<input type="text" id="number" name="number" />
<input type="submit" onclick="window.location = window.location + '/' + number.value; return false;"/>
</form>
Ideally you'd also handle it in whatever server side language you're using as well. Here's a PHP example:
<?
if(isset($_POST['number'])){
header('Location: http://www.yourdomain.tld/'.$_POST['number']);
exit;
}
?>
Very simple example with PHP, so that you can understand how it works. Very simple.
<?php
if (isset($_POST['bt']))
{
header("Location: http://localhost/" . $_POST['folder']);
}
?>
<html>
<form id="form1" name="form1" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="text" name="folder" id="folder" />
<input type="submit" name="bt" id="bt" value="Go To" />
</form>
</html>
This is you index.php File, in your htdocs/www folder.
The PHP notices when you click the button and it will redirect you to
http://www.yourdomain.com/what-you-have-writen-in-the-textfield
You can also do it with JavaScript, but with PHP it will work even if your visitor browser has JavaScript disabled.

Resources