How to upload files in grails webflow? - grails

I try to build a grails webflow containing a step which has a file upload field in it. Can't find a clear example. How can I do it?
The form looks like this:
<g:form method="POST" action="upload" enctype="multipart/form-data">
<input type="file" name="myFile" />
<input type="submit" value="Upload! " />
</g:form>
The flow looks like this:
def rgdpsRequestFlow = {
chargeCheck{
on("upload"){
println "Hello file upload"
}.to("uploadSuccess")
}
uploadSuccess{
}
}
When I submit the form I get
HTTP Status 404 - /webflowTest/mortgage/upload

It's a little unclear which part you're struggling with. If you need help with the file upload, you may want to try the Grails File Uploader plugin, or otherwise check out this step-by-step tutorial or use the uploadForm tag that James Kleeh has pointed out.
Namely, in the gsp you can add:
<g:uploadForm controller='yourControllerName' action='save'>
<input type='file' name='file'/>
<input type='submit'/>
</g:uploadForm>
Then handle it in the controller:
def save = {
def file = request.getFile('file').inputStream.text
file.transferTo(new File('someLocation/filename'))
}

The problem was I used g:form action attribute.
The correct way to use file upload with webflow is using g:submitButton
Here is the form code that works fine with my webflow:
<g:form method="POST" enctype="multipart/form-data">
<input type="file" name="myFile" />
<g:submitButton class="save" name="upload" value="Upload!"/>
</g:form>

Related

Grails file upload not using correct content-type

I created a file upload in Grails, but I can't find a way to get the request to use Content-Type:multipart/form-data. The requests are being sent with
Content-Type:application/x-www-form-urlencoded.
This is my form
<g:uploadForm controller='asset' action='upload'>
<label>Select file(s) to upload</label>
<input type='file' id='fileUpload' name='filesToUpload' multiple />
<g:submitButton name="upload" value="Upload"/>
</g:uploadForm>
In Config.groovy, grails.mime.types contains multipartForm: 'multipart/form-data' and grails.web.disable.multipart=false. I am using Spring Security, and the AssetsController is #Secured.
How do I get the requests to be sent with Content-Type:multipart/form-data?
if the <g:uploadForm >-Tag does not what's described in the manual:
http://grails.github.io/grails-doc/2.5.x/ref/Tags/uploadForm.html
Identical to the standard form tag except that it sets the enctype attribute to "multipart/form-data" automatically.
then you could try to use the normal <g:form >-Tag and add the enctype attribute or even don't use the tag at all. A simple HTML-Upload will work, too:
<form action="${g:createLink controller:'', action:''}" method="post" enctype="multipart/form-data">
<input type="file" name="fileupload">
<input type="submit" name="upload">
</form>

How do I get results (successful/not successful) from a g:uploadForm in a GSP file?

I'm trying to get the results from a g:uploadForm action I have in my GSP file. I need to upload the file, then after it's successfully uploaded I need to tell if the upload was successful, then display another area in the GSP file.
<g:uploadForm action="save" method="post">
<h1>
<g:message code="upload"/>
</h1>
<h5>
<g:message code="upload.message"/>
</h5>
<br/>
<input type="file" id="Upload" name="Upload" class="input"/>
<br/>
<g:formButton type="submit" buttonClass="btn-primary btn-lg" buttonTextCode="upload.button" />
</g:uploadForm>
I just need something to say if it was successful or not.
Is this something I need to handle in the controller and just post to the GSP after that? I'm new to grails and groovy.
It's pretty common to use flash scoped variables for these types of messages. In fact, if you look at the Grails documentation about uploading files you will see it does just that.
def upload() {
def f = request.getFile('myFile')
if (f.empty) {
flash.message = 'file cannot be empty'
render(view: 'uploadForm')
return
}
f.transferTo(new File('/some/local/dir/myfile.txt'))
redirect(render: 'uploadForm')
}
Using the above example you could then include the following in your uploadForm GSP page.
${flash.message} to display this.

Passing Connectionstring as parameter in SSRS through URL

