Vietnamese version
The uploading file is an activity frequently. You can use this activity to upload document files (for examples: doc, docx, pdf, png...) or data files (for examples: csv, xls, xlsx, odt,..). There are many ways to upload a file, I will show you my way in this post.
Assume you have a portlet named uploadfile-portlet. This portlet contains view.jsp and UploadFilePortlet.class. Following is the detail of them:
File view.jsp includes some JSP code lines as following:
File view.jsp chứa các dòng code JSP as:- Line 6th: This line generates action value and assign for action attribute of the form.
- Line 9-13th: These lines contain a form with an input component. As you see, this form includes a special attribute, that is the enctype attribute. If you don't include this attribute, the form will not send file data.
- Line 10th: There is the input tag. The value of type attribute is "file", this component will send file data after submitting. The value of name attribute is a unique in this form, this attribute will be used when reading file data in the controller.
UploadFilePortlet.class called controller, this class contains a method with @ActionMapping annotation. This method will get the file data and do something depend on your opinions. Following is the detail of UploadFilePortlet:
We focus some special lines:
We will take a look at some special lines:
We will take a look at some special lines:
- Line 10th: The mission of this line is conversion ActionRequest into UploadPortletRequest.
- Line 21rt: The purpose of this line is getting file data from UploadPortletRequest. The parameter of getFile method is name value of input tag (line 10th in view.jsp).
- Now, you can do anything with your file.
Click here to download source code
Note by author:
This way applied for MVCPortlet but it doesn't apply for Spring Framework. I will show you the way upload file for Spring framework in next post.
Không có nhận xét nào:
Đăng nhận xét