zoukankan      html  css  js  c++  java
  • 文件下载 浏览器直接打开文件(前端控制)

    前端部分

    //得到拼接字符串
    function getFJInfo(name, url) {
    	return "<tr><td style=''><a  href='javascript:void(0)' οnclick='getDownFile("" + url + "","" + name + "")'>" + name + "</a></td></tr>"
    }
    //文件下载
    function getDownFile(url, name) {
    	var param = {
    		"url": url
    	};
    	$.ajax({
    		url: contextPath + '/product-label/file2Stream',
    		type: 'GET',
    		data: Base64.encode(JSON.encode(param)),
    		dataType: "text",
    		success: function(data) {
    			downloadFile(name, data)
    		}
    	})
    }
    //流处理触发下载事件
    function downloadFile(fileName, content) {
    	var aLink = document.createElement('a');
    	var blob = new Blob([content]);
    	var evt = document.createEvent("MouseEvents");
    	evt.initMouseEvent("click", true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
    	aLink.download = fileName;
    	aLink.href = URL.createObjectURL(blob);
    	aLink.dispatchEvent(evt)
    }
    

     后台controller处理,参考http://blog.sina.com.cn/s/blog_87216a0001014sm7.html

    /**
         * 返回流
         * 
         * @param requestMap 请求参数
         * @param response 返回对象
         */
        @RequestMapping(value = "/file2Stream", method = RequestMethod.GET)
        public void file2Stream(@Json Map<String, Object> requestMap, HttpServletResponse response) {
            try {
                String url = String.valueOf(requestMap.get("url"));
                // URL url =new URL(String.valueOf(requestMap.get("url")));
                InputStream iStream = getFileStream(url);
                OutputStream stream = response.getOutputStream();
                stream.write(StreamUtils.getBytes(iStream));
                stream.flush();
                stream.close();
            } catch (Exception e) {
                LOG.error("ProductSalesRecommendController.file2Stream  error | ({})", e);
            }
        }
     
        /**
         * HttpURLConnection获取网络路径的文件流
         * 
         * @param url 链接
         * @return InputStream
         * @throws IOException
         */
        private InputStream getFileStream(URL url) throws IOException {
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setConnectTimeout(5 * 1000);
            conn.setRequestMethod("GET");
            InputStream inStream = conn.getInputStream();
            return inStream;
        }
     
        /**
         * HttpClient获取网络路径的文件流
         * 
         * @param url 链接字符串
         * @return InputStream
         * @throws IllegalStateException
         * @throws IOException
         */
        private InputStream getFileStream(String url) throws IllegalStateException, IOException {
            HttpParams httpParams = new BasicHttpParams();
            HttpConnectionParams.setConnectionTimeout(httpParams, 5000); // 设置连接超时为5秒
            HttpClient client = new DefaultHttpClient(httpParams); // 生成一个http客户端发送请求对象
            HttpResponse httpResponse = client.execute(new HttpGet(url)); // 发送请求并等待响应
            HttpEntity entity = httpResponse.getEntity(); // 获取响应里面的内容
            InputStream inStream = entity.getContent();
            return inStream;
        }
    

      

    //得到拼接字符串function getFJInfo(name, url) {return "<tr><td style=''><a  href='javascript:void(0)' οnclick='getDownFile("" + url + "","" + name + "")'>" + name + "</a></td></tr>"}//文件下载function getDownFile(url, name) {var param = {"url": url};$.ajax({url: contextPath + '/product-label/file2Stream',type: 'GET',data: Base64.encode(JSON.encode(param)),dataType: "text",success: function(data) {downloadFile(name, data)}})}//流处理触发下载事件function downloadFile(fileName, content) {var aLink = document.createElement('a');var blob = new Blob([content]);var evt = document.createEvent("MouseEvents");evt.initMouseEvent("click", true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);aLink.download = fileName;aLink.href = URL.createObjectURL(blob);aLink.dispatchEvent(evt)}

  • 相关阅读:
    关于git你日常工作中会用到的一些东西
    require.context
    vue-cli3.0 使用postcss-plugin-px2rem(推荐)和 postcss-pxtorem(postcss-px2rem)自动转换px为rem 的配置方法;
    div实现富文本编辑框
    webpack-bundle-analyzer打包文件分析工具
    web页面调用支付宝支付
    ajax回调中window.open弹出的窗口会被浏览器拦截的解决方法
    Django 文件上传
    Django 序列化 前端通过ajax来获取数据库中的数据
    Django Form组件 基于源码的扩展
  • 原文地址:https://www.cnblogs.com/Andrew520/p/12382381.html
Copyright © 2011-2022 走看看