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上传文件的更多相关文章
- IE8/9 JQuery.Ajax 上传文件无效
IE8/9 JQuery.Ajax 上传文件有两个限制: 使用 JQuery.Ajax 无法上传文件(因为无法使用 FormData,FormData 是 HTML5 的一个特性,IE8/9 不支持) ...
- 三种上传文件不刷新页面的方法讨论:iframe/FormData/FileReader
发请求有两种方式,一种是用ajax,另一种是用form提交,默认的form提交如果不做处理的话,会使页面重定向.以一个简单的demo做说明: html如下所示,请求的路径action为"up ...
- asp.net mvc 上传文件
转至:http://www.cnblogs.com/fonour/p/ajaxFileUpload.html 0.下载 http://files.cnblogs.com/files/fonour/aj ...
- app端上传文件至服务器后台,web端上传文件存储到服务器
1.android前端发送服务器请求 在spring-mvc.xml 将过滤屏蔽(如果不屏蔽 ,文件流为空) <!-- <bean id="multipartResolver&q ...
- .net FTP上传文件
FTP上传文件代码实现: private void UploadFileByWebClient() { WebClient webClient = new WebClient(); webClient ...
- 通过cmd完成FTP上传文件操作
一直使用 FileZilla 这个工具进行相关的 FTP 操作,而在某一次版本升级之后,发现不太好用了,连接老是掉,再后来完全连接不上去. 改用了一段时间的 Web 版的 FTP 工具,后来那个页面也 ...
- 前端之web上传文件的方式
前端之web上传文件的方式 本节内容 web上传文件方式介绍 form上传文件 原生js实现ajax上传文件 jquery实现ajax上传文件 form+iframe构造请求上传文件 1. web上传 ...
- Django session cookie 上传文件、详解
session 在这里先说session 配置URL from django.conf.urls import patterns, include, url from django.contrib i ...
- 4 django系列之HTML通过form标签来同时提交表单内容与上传文件
preface 我们知道提交表单有2种方式,一种直接通过submit页面刷新方法来提交,另一种通过ajax异步局部刷新的方法提交,上回我们说了通过ajax来提交文件到后台,现在说说通过submit来提 ...
随机推荐
- Mac 如何导出ipa文件中Assets.car包中的切图
在之前 获取 AppStore 中 应用 的 IPA 包文件(Mac OS 13+)中获取到应用的 IPA 包,可以取出应用的部分图片(如 Logo),如果项目工程中把图片添加到 Assets.xca ...
- org.springframework.data.redis.RedisConnectionFailureException
org.springframework.data.redis.RedisConnectionFailureException: Cannot get Jedis connection; nested ...
- i.MX RT600之DSP调试环境搭建篇
恩智浦的i.MX RT600是跨界处理器产品,同样也是i.MX RTxxx系列的开山之作.不同于i.MX RT1xxx系列单片机,i.MX RT600 采用了双核架构,将新一代Cortex-M33内核 ...
- HA: Infinity Stones-Write-up
下载地址:点我 哔哩哔哩:点我 主题还是关于复仇者联盟的,这次是无限宝石的. 信息收集 虚拟机的IP为:192.168.116.137 ➜ ~ nmap -sn 192.168.116.1/24 St ...
- AStarPathFinding
简单例子: https://blog.csdn.net/wingfourever/article/details/9274091 修饰符解释: https://zhuanlan.zhihu.com/p ...
- UIAutomation 测试winForm
static void Main(string[] args) { Console.WriteLine("\n开始窗口程序自动化测试\n"); //启动被测试程序 string p ...
- PTA的Python练习题(一)
最近宅家里没事干,顺便把python给学了.教程和书看了一段时间,但是缺少练习的平台. 想起大一时候练习C语言的PTA平台,就拿来练手了. (因为没有验证码无法提交题目,所以自己用pycharm来做题 ...
- 12 JavaScript String对象 & Date对象
<script> var a = "string"; var b = new String("string"); var c = new Strin ...
- 11 JavaScript Number原始值&对象&科学记数法&范围&进制转换&溢出Infinity&NaN
JavaScript Number对象 是经过封装的能处理数字值的对象 由Number()构造器创建 只有一种数字类型 可以使用也可以不使用小数点书写数字 JavaScript原始值与对象: 在Jav ...
- text-align:center;在ie7下,父级加上会让block状态的子元素居中。
text-align:center:在ie7下,父级加上会让block状态的子元素居中.ie8以上不会.