分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 网页技术

layerui上传文件

发布时间:2023-09-06 02:04责任编辑:林大明关键词:暂无标签

参考: 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上传文件

原文地址:https://www.cnblogs.com/lvlin241/p/9313317.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved