jquery Mobile Form Submit picture/File Upload

Source: Internet
Author: User

Jquerymobile the following form form submission and ordinary HTML no difference, the most important is <form to add a data-ajax= ' false ' otherwise the upload will fail

1 HTML code

<!doctype html>
<meta name= "viewport" content= "Width=device-width, initial-scale=1" charset= "UTF-8" >
<link rel= "stylesheet" type= "Text/css" href= "Jquerymobile1.4.0-green/zms-green.css"/>
<link rel= "stylesheet" href= "Jquerymobile1.4.0-green/jquery.mobile.icons.min.css"/>
<link rel= "stylesheet" href= "Jquerymobile1.4.0-green/jquery.mobile.structure-1.4.0.css"/>


<link rel= "stylesheet" href= "Css/my.css"/>


<script src= "Commond-plug/jquery.min.js" type= "Text/javascript" ></script>
<script src= "Jquerymobile1.4.0-green/jquery.mobile-1.4.0.min.js" type= "Text/javascript" ></script>
<title>HTML-ZMS</title>
<script>
$ (document). Ready (function () {


});
</script>


<stytle>


</stytle>


<body>


<div data-role= "Page" >


<div data-role= "header" data-position= "fixed" style= "background: #ff6932; color: #ffffff; text-shadow:none;" >
<!--<a href= "#" class= "ui-btn" > Back </a>-->
</div>


<div class= "Ui-content" >
<div class= "File-box" >
<form action= ". /servlet/phonegapup "method=" post "enctype=" Multipart/form-data "data-ajax=" false ">


<input type= "text" id= "Zms" name= "Zms" >
<input type= "text" id= "value1" name= "value1" >
<input type= "text" id= "value2" name= "value2" >




<input type= "File" accept= "Image/png" name= "Filefield" id= "Filefield"/>


<input type= "Submit" name= "Submit" class= "BTN" value= "on 22 biography"/>
</form>
</div>
</div>


<div data-role= "Footer" data-position= "fixed" style= "background: #ff6932; color: #ffffff; text-shadow:none;" >
</div>




</div>


</body>

2 service-side code create a new servlet, modify the Dopost code

public void DoPost (HttpServletRequest request, httpservletresponse response)
Throws Servletexception, IOException {
Request.setcharacterencoding ("UTF-8");
Date date = new Date ();//Get current time
SimpleDateFormat sdffilename = new SimpleDateFormat ("Yyyymmddhhmmss");
SimpleDateFormat Sdffolder = new SimpleDateFormat ("Yymm");
String NewFileName = Sdffilename.format (date);//File name
String Filerealpath = "";//file holds real address

String firstfilename= "";

Obtain the physical path of the upload folder in the container if it is stored by date, you can continue to add sdfolder+ "\ \" After files\\
String Savepath = This.getservletconfig (). Getservletcontext (). Getrealpath ("/") + "files\\";
/* SYSTEM.OUT.PRINTLN ("path" + savepath+ "; Name:" +name); */
SYSTEM.OUT.PRINTLN ("path" + Savepath);
File File = new file (Savepath);
if (!file.isdirectory ()) {
File.mkdirs ();
}

try {
Diskfileitemfactory FAC = new Diskfileitemfactory ();
Servletfileupload upload = new Servletfileupload (FAC);
Upload.setheaderencoding ("UTF-8");
Get multiple Upload files
List fileList = FileList = upload.parserequest (request);
Traversing the upload file to disk
Iterator it = Filelist.iterator ();
while (It.hasnext ()) {
Fileitem obit = (Fileitem) it.next ();
If it is a normal form parameter

if (Obit.isformfield ()) {//normal domain, get page parameters
String field = Obit.getfieldname ();

if (Field.equals ("value1"))
{
System.out.println (obit.getstring ("UTF-8"));

}
else if (field.equals ("value2")) {

System.out.println (obit.getstring ("UTF-8"));

}
}
If it is multimedia

if (obit instanceof Diskfileitem) {
Diskfileitem item = (diskfileitem) obit;
If item is a File Upload form field
Get the file name and path
String fileName = Item.getname ();
if (fileName! = null) {
Firstfilename=item.getname (). substring (Item.getname (). lastIndexOf ("\ \") +1);
String FormatName = firstfilename.substring (Firstfilename.lastindexof (".")); /Get file suffix name
Filerealpath = Savepath + newfilename + formatname;//file holds real address

Bufferedinputstream in = new Bufferedinputstream (Item.getinputstream ());//get file input stream
Bufferedoutputstream OutStream = new Bufferedoutputstream (new FileOutputStream (new file (Filerealpath)));//get file output stream
Streams.copy (in, OutStream, true);//start writing the file to the upload folder you specified
Upload successful,
if (new File (Filerealpath). Exists ()) {
Virtual path Assignment
Filerealresistpath=sdffolder.format (date) + "/" +filerealpath.substring (filerealpath.lastindexof ("\ \") +1);
Save to Database
System.out.println ("Upload successful, you can also do other operations");

SYSTEM.OUT.PRINTLN ("Virtual path:" +filerealresistpath);
Response.getwriter (). Write (Filerealpath.substring (filerealpath.lastindexof ("\ \") +1);
}

}
}
}
} catch (Org.apache.commons.fileupload.FileUploadException ex) {
Ex.printstacktrace ();
System.out.println ("No files uploaded");
Return
}
/* Response.getwriter (). Write ("1"); */
}

Data-ajax= "False" is the focus, finally solved the

jquery Mobile Form Submit picture/File Upload

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.