jquery mobile 表单提交 图片/文件 上传

jquerymobile 下面 form 表单提交 和普通html没区别,最主要是 <form 要加一个 data-ajax=‘false‘ 否则 上传会失败

1  html代码

<!doctype html>
<html>
<head>
    <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>
</head>

<body>

<div data-role="page">

<div data-role="header" data-position="fixed" style="background: #ff6932;color: #ffffff;text-shadow: none;">
        <h1>Jquery mobile 1.4</h1>
        <!--   <a href="#" class="ui-btn">返回</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="上22传" />
            </form>
        </div>
    </div>

<div data-role="footer" data-position="fixed" style="background: #ff6932;color: #ffffff;text-shadow: none;">
        <h4>中兴长天(南昌)信息技术有限公司</h4>
    </div>

</div>

</body>
</html>

2  服务端代码  新建一个servlet,修改 dopost代码

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");    
        Date date = new Date();//获取当前时间    
        SimpleDateFormat sdfFileName = new SimpleDateFormat("yyyyMMddHHmmss");    
        //SimpleDateFormat sdfFolder = new SimpleDateFormat("yyMM");    
        String newfileName = sdfFileName.format(date);//文件名称    
        String fileRealPath = "";//文件存放真实地址    
        
        String firstFileName="";    
        
        // 获得容器中上传文件夹所在的物理路径    如果按日期存放,则可以在files\\后面继续加 sdFolder+"\\"
        String savePath = this.getServletConfig().getServletContext().getRealPath("/") + "files\\";    
       /* System.out.println("路径" + savePath+"; name:"+name);    */
        System.out.println("路径" + 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");    
            // 获取多个上传文件    
            List fileList = fileList = upload.parseRequest(request);    
            // 遍历上传文件写入磁盘    
            Iterator it = fileList.iterator();    
            while (it.hasNext()) {    
            FileItem obit = (FileItem)it.next();  
                //如果是普通  表单参数
             
                if(obit.isFormField()){ //普通域,获取页面参数
                    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(obit instanceof DiskFileItem){  
                    DiskFileItem item = (DiskFileItem) obit;    
                    // 如果item是文件上传表单域       
                    // 获得文件名及路径       
                    String fileName = item.getName();    
                    if (fileName != null) {    
                        firstFileName=item.getName().substring(item.getName().lastIndexOf("\\")+1);    
                        String formatName = firstFileName.substring(firstFileName.lastIndexOf("."));//获取文件后缀名    
                        fileRealPath = savePath + newfileName + formatName;//文件存放真实地址    
                            
                        BufferedInputStream in = new BufferedInputStream(item.getInputStream());// 获得文件输入流    
                        BufferedOutputStream outStream = new BufferedOutputStream(new FileOutputStream(new File(fileRealPath)));// 获得文件输出流    
                        Streams.copy(in, outStream, true);// 开始把文件写到你指定的上传文件夹    
                        //上传成功, 
                        if (new File(fileRealPath).exists()) {    
                            //虚拟路径赋值    
                           // fileRealResistPath=sdfFolder.format(date)+"/"+fileRealPath.substring(fileRealPath.lastIndexOf("\\")+1);    
                            //保存到数据库    
                            System.out.println("上传成功了, 您还可以做其他操作");    
                            
                            //System.out.println("虚拟路径:"+fileRealResistPath);   
                            response.getWriter().write(fileRealPath.substring(fileRealPath.lastIndexOf("\\")+1)); 
                        }    
                             
                    }     
                }  
            }     
        } catch (org.apache.commons.fileupload.FileUploadException ex) {  
           ex.printStackTrace();    
           System.out.println("没有上传文件");    
           return;    
        }     
     /*  response.getWriter().write("1");    */
}

data-ajax="false" 是重点,终于解决了

时间: 2024-08-09 06:34:44

jquery mobile 表单提交 图片/文件 上传的相关文章

fileupload form表单提交(包括文件上传)

需要的jar包:commons-fileupload-1.3.1.jar 示例代码: package com.expai.servlet; import java.io.IOException; import java.io.PrintWriter; import java.util.ArrayList; import java.util.HashMap; import javax.servlet.ServletException; import javax.servlet.http.HttpS

node07---post请求、表单提交、文件上传

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> <form action="dopost" method="post" enctype="multipart/fo

WebApi发送HTML表单数据:文件上传与多部分MIME

5.3 Sending HTML Form Data5.3 发送HTML表单数据(2) 本文引自:http://www.cnblogs.com/r01cn/archive/2012/12/20/2826230.html By Mike Wasson|June 21, 2012作者:Mike Wasson | 日期:2012-6-21 Part 2: File Upload and Multipart MIME第2部分:文件上传与多部分MIME This tutorial shows how to

(转)WebApi发送HTML表单数据:文件上传与多部分MIME

5.3 Sending HTML Form Data5.3 发送HTML表单数据(2) 本文引自:http://www.cnblogs.com/r01cn/archive/2012/12/20/2826230.html By Mike Wasson|June 21, 2012作者:Mike Wasson | 日期:2012-6-21 Part 2: File Upload and Multipart MIME第2部分:文件上传与多部分MIME This tutorial shows how to

form表单系列中文件上传及预览

文件上传及预览 Form提交 Ajax 上传文件 时机: 如果发送的[文件]:->iframe, jQurey(),伪Ajax 预览 import os img_path = os.path.join('static/img', fafafa.name) with open(img.path, 'wb') as f: for item in fafafa.chunks(): f.write(item) function iframeSubmit(){ $('#ifm1').load(functi

工作中如何使用ajax提交form表单,包括ajax文件上传

提供一种方法就是利用jquery.form.js,我们是和java对接的后台. 代码如下: <input type="text" id="text1"> <input type="text" id="text2"> <input type="text" id="text3"> <form method="post" encty

Ajax 提交表单【包括文件上传】

利用js插件实现 <script src="@Url.Content("~/js/layer/jquery.form.min.js")"></script> 实例代码 前端: @using WebSearch.EFDB; @{ ViewBag.Title = "UploadFeeCertificate"; NoveltyProxy proxy = ViewData["Proxy"] as Novelty

form表单的file文件上传那些事

file API 客户端直接访问用户计算机的文件,2000以前,在表单添加了<input type="file">字段. file API是为給web开发提供安全的方式,以便在客户端更好访问用户的文件,字段的基础上加 了一些直接访问文件信息的接口-files集合.与fileReader类型读取文件的数据. files集合 name: 本地文件的名称 size: 文件的字节大小 type: 字符.文件的MIME类型 lastModifiedDate: 文件上次修改的时间(ch

通过AJAX和PHP,提交JQuery Mobile表单

通过AJAX和PHP,提交JQuery Mobile表单   File name: callajax.php <?php $firstName = $_POST[firstName]; $lastName = $_POST[lastName]; echo("First Name: " . $firstName . " Last Name: " . $lastName); ?> File name: index.php <!DOCTYPE html&