I am working on an application using Classic ASP and SQL Server 2008 R2. We are using SSRS for the reports. Now the datasource changes depending on the user. I have been using a parameter for the connectionstring. It is working fine but the problem is the connectionstring shows in the URL. Is there a way to hide it? Or is there a better way.
Please Help.
Yes - change the method on your form to POST and use the Request.Form syntax instead of Request.QueryString:
<form id="myForm" method="post" action="myPage.asp">
<label for="txtBox">Type something</label>
<input type="text" id="txtBox" name="txtBox" />
</form>
<%
Dim value
value = Cstr(Request.Form("txtBox"))
If value <> "" then
'Do your processing
End if
%>
-- EDIT --
To be honest, though, I would not store my connection string on my form like this. You'd be far better off storing it in an Application level variable, like so:
Application("CON_STRING") = "...blahblahblah..."
This should be stored in the Application_OnStart event of the Global.asa* file.
*
Apologies for the link to w3schools - it was the best at the time!
-- EDIT 2 --
Try using an iframe to display the info...
<form id="frmRender" action="ABCD/ReportServer?/Reports/rptSalesReport.rpt"; method="post" target="_blank">
<input type="hidden" name="rs:Command" value="Render">
<input type="hidden" name="rc:LinkTarget" value="_blank">
<input type="hidden" name="rs:Format" value="HTML4.0">
<input type="hidden" name="rc:Parameters" value="False">
<input type="hidden" name="ConnectionString" value="<%=Session("ConnectionString")%>">
<input type="hidden" name="StartDate" value="<%=StartDate%>">
<input type="hidden" name="EndDate" value="<%=EndDate%>">
<a id="linkInfo" href="javascript:generateSsrs();">Generate Report</a>
<iframe id="ssrsReport" class="reportHeightWidth"></iframe>
</form >
<script language="javascript">
function genreateSsrs() {
document.getElementById("ssrsReport").src = "ABCD/ReportServer?/Reports/rptSalesReport.rpt?rs:Command=Render&rc:LinkTarget=top&rs:Format=HTML4.0&rc:Parameters=False&ConnectionString=<%=Server.URLEncode(Session("ConnectionString"))%>&StartDate=<%=StartDate%>&EndDate=<%=EndDate%>";
}
</script>
That's a rough version, but it's untested, so may need some tweaks.
In you code, use the below code
="Data Source="+Parameters!DatabaseServerName.Value+";Initial Catalog="&Parameters!DatabaseCatalogName.Value

Grails form submit causes 404

I have created a very simple form for signing up a user
<g:form name="signupForm" url="[controller:'users', action:'signup']">
<g:textField name="username" placeholder="Username" />
<g:passwordField name="password" placeholder="Password" />
<g:textField name="email" placeholder="Email" />
<g:actionSubmit class="right" value="Signup" action="update" />
</g:form>
When I click the submit button I get a 404 error The requested resource is not available. However, if I navigate to the exact same URL manually (or even just select the address bar on the 404 error page and press enter) then it works!
My controller looks like this, it's very simple.
class UsersController {
def signup() {
render "Hello World"
}
}
Sorry if this is a noob question, but I've looked all over the Grails docs and can't figure out why this is happening. Any help much appreciated. Thanks.
The g:actionSubmit has the parameter action="update" which will push it to the UsersController def update which isn't there so it will throw a 404.
You can remove the action="update" or add that action to the controller.
http://grails.org/doc/latest/ref/Tags/actionSubmit.html
There is also a g:submitButton you can use instead.
http://grails.org/doc/latest/ref/Tags/submitButton.html

Handling 2 buttons submit Actions in a single View/Form - ASP.NET MVC 2 RTM

I have a View in which the user is able to upload a file to the server.
In this view I also have 2 buttons: one to Upload a file and other to Download the last file imported.
In my Controller I created 2 action methods: Import and Export.
How could I manage to redirect each button click to the proper action method in my Controller?
I have tried Html.ActionLink:
<%= Html.ActionLink("Upload", "Import", "OracleFile")%>
<%= Html.ActionLink("Download", "Export", "OracleFile")%>
Html.ActionLink didn't do the trick. The action links were taking me to the right Action methods but they were generating a GET request. This way Request.Files.Count = 0.
I need a POST request.
Note: the most intriguing part is that the upload was working and all of sudden it stopped working. I've seen that some people are having the same problem with FileUpload tasks in which the Request.Files is always Empty. I think it's empty because you need a post to the server. Isn't it?
maybe this will give u the idea:
view:
<form enctype="multipart/form-data" method="post" action="/Media/Upload/Photo">
<input type="file" name="file" id="file" />
<input type="submit" name= "submitImport" value="Upload" />
<input type="submit" name = "submitExport" value="Download" />
</form>
controller:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Action (FormCollection formCollection)
{
if (formCollection["submitImport"] != null)
{
return Import(formCollection);
}
if (formCollection["submitExport"] != null)
{
return Export(formCollection);
}
}
the Export and Import are the appropriateactions
You have to use a "multipart/form-data" form, and submit the form. No ActionLink.
<form enctype="multipart/form-data" method="post" action="/Media/Upload/Photo">
<input type="file" name="file" id="file" />
<input type="submit" value="Upload" />
</form>
To generate a POST request for the upload, use the File Input form element and just post back to the server ala normal.
http://www.w3schools.com/jsref/dom_obj_fileupload.asp
Have a look at this blog post from Scott Hanselman.
http://www.hanselman.com/blog/ABackToBasicsCaseStudyImplementingHTTPFileUploadWithASPNETMVCIncludingTestsAndMocks.aspx

Resources