zoukankan      html  css  js  c++  java
  • layerui上传文件

    参考: http://www.layui.com/doc/modules/upload.html

    <1> 文件上传(以下函数必须要在js文件加载时执行)
        upload.render({
          elem: '#id',
          url: '/api/upload/',
          before: function(obj){ //obj参数包含的信息,跟 choose回调完全一致,可参见上文。
            layer.load(); //上传loading
          },
          done: function(res, index, upload){
            layer.closeAll('loading'); //关闭loading
          },
          error: function(index, upload){
            layer.closeAll('loading'); //关闭loading
          }
        });   
    <2> 文件下载 参考:https://memorynotfound.com/spring-mvc-download-file-examples/
    package com.memorynotfound.controller;
    
    import org.springframework.core.io.FileSystemResource;
    import org.springframework.core.io.Resource;
    import org.springframework.http.HttpEntity;
    import org.springframework.http.HttpHeaders;
    import org.springframework.http.MediaType;
    import org.springframework.stereotype.Controller;
    import org.springframework.util.FileCopyUtils;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.ResponseBody;
    import javax.servlet.http.HttpServletResponse;
    import java.io.*;
    
    @Controller
    @RequestMapping("/download")
    public class DownloadController {
    
        private static final String FILE_PATH = "/tmp/example.pdf";
        private static final String APPLICATION_PDF = "application/pdf";
    
        @RequestMapping(value = "/a", method = RequestMethod.GET, produces = APPLICATION_PDF)
        public @ResponseBody void downloadA(HttpServletResponse response) throws IOException {
            File file = getFile();
            InputStream in = new FileInputStream(file);
    
            response.setContentType(APPLICATION_PDF);
            response.setHeader("Content-Disposition", "attachment; filename=" + file.getName());
            response.setHeader("Content-Length", String.valueOf(file.length()));
            FileCopyUtils.copy(in, response.getOutputStream());
        }
    
        @RequestMapping(value = "/b", method = RequestMethod.GET, produces = APPLICATION_PDF)
        public @ResponseBody HttpEntity<byte[]> downloadB() throws IOException {
            File file = getFile();
            byte[] document = FileCopyUtils.copyToByteArray(file);
    
            HttpHeaders header = new HttpHeaders();
            header.setContentType(new MediaType("application", "pdf"));
            header.set("Content-Disposition", "inline; filename=" + file.getName());
            header.setContentLength(document.length);
    
            return new HttpEntity<byte[]>(document, header);
        }
    
        @RequestMapping(value = "/c", method = RequestMethod.GET, produces = APPLICATION_PDF)
        public @ResponseBody Resource downloadC(HttpServletResponse response) throws FileNotFoundException {
            File file = getFile();
            response.setContentType(APPLICATION_PDF);
            response.setHeader("Content-Disposition", "inline; filename=" + file.getName());
            response.setHeader("Content-Length", String.valueOf(file.length()));
            return new FileSystemResource(file);
        }
    
        private File getFile() throws FileNotFoundException {
            File file = new File(FILE_PATH);
            if (!file.exists()){
                throw new FileNotFoundException("file with path: " + FILE_PATH + " was not found.");
            }
            return file;
        }

    出现的问题:
    1. 上传后,出现文件下载框(一般为ie下),那么你需要在服务端对response的header设置 Content-Type: text/html
      response.addHeader("Content-Type","text/html");
    2. 如果上传后,文件名称回显为乱码
      response.addHeader("Content-Type","text/html;charset=utf-8");

  • 相关阅读:
    基于WPF的UI自动化测试[1] 自动化测试工具
    PSR
    技术型人员如何晋升项目经理
    HyperV 组件架构(1)—总体架构
    从技术到管理:工作转型后角色定位
    Web性能优化方案
    一个项目经理的一些个人体会
    从技术人才到项目管理的跨越
    研发项目经理的管理
    从程序类转向销售类工作,该如何进行?
  • 原文地址:https://www.cnblogs.com/lvlin241/p/9313317.html
Copyright © 2011-2022 走看看