• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Restricting user to upload a CSV file

 
Snehasish Jana
Greenhorn
Posts: 1
Java Tomcat Server
  • 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?
 
Tim Moores
Bartender
Posts: 2675
33
  • 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.
 
Geeta Puttappanavar
Ranch Hand
Posts: 43
  • 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
 
Saif Asif
Ranch Hand
Posts: 440
Eclipse IDE Hibernate 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: 43
  • 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.
 
Lalit Mehra
Ranch Hand
Posts: 384
Eclipse IDE Firefox Browser 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: 43
  • 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 ..
 
Amit Ghorpade
Bartender
Posts: 2854
10
Fedora Firefox Browser 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: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Amit,

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

Happy ranching .
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic