一聚教程网:一个值得你收藏的教程网站

热门教程

ajax实现文件异步上传并回显文件相关信息功能示例

时间:2022-06-25 15:57:58 编辑:袖梨 来源:一聚教程网

本文实例讲述了ajax实现文件异步上传并回显文件相关信息功能。分享给大家供大家参考,具体如下:

上传文件的信息

ajax提交

$("#subm").click(function(){
    var formData = new FormData();
    formData.append("appFile", document.getElementById("appFile").files[0]);
    alert("8888888888888888888888888");
    $.ajax({
      url: '${ctx}/appresources/fileUpload.shtml',
      type: "POST",
      data: formData,
      dataType: "json",
      contentType: false,
      processData: false,
      success: function (data) {
        alert("上传成功");
        $("#versionCode").val(data.versionCode);
        $("#appVersion").val(data.appVersion);
        $("#appPackageName").val(data.appPackageName);
      },
      error: function () {
        alert("上传失败!");
      }
    });
});

后台处理:

@ResponseBody
@RequestMapping("fileUpload")
public JSONObject fileUpload(MultipartFile appFile,Model model,HttpServletRequest request,HttpServletResponse response) {
    ServletContext
//........................省略
    String infos=ApkUtil.getApkInfo(needPath+"source"+File.separator+"apk"+File.separator+fileName);
    String[] info=infos.split(",");
    String versionCode=info[0];//版本号
    String versionName=info[1];//版本名
    String packageName=info[2];//包名
    String appName=fileName;
    AppResourcesFormMap appResourcesFormMap = getFormMap(AppResourcesFormMap.class);
    appResourcesFormMap.put("appName",fileName);
    appResourcesFormMap.put("appVersion",versionName);
    appResourcesFormMap.put("appPackageName",packageName);
    appResourcesFormMap.put("versionCode", versionCode);
    System.out.println(appResourcesFormMap);
    //model.addAttribute("appresources", appResourcesFormMap);
    JSONObject fromObject = JSONObject.fromObject(appResourcesFormMap);
    //String string = fromObject.toString();
    return fromObject;
}

热门栏目