参考: 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上传文件的更多相关文章

  1. IE8/9 JQuery.Ajax 上传文件无效

    IE8/9 JQuery.Ajax 上传文件有两个限制: 使用 JQuery.Ajax 无法上传文件(因为无法使用 FormData,FormData 是 HTML5 的一个特性,IE8/9 不支持) ...

  2. 三种上传文件不刷新页面的方法讨论:iframe/FormData/FileReader

    发请求有两种方式,一种是用ajax,另一种是用form提交,默认的form提交如果不做处理的话,会使页面重定向.以一个简单的demo做说明: html如下所示,请求的路径action为"up ...

  3. asp.net mvc 上传文件

    转至:http://www.cnblogs.com/fonour/p/ajaxFileUpload.html 0.下载 http://files.cnblogs.com/files/fonour/aj ...

  4. app端上传文件至服务器后台,web端上传文件存储到服务器

    1.android前端发送服务器请求 在spring-mvc.xml 将过滤屏蔽(如果不屏蔽 ,文件流为空) <!-- <bean id="multipartResolver&q ...

  5. .net FTP上传文件

    FTP上传文件代码实现: private void UploadFileByWebClient() { WebClient webClient = new WebClient(); webClient ...

  6. 通过cmd完成FTP上传文件操作

    一直使用 FileZilla 这个工具进行相关的 FTP 操作,而在某一次版本升级之后,发现不太好用了,连接老是掉,再后来完全连接不上去. 改用了一段时间的 Web 版的 FTP 工具,后来那个页面也 ...

  7. 前端之web上传文件的方式

    前端之web上传文件的方式 本节内容 web上传文件方式介绍 form上传文件 原生js实现ajax上传文件 jquery实现ajax上传文件 form+iframe构造请求上传文件 1. web上传 ...

  8. Django session cookie 上传文件、详解

    session 在这里先说session 配置URL from django.conf.urls import patterns, include, url from django.contrib i ...

  9. 4 django系列之HTML通过form标签来同时提交表单内容与上传文件

    preface 我们知道提交表单有2种方式,一种直接通过submit页面刷新方法来提交,另一种通过ajax异步局部刷新的方法提交,上回我们说了通过ajax来提交文件到后台,现在说说通过submit来提 ...

随机推荐

  1. git分支在项目中管理

    实际项目中如何使用Git做分支管理 2018年06月24日 18:08:24 ShuSheng007 阅读数:9241   版权声明: https://blog.csdn.net/ShuSheng00 ...

  2. MyBatis 入门Demo

    新建数据库my_db,新建表student_tb id为主键,不自动递增. 不必插入数据. 下载MyBatis https://github.com/mybatis/mybatis-3/release ...

  3. Java基础知识笔记第八章:常用的实体类

    String类 String类位于lang包下 java会默认导入lang包下的类,所以可以直接使用,注意String是final类所以不能有子类 构造String对象: 常量对象:String常量也 ...

  4. ES5-严格模式

    在es5中可以开启一种严格模式的代码形式,开启方式是:将全局或者函数的第一条语句定义为:'use strict';. 如果浏览器不支持,会将其解析为一条普通语句,没有任何的副作用. 开启全局模式后会有 ...

  5. 使用tag标签是SEO优化的重要性是什么?

    使用tag标签是SEO优化的重要性是什么? tag标签是一种SEO技术,在网站优化的过程中,更准确.更具体地用关键词对文章进行分类,对SEO优化具有重要的作用. 但是,很多新人站长在发表文章时不太注意 ...

  6. [Android实例] Android网络收音机项目(内含源码)

    ======================帖子内容===================================最近喜欢听广播,但是搜索了一下,苦于网上没有Android的网络收音机项目的例 ...

  7. Vue - 动态绑定class

    <div :class="{active:item.index==1}" >  </div>

  8. Systemverilog for design 笔记(七)

    转载请标明出处 第一章 接口(interface) 1.1.    接口的概念 接口允许许多信号合成一组由一个端口表示. 1.2.    接口声明 //接口定义 Interface main_bus ...

  9. Gamma 矫正

    参考如下链接: https://www.zhihu.com/question/27467127

  10. error C2664: “FILE *fopen(const char *,const char *)”: 无法将参数 1 从“LPCTSTR”转换为“const char *”

    遇到这个问题,请打开本项目的Properties(属性)-------> Configuration Properties(配置属性)-------->General(常规)------- ...