This is my main page for the download file pop-up file path selection box Summary hope can help to need friends.
The first kind: JS in the Actionxobject
This method does not make an introduction because using IE compatibility is too bad to be used.
The second type: hyperlinks
If your file is displayed directly on the page, you can use <a href= "" >**</a>.
If your file does not appear on the page by clicking the button to implement the Window.location.href after the file name is window.location.href equivalent to localhost://port number/project name
"Parent.location.href" is a page jump from the previous layer
"Top.location.href" is the outermost page jump
js get current URL, host address project Root path
//Get current URL, such as: http://localhost:8080/Tmall/index.jsp var curwwwpath=window.document.location.href;
// Gets the directory after the host address, such as:/tmall/index.jsp var pathname=window.document.location.pathname; var pos= Curwwwpath.indexof (pathName);
//Get host address, such as: http://localhost:8080 var localhostpaht= Curwwwpath.substring (0,pos);
//Get the project name with "/", such as:/tmall var projectname=pathname.substring (0, PATHNAME.SUBSTR (1). IndexOf ('/') +1);
Disadvantage: Only one file can be downloaded at a time
the third: jfilechooser
Paste code directly:
string path=null;
File F=null;
try{flag=fc.showopendialog (NULL);
The catch (Headlessexception head) {System.out.println ("Open File Dialog error!");
} if (flag==jfilechooser.approve_option) {//Get the file F=fc.getselectedfile ();
Path=f.getpath ();
}//To get the selected folder//To determine whether there are other directories, you can do this file Dir=new file (path);
Obtain the file name of the file under the change directory, if not, Filesname.length () =0 string[] 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.
Select files (Pro, pay attention to their differences oh smug) JFileChooser FC = new JFileChooser ();
Fc.setdialogtype (jfilechooser.files_only);
Fc.setdialogtitle ("Select File");
Fc.setmultiselectionenabled (FALSE);
Fc.showsavedialog (FC); if (Fc.getselectedfile () ==null) {return null;} return Fc.getselectedfile (). GetPath ();
Disadvantage: Users must install Java
The fourth favorite kind of HTML:
1. <a href= "javascript: onclick=" Down1 (); return false; " > Single File Download </a><br/><br/>
2. <a href= "javascript: onclick=" Down2 (); return false; " > Multi-File download </a><br/><br/>
3. <a href= "javascript: onclick=" Down3 (); return false; " > Multi-File Download custom name </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; /** * Use this method to download a single file while supporting download properties * @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:", IsP
Ath = Contentorpath.lastindexof (".") >-1;
Initialize Click event evt.initevent ("click");
Add File Download Name alink.download = FileName; If it is path or dataurl direct assignment//If it is file or other content, use Blob conversion 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 situation isimg && ifr.contentWindow.document.write (" Save File/alert (Ifr.contentWindow.document.body.innerHTML) if (bool) {Ifr.contentWindow.document.execCom
Mand (' 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) {//select download function var downer = H5down? downloadfile:iedownloadfile; To determine the type, process the download file name if (Files instanceof Array) {for (var i = 0, L = files.length; i < L; i++)//Bug handling down
ER (parseURL (files[i]), Files[i], true);
else if (typeof files = = "string") {Downer (parseURL (files), files);
else {//object for (var file in files) Downer (file, Files[file]);
}
}
})(); function Down1 () {downer ().
/file/test.txt "); The function down2 () {downer (). /file/test.txt ",".
/file/test.txt "]); function Down3 () {Downer ({"1.txt":). /file/test.txt "," 2.jpg ":".
/file/test.jpg "}); }