How to realize the automatic download of files in IE

		function DownLoadPDF(attachGuid, filename,filepath) {
			var fso = new ActiveXObject("Scripting.FileSystemObject");
			if (!fso.FolderExists(filepath)) {
				fso.CreateFolder(filepath);
			}
			
			var curWwwPath = window.document.location.href;
			var pathName = window.document.location.pathname;
			var pos = curWwwPath.indexOf(pathName);
			var localhostPaht = curWwwPath.substring(0, pos);
			var projectName = pathName.substring(0, pathName.substr(1).indexOf(
					'/') + 1);
			var serviceUrl = localhostPaht + projectName + "/";
			var downloadRootUrl = serviceUrl;
			var strUrl = downloadRootUrl
					+ "rest/frame/base/attach/attachAction/getContent?attachGuid="
					+ attachGuid +'&isCommondto=true';
			var myFile = filepath + "\\" + filename; //The target path here can also be flexibly assigned through server-side code
			
			var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			xmlhttp.open("get", strUrl, false);
			xmlhttp.send();
			
			var ado_stream = new ActiveXObject("ADODB.Stream");
			ado_stream.Type = 1; //' 1=adTypeBinary
			ado_stream.open();
			ado_stream.Write(xmlhttp.responseBody);
			ado_stream.SaveToFile(myFile, 2); // save the file to disk
			ado_stream.close();
		}

The above is to use js to download the file, attachguid is the unique identifier of the file, which is to splicing the url of the file, file is the name of the file storage, fileFolder is the path of the stored folder

In this, two actionX controls of two IE browsers are used. In fact, if the IE browser has opened some permissions, it can operate the files on the client side, for example, delete the files on the client side:

	function deleteLocalFile(filepath){
			var fso = new ActiveXObject("Scripting.FileSystemObject");
			fso.DeleteFolder(filepath);
		}

This function can realize the deletion of the local file of the client by passing the path of a folder. In fact, this is to realize the control of our operation on the file of the client. If you can delete a folder, you can naturally create a folder:

	var fso = new ActiveXObject("Scripting.FileSystemObject");
			if (!fso.FolderExists(filepath)) {
				fso.CreateFolder(filepath);
			}

The above also realizes the creation of files on the client side, and then the user files can be saved:

var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			xmlhttp.open("get", strUrl, false);
			xmlhttp.send();
			
			var ado_stream = new ActiveXObject("ADODB.Stream");
			ado_stream.Type = 1; //' 1=adTypeBinary
			ado_stream.open();
			ado_stream.Write(xmlhttp.responseBody);
			ado_stream.SaveToFile(myFile, 2); // save the file to disk
			ado_stream.close();

There are two variables involved in this, one is the strUrl of the file, which means the address of the file, which can be http://.... Such an address myFile is the address where the file of the file is stored, including filepath+filename

In fact, this also realizes the use of IE browser to download and save files to a fixed address on the client side

In the implementation of the above operations are aimed at the IE browser, you also need to add some settings to the IE browser, such as the ActionX control needs to be enabled, allowing access to data sources, etc., these



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324415481&siteId=291194637