How to handle csv file upload?
Asked by Chong Jing Hong:

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)?


Answers:
author: Alex Medved _ConfiForms_
body: 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

comments: author: Chong Jing Hong
body: Hey Alex, thanks for replying!

It says [here](https://docs.atlassian.com/DAC/javadoc/opensymphony-webwork/1.4-atlassian-19/reference/webwork/action/ServletRequestAware.html) that ServletRequestAware is deprecated. I'm guessing I should do something like the answer in [this question](https://community.atlassian.com/t5/Answers-Developer-Questions/How-do-I-access-the-request-or-session-from-within-the-condition/qaq-p/550472)?


author: Alex Medved _ConfiForms_
body: Yes, you can use

```
ServletActionContext.getRequest();
```


author: Chong Jing Hong
body: Hi Alex, I've tried following [this solution](https://community.developer.atlassian.com/t/upload-a-file-to-server-from-an-add-on-admin-page/18609) but I still can't get it to work. It just refreshes my page and nothing is logged. What could be the issue?


author: Alex Medved _ConfiForms_
body: Your "action" attribute should point at the action (class mapping) that handles the upload... not at #


author: Chong Jing Hong
body: 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>
```


author: Alex Medved _ConfiForms_
body: 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...


Tags: confluence-development

confluence-server

csv

server