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来提 ...
随机推荐
- HttpClient和HtmlUnit的比较总结以及使用技巧
本文转自: https://blog.csdn.net/zstu_cc/article/details/39250903 https://blog.csdn.net/zstu_cc/article/d ...
- cocoapods diff: /../Podfile.lock: No such file or directory 解决方案
在运行之前的使用 CocoaPods 工程时,有时会报错:diff: /../Podfile.lock: No such file or directory diff: /Manifest.lock: ...
- Linux批量装机(PXE)!
一 .PXE 简介PXE:Pre-boot Excution Environment,预启动执行环境PXE 是由 Intel 公司开发的网络引导技术,工作在 Client/Server 模式,允许客户 ...
- C/C++网络编程10——I/O复用服务器端实现select方式
#include <iostream> #include <cstdlib> #include <string> #include <cstring> ...
- unity 热更方案对比
现在一般使用的方案有:tulua&ulua.xlua.ILRuntime 对比: tulua&ulua 方案成熟,稳定第三方库支持 xlua 之前是为了热更修复线上bug的,腾讯发起的 ...
- 【原】postman设置环境变量和全局变量
一:设置环境变量 1. postman通过变换环境变量来快速变换环境地址. 2. 现可以将localhost:80信息添加至环境 3. 点击确定后,在首页可看到已添加的环境变量信息及设置的变量信息: ...
- Python学习笔记011
多行注释 '''字符串 ''' 除了用来多行注释还可以用来打印多行
- bash脚本编程
一.bash中的变量 变量类型: 本地变量:只对当前shell进程有效,对其子shell以及其它shell都无效; 定义变量:[set]Var_name="value" 变量赋 ...
- Android音频录制MediaRecorder之简易的录音软件实现代码(转)
原文:http://www.jb51.net/article/46182.htm Android音频录制MediaRecorder之简易的录音软件实现代码 这篇文章主要介绍了Android音频录制Me ...
- Linux centos7VMware Apache和PHP结合、Apache默认虚拟主机
一.Apache和PHP结合 httpd主配置文件/usr/local/apache2.4/conf/httpd.conf 启动报错 [root@davery ~]# /usr/local/apach ...