Win a copy of Reactive Streams in Java: Concurrency with RxJava, Reactor, and Akka Streams this week in the Reactive Progamming forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar

Restricting user to upload a CSV file

 
Greenhorn
Posts: 1
Tomcat Server Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to restrict user only to upload CSV files in a perticular page.
I have tried with <input type="file" accept="text/csv" name="uploadCSV"> . But its not showing any filter. Is it possible to set a filter in HTML tag Or I need to write a javascript to validate that?
 
Saloon Keeper
Posts: 5809
146
Android Mac OS X Firefox Browser VI Editor Tomcat Server Safari
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JavaScript has no access to the file contents. And even if the "accept" worked, the user could upload a non-CSV file simply by giving it a .csv extension. No, you'll need to do the validation on the server.
 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Tim,

We too have the same the requirement.

Could you please tell me how to validate on the server using Spring?

Thanks,
Geeta
 
Ranch Hand
Posts: 440
Hibernate Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just to share my experience , I am using FilePicker.io in our application because, and its doing the job wonderfully. At some places I have to restrict users with file-extensions, and in some places of the application , users are restricted to a certain file size. And all the validation is done by filepickerio itself on the client side. Our main reason for using this was on the video upload module. We only accepted .mp4 and if user selected a large file (around 250mb with anyother extension) , then it was sheer stupidity to upload the 250mb file then perform validation of extension on the servlet side. FilePicker IO performs basic validation on the client side.
 
Geeta Puttappanavar
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Asif,

Thank you so much for the reply.

Can you please provide me some example? And

I request you to suggest me the validation using Spring.
 
Ranch Hand
Posts: 384
Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Geeta,

In this case you can check for the file extension once the user uploads the file and validate the same using the Java File API

You can also add a "file type" to the dialog box that opens up when you click to upload a file, i.e. using <input type="file">, i hope you are using this.

You can use the accept keyword of the <input> tag of HTML

try this : <input type="file" accept=".csv"/>

But this will only add a specific file type to the dialog box and won't restrict the entry to a single type.

hope this helps.

 
Geeta Puttappanavar
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Lalit,

Thanks for the quick reply.

I tried this

<form:input path = "file" type = "file" accept=".csv"//>


But I am getting 500 exception

org.apache.jasper.JasperException: /pages/FileUploadForm.jsp(16,60) Unterminated <form:input tag...........................



Again I tried by using regular expression which I got it from some forum



public class FileUploadController {
private static Pattern fileExtnPtrn = Pattern.compile("([^\\s]+(\\.(?i)(|csv|))$)");

boolean isCSV = false;
if(match.matches()){
isCSV = true;
}
else {
result.rejectValue("file", "please enter Csv File", "please enter Csv File");
}
}


This is working fine ..
 
Bartender
Posts: 2856
10
Firefox Browser Fedora Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The exception is self explanatory. It says your tag is not closed. Now carefully read the end tag, any JSP tag starts with <> and ends with </>.
 
Geeta Puttappanavar
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Amit,

Thank you. Now its working
 
Amit Ghorpade
Bartender
Posts: 2856
10
Firefox Browser Fedora Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are welcome.

Happy ranching .
 
He's dead Jim. Grab his tricorder. I'll get his wallet and this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!