Jkmvc uses servlet3.0's javax.servlet.http.Part
for uploading files, and improves into a convenient API to handle the upload file.
vim src/main/resources/upload.properties
# upload directory, where the uploaded file save, without postfix "/"
uploadRootDirectory=/var/www/upload
encoding=gbk
# forbidden file extensions, splited by `,`
forbiddenExt = jsp,jspx,exe,sh,php,py
# domain to visit uploaded file
uploadDomain=http://localhost:8081/jkmvc/upload
Configuration item | usage |
---|---|
uploadRootDirectory | The root directory for upload, which jkmvc saves the upload files. To access these files, you need to setup a file server on this diretory. |
uploadDomain | Domain name to access to upload files |
The form is defined with enctype =" multipart / form-data "
property.
<form class="form-inline" action="<%= req.absoluteUrl("user/uploadAvatar/" + user.getId()) %>" method="post" enctype="multipart/form-data">
<div class="form-group">
<label for="avatar">avatar</label>
<input type="file" class="form-control" id="avatar" placeholder="avatar" name="avatar">
</div>
<button type="submit" class="btn btn-default">Upload</button>
</form>
/**
* upload avatar
*/
public fun uploadAvatar()
{
// find a user
val id: Int = req["id"]!!
val user = UserModel(id)
if(!user.isLoaded()){
res.renderHtml("use [" + req["id"] + "] not exists")
return
}
// check and handle upload request
if(req.isUpload){ // check upload request
user.avatar = req.storePartFileAndGetRelativePath("avatar")
user.update()
}
// redirect to detail page
redirect("user/detail/$id");
}
After the file upload, it needs to be downloaded. There are 2 ways to download the file:
Call res.render (file: File)
in the Controller, to return file to the browser
We can use apache / nginx to provide file download service, by specify the upload directory as http root directory
location ~ \.(gif|jpg|jpeg|.js|.css)$ {
root /var/www/upload;
index index.html index.htm;
}