Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

How to handle csv file upload?

Chong Jing Hong
Contributor
June 16, 2021

Hi, I am developing a plugin that allows you to bulk create users on a server based on the data in a csv file. I've managed to get a velocity template showing with a form, as such:

<form id="form" action="#">                
<label for="file-input">Choose CSV file:</label>                
<input type="file" id="file-input" name="file-input" accept=".csv">                
<button type="submit" value="Submit">Submit</button>            
</form>

However, I'm stuck on the action java class. How do I handle the csv file upon submitting the form (extract the data from the csv file)? 

1 answer

0 votes
Alex Medved _ConfiForms_
Community Champion
June 17, 2021

You form needs to be 

enctype="multipart/form-data"

And your action should implement "ServletRequestAware" 

And you should receive a "MultiPartRequestWrapper" really into your action class (injected by xwork framework)

Then it is a matter of simple calls to "MultiPartRequestWrapper" instance to get uploaded file(s). I am pretty sure you can figure out the rest

Alex

Chong Jing Hong
Contributor
June 17, 2021

Hey Alex, thanks for replying!

It says here that ServletRequestAware is deprecated. I'm guessing I should do something like the answer in this question?

Alex Medved _ConfiForms_
Community Champion
June 17, 2021

Yes, you can use 

ServletActionContext.getRequest();
Chong Jing Hong
Contributor
June 21, 2021

Hi Alex, I've tried following this solution but I still can't get it to work. It just refreshes my page and nothing is logged. What could be the issue?

Alex Medved _ConfiForms_
Community Champion
June 21, 2021

Your "action" attribute should point at the action (class mapping) that handles the upload... not at #

Chong Jing Hong
Contributor
June 21, 2021

Thanks for replying Alex. The action attribute of the form does point to the servlet class.

 

Form from Velocity template:

<form class="aui" id="form" enctype="multipart/form-data" method="POST" action="${req.contextPath}/my-servlet">                
<label for="file-input">Upload CSV file:</label>                
<input class="upfile" type="file" id="file-input" name="file-input" accept=".csv">                
<button id="submit-btn" type="submit" value="Submit">Submit</button>            
</form>

 

atlassian-plugin.xml:

<servlet key="my-servlet" class="com.dso.intern.plugin.MyServlet" name="Bulk User Creation Tool Servlet">        
<description key="my-servlet-description">#</description>        
<url-pattern>/bulk-user-creator</url-pattern>    
</servlet>
Alex Medved _ConfiForms_
Community Champion
June 21, 2021

First of all the patterns dont match ("my-servlet" vs "/bulk-user-creator") and second, is that the mapping to servlets declared in plugins starts with 

/plugins/servlet/

Check Atlassian documentation on this... 

Like ahuachani likes this

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events