• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Restricting user to upload a CSV file

 
Greenhorn
Posts: 1
Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 7645
178
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 439
Hibernate Eclipse IDE Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are welcome.

Happy ranching .
 
Do not meddle in the affairs of dragons - for you are crunchy and good with ketchup. Crunchy tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic