layerui上传文件

Posted lvlin241

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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");

 







以上是关于layerui上传文件的主要内容,如果未能解决你的问题,请参考以下文章

LayerUI、g.drawImage 下划线

layerui 页面定位和行定位

layerui 页面定位和行定位

layerUi与AJAX的一种思路

Alamofire 文件上传出现错误“JSON 文本未以数组或对象开头,并且允许未设置片段的选项”

layerui中如何点击表格行让复选选中并可以监听到复选框选中这个事件。