路径 选择 弹框

来源:互联网 发布:哪个批发软件最好用 编辑:程序博客网 时间:2024/05/01 21:39

本篇主要是我针对页面的下载文件  弹出文件路径选择框的总结   希望可以帮到需要的朋友.


第一种:  js 中的ActionXObject   

该方法不做简介  因为只使用IE  兼容性太差  没有用过.

第二种: 超链接


如果你的文件直接显示在页面上  那你就可以用<a href=" ">**</a>.

如果你的文件不显示在页面上  是通过点击按钮 来实现 可以用window.location.href后加文件名称 前面的是window.location.href相当于localhost://端口号/项目名称  

"parent.location.href"是上一层页面跳转

"top.location.href"是最外层的页面跳转

JS获取当前网址、主机地址项目根路径

//获取当前网址,如: http://localhost:8080/Tmall/index.jsp
var curWwwPath=window.document.location.href; 

//获取主机地址之后的目录如:/Tmall/index.jsp 
var pathName=window.document.location.pathname; 
var pos=curWwwPath.indexOf(pathName); 

//获取主机地址,如: http://localhost:8080 
var localhostPaht=curWwwPath.substring(0,pos); 

//获取带"/"的项目名,如:/Tmall 
var projectName=pathName.substring(0,pathName.substr(1).indexOf('/')+1); 

弊端:每次只能下载一个文件

第三种: JFileChooser 
直接贴代码:
String path=null;File f=null;try{                 flag=fc.showOpenDialog(null);             }            catch(HeadlessException head){                  System.out.println("Open File Dialog ERROR!");            }                if(flag==JFileChooser.APPROVE_OPTION){             //获得该文件                f=fc.getSelectedFile();                path=f.getPath();         }    //以上获得选择的文件夹//若要判断其中是否还有其他目录,可以这样做File dir=new File(path);//获得改目录下的文件的文件名,如果没有的话,filesName.length()=0String[] filesName=dir.list();for(int i=0;i<filesName.length;i++){    File temp=new File(path+"/"+filesName[i]);    if(temp.isDirectory()){        break;    }}return path;   }2.  选择文件(亲 ,要注意他们的区别哦得意)JFileChooser fc = new JFileChooser();fc.setDialogType(JFileChooser.FILES_ONLY);fc.setDialogTitle("选择文件");fc.setMultiSelectionEnabled(false);fc.showSaveDialog(fc);if (fc.getSelectedFile()==null) {return null;}return fc.getSelectedFile().getPath();
弊端:用户必须安装java

第四种 最喜欢的一种
html:
1. <a href="javascript:" onclick="down1();return false;">单文件下载</a><br /><br />2. <a href="javascript:" onclick="down2();return false;">多文件下载</a><br /><br />3. <a href="javascript:" onclick="down3();return false;">多文件下载自定义名字</a><br /><br />
js:
var Downer = (function(files){var h5Down = !/Trident|MSIE/.test(navigator.userAgent);// try{// h5Down = document.createElement("a").hasOwnProperty("download");// } catch(e){// h5Down = document.createElement("a").download;// }/** * 在支持 download 属性的情况下使用该方法进行单个文件下载 * @param  {String} fileName * @param  {String|FileObject} contentOrPath * @return {Null} */function downloadFile(fileName, contentOrPath){var aLink = document.createElement("a"),evt = document.createEvent("HTMLEvents"),isData = contentOrPath.slice(0, 5) === "data:",isPath = contentOrPath.lastIndexOf(".") > -1;// 初始化点击事件evt.initEvent("click");// 添加文件下载名aLink.download = fileName;// 如果是 path 或者 dataURL 直接赋值// 如果是 file 或者其他内容,使用 Blob 转换aLink.href = isPath || isData ? contentOrPath: URL.createObjectURL(new Blob([contentOrPath]));aLink.dispatchEvent(evt);}/** * [IEdownloadFile description] * @param  {String} fileName * @param  {String|FileObject} contentOrPath */function IEdownloadFile(fileName, contentOrPath, bool){var isImg = contentOrPath.slice(0, 10) === "data:image",ifr = document.createElement('iframe');ifr.style.display = 'none';ifr.src = contentOrPath;document.body.appendChild(ifr);// dataURL 的情况isImg && ifr.contentWindow.document.write("<img src='" + contentOrPath + "' />");// 保存页面 -> 保存文件// alert(ifr.contentWindow.document.body.innerHTML)if(bool){ifr.contentWindow.document.execCommand('SaveAs', false, fileName);document.body.removeChild(ifr);} else {setTimeout(function(){ifr.contentWindow.document.execCommand('SaveAs', false, fileName);document.body.removeChild(ifr);}, 0);}}/** * [parseURL description] * @param  {String} str [description] * @return {String}     [description] */function parseURL(str){return str.lastIndexOf("/") > -1 ? str.slice(str.lastIndexOf("/") + 1) : str;}return function(files){// 选择下载函数var downer = h5Down ? downloadFile : IEdownloadFile;// 判断类型,处理下载文件名if(files instanceof Array) {for(var i = 0, l = files.length; i < l ; i++) // bug 处理downer(parseURL(files[i]), files[i], true);} else if(typeof files === "string") {downer(parseURL(files), files);} else {// 对象for(var file in files) downer(file, files[file]);}}})();function down1(){Downer("../file/test.txt");}function down2(){Downer(["../file/test.txt","../file/test.txt"]);}function down3(){Downer({"1.txt":"../file/test.txt","2.jpg":"../file/test.jpg"});}

目前没有发现弊端


0 0