文件上传

3 篇文章 0 订阅
2 篇文章 0 订阅

1、Form表单,enctype文件类型,


<form class="form-horizontal" action="/MyRen/resultmanage/uploadexcel" method="post" id="form1"

enctype="multipart/form-data" target="hidden_frame">

<input name="files" type="file" class="form-control" placeholder=""
style="padding: 0.2em 0.5em;" accept=".xls,.xlsx,.doc,.pdf">


<iframe id="hidden_frame" name="hidden_frame" style="display:none"></iframe>

因为form表单提交后总是会刷新界面,所以target可以实现刷新后的界面仍然是本界面。   input通过 accept属性限制文件类型。



2、ajax法。

new AjaxUpload('#upload', {
        action: baseURL + "registerathlete/uploadexcel",
        name: 'file',
        autoSubmit:true,
        responseType:"json",
        onSubmit:function(file, extension){
           
            if (!(extension && /^(xls|xlsx)$/.test(extension.toLowerCase()))){
                alert('只支持xls、xlsx格式的表格!');
                return false;
            }
        },




后台处理

@RequestMapping("/uploadexcel")
public void uploadExcel(
@RequestParam("result_id") String result_id,
@RequestParam("competition_id") String competition_id,
@RequestParam("additional_word") String additional_word,
@RequestParam(value = "files", required = false) MultipartFile file,
ModelMap model, HttpServletRequest request,
HttpServletResponse response) throws Exception, IOException {
// String path =
// request.getSession().getServletContext().getRealPath("/download/GradeDownLoad");
String path = Config.FILE_RESULT_UPLOAD_PATH;
String fileName = UUID.randomUUID() + "-" + file.getOriginalFilename();
File targetFile = new File(path, fileName);
if (!targetFile.exists()) {
targetFile.mkdirs();
}
System.out.println(path + "--" + fileName);
// 本地测试
file.transferTo(targetFile);


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值