Primefaces File Upload - Make sure user uploads files before leaving page - primefaces

I am using PrimeFaces File Upload with mode="advanced" and multiple="true" just like the demo shows here https://www.primefaces.org/showcase/ui/file/upload/multiple.xhtml?jfwid=50dd6
In this case, the user uploads multiple files and is shown the files to edit the list. The user must then click "upload" to upload the files. If they fail to click upload and submit the form on the page, the files don't get uploaded. I know about the auto="true" option, but we want to keep the ability to edit the list.
How can I prevent form submission if the user has pending file uploads?

You can block the submit button by checking the number of files in the onclick attribute:
<h:form>
<p:fileUpload widgetVar="upload" .../>
<p:commandButton onclick="return PF('upload').files.length===0" .../>
</h:form>
Of course it would be better to extend this check and show a message to the user if they forgot to upload any files. For example by adding a message to a p:growl component:
PF('growl').add({detail:'Please upload files first',severity:'warn'});
See also:
Add message to p:growl using Javascript

Well I don't know if you are using some other "dirty" form logic but can't you just set your dirty flag in the onAdd JS function of the FileUploader?
onAdd="setDirty(true);"
Then you can use onBeforeUnload to display the dirty dialog like in this post: What is the easiest way to detect if at least one field has been changed on an HTML form?

Related

Suppress Upload folder alert

So I have an input that allows the selection of a folder. I have it all working correctly, my tag for reference is below:
<input #selectfolder type="file" class="hidden-file-select" [(ngModel)]="selectedFolder" webkitdirectory mozdirectory msdirectory odirectory directory/>
My question is, once I select the folder, the browser (I think) creates a confirmation dialog like so:
Is there any way I can suppress this dialog, as I want to handle the confirmation in my app?

HTML form with GET method to PDF file ignores query string

I have this simple web form which has just a single button so the user can open a static PDF file when they click it.
Now that I've updated the PDF file, I updated the query string timestamp cache-busting parameter so they see a new version of the file.
<form action="Path/To/My/PDF Document.pdf?v=1234" target="_blank">
<button>Get your PDF here!</button>
</form>
Well, the PDF opens up alright, but the querystring is automatically reduced to just the question mark.
What opens up is: Path/To/My/PDF Document.pdf?
This shows the original version and not the new one, and I'm wondering what is the matter with this process?
*I know method="GET" is not in my example, but it does the same with or without it.
Give this a go:
<form
action="Path/To/My/PDF Document.pdf"
method="GET">
<input type="hidden" name="v" value="1234" />
<button>Get your PDF here!</button>
</form>
Key points:
specify the method as GET
provide the query-string parameter as a hidden input, with the name set to the name of the query-string
The reason this works is because of how forms process input data.
Demo: https://jsfiddle.net/1tszbe5o/
Recommended reading for WHY this works: https://developer.mozilla.org/en-US/docs/Learn/Forms/Sending_and_retrieving_form_data
TLDR: when you use the GET method, any form data provided with form inputs will be included in the query-string of the HTTP request resulting from the form action.
Why does it get stripped out in the first place? Because the form data was empty, hence the query-string was empty also ;)

Glassfish how to prevent direct access to a html file

I am running glassfish 3.1
Say I have 2 html files: a.html b.html and their urls are:
http://localhost:8080/mysite/a.html
http://localhost:8080/mysite/b.html
I only want a user to click a link from within a.html to get to b.html. if a user try to directly access b.html using http://localhost:8080/mysite/b.html, he should be redirected back to a.html.
How do i achieve this? I read something about .htaccess file. Does glassfish support this where should I find this file and modify it to suit my needs as stated above?
when user clicks link on a.html to go to b.html, save a flag in database(using javascript for click -> database for save ). On loading b.html, first check the flag(database for retrieval -> javascript to check), if untrue, go back to a.html, else show b.html.
Depending on how your users would move from page a to b. What you could do is instead of clicking a link going to page-b have a button.
Then that button can be part of a form with a hidden value... and check on page-b if that value was posted if not then redirect to page-a for example.
page-a:
<form action="b.php" method="post">
<input type="submit" name="a_submit" value="Go to page b">
</form>
top of page-b:
<?php
if(isset($_POST['a_submit'])){
///they came from page a
}else{
header('Location:a.html');
}
?>
With these two pieces of code you can implement exactly what you wanted. The only catch is you have to rename your b.html to b.php this is because only php files can handle php scripts. But do not worry there is no down side of using a php file instead of html; they act exactly the same but they only also allow php scripts to be ran.

html single submit button used for two different tasks

I've a html button which I want for two different tasks on a single press event. First, when I press the submit button it should insert data to the database (which I've done) and the another task is to render to another page. How can I make this button do these two functions simultaneously?
You can do this by specyfing action attribute of form element:
<form method="post" action="page.jsp?action=save">
...
<input type="submit" value="Submit!" />
</form>
After clicking submit button user is redirected to page.jsp (it may be the same page) and in that file you can get value of action variable: if it's save then execute code responsible for writing to db. After that code you place page-rendering code.
It's the simplest solution, but maybe not the most elegant.
"Just" do it then.
someDAO.save(someData);
response.sendRedirect(newURL);
That's basically all you need to execute in your servlet (or JSP if you're still abusing it as a controller).

HtmlInputFile through post action

I have a web page which have 3 controls:
form using the post command.
in the form i have an input file control named "myFile".
a button
the upload process works just fine, until I'm trying to post the form and handle the upload in another form.
Request["myFile"] and request.Params["myFile"] gave me nothing
It's the Request.Files collection you need - look it up in the .NET Framework docs:
Request.Files["myFile"]
Also make sure that the enctype attribute of your upload form is set correctly - it should be "multipart/form-data" if the form contains file inputs.
Maybe this will help
<input type="file" size="50" id="ipFile" runat="server"/>
runat="server" gives you access tot the HtmlInputFile structure