[Study Notes] -- submitting an attachment to the Jfinal form and learning notes -- jfinal
Recently, the upload attachment in Jfinal is used in the project. The Jfinal Controller provides a getFile series method to upload files.
Here, the file upload and form parameters are submitted together. The page is similar:
Here, the form uses enctype = "multipart/form-data ". Let's take a look at the instructions in the Jfinal manual! You can see it.
Then there is a problem if it is form submission can not be submitted with ajax, the specific reason also found the http://www.oschina.net/question/941098_93842
This is why ajax cannot be used for submission. Then we can submit it on the page as follows:
Method 1:
Function tosave () {var data = $ ("# Form id "). serialize (); $ ("# <span style =" font-family: Arial, Helvetica, sans-serif; "> Form id </span> "). attr ("action", "equipment/saveOrUpdate? "+ Data); // jump to the specified contronler $ (" # Form id "). submit (); // submit };
Set an onclick event in the submit button. In this way, we can submit the parameters A, B, C, D... in the front-end form to the background!
Background code:
UploadFile file = getFile ("filedata", path, 200*1024*1024, "UTF-8"); // upload the file and obtain the form parameters
Okay. Now I can get the basic file upload and parameters in the form. In the past, after successfully saving, we were prompted that the page was successfully saved and refreshed.
But there is no returned value here. Maybe you can say you can redirect or forward it. Yes, I just want to say that my requirement here is to prompt that the storage is successful, refresh the list page, or prompt that the storage fails,
You also need to refresh the page.
So... I found the second method for submission.
$ ('# Form id '). ajaxForm ({target: '# output1', iframe: true, url: 'abc/saveorupdate', beforeSubmit: function () {var filename = $ ("input [name = 'filedata']"). val (); // alert ($ ("# equ_no "). val (); // alert (filename); // if (filename. endWith (". png ") | filename. endWith (". jpg ") // | filename. endWith (". jpeg ") | filename. endWith (". ico ") {// return window. confirm ("are you sure you want to process this file? "); //} Else {// alert (" upload an image file! "); // Return false ;//}}});
Here we use the jquery form plug-in ajaxForm to go to Baidu for specific usage. I am also catching up with pig.
Okay, let's continue with some of my improved background code.
String message = "Operation error"; String result = Setting. OPTION_FAILUE;
<Span style = "font-family: Arial, Helvetica, sans-serif;"> UploadFile file = getFile ("filedata", path, 200*1024*1024, "UTF-8"); // upload a file and get form parameters </span>
this.setAttr("result", result);this.setAttr("message", message);this.renderHtml("<script>parent.showResponse('"+result+"','"+message+"')</script>");
The renderHtml here is similar to the previous rendJson ();
Some of the Code after this involves other places, so I chose the relevant code. You can look at this idea. The writing level is limited. Do not spray it ~
The JFinal framework submits the form data from the page to the background.
Public class userAction extends Controller {
Public void list {
List <Record> user = Db. find ("select * from user ");
SetAttr ("user", user );
Render ("?. Jsp ");
}
}
If the <form> form submission type is enctype = "multipart/form-data" (with attachment)