function winopen(proj,rep,filename)
{
win3=window.open();
win3.document.write(filename);
win3.document.execCommand(‘saveAs’,1,filename);
}
Currently the function can only save the webpage with the filename printed on it. However, I hope to save the file instead of just a filename. The variables that i pass to the function contain:
1) proj –> contain project location
2) rep –> contain report location
3) filename –> contain filename of a specific file.eg: test.xls
Moreover, can the file be saved at the exact location specified by project+report location?eg: C://project/report , instead of the starting at Desktop in the save dialog box.
Experts, please help me!! Thank you in advance!
[i]Originally posted by Huang Youyi [/i]Do these 'users' have access to a local network in your company? Otherwise, if the website is online somewhere, how should the user's machine be able to save something to your local harddisk?
[B]It is because I am trying to do it on a webpage to allow user to select file and then save into a common directory in my company.[/B][/QUOTE]
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Example</title>
<SCRIPT LANGUAGE="JavaScript">
<!--
var errM;
var fso;
var fInp;
function copyF(proj,rep){
var success=0;
fInp=document.myform.file1.value;
var nName=document.myform.newName;
if(fInp==''){
alert('You didn\'t select a file to be copied!');
return false;
}
fso = new ActiveXObject("Scripting.FileSystemObject");
fileObj = fso.GetFile(fInp);
if(fileObj.Size>0){
fName=fileObj.Name;
newF=proj+rep+fName;
f1=fso.FileExists(newF);
f2=fso.FileExists(proj+rep+nName.value);
msg1='A file with the name "';
msg2='" already existsnin the destination folder "'+proj+rep+'"!nPlease enter a different name into the textfield.';
if(f1&&(nName.value==''||nName.value==fName)){
alert(msg1+fName+msg2);
nName.style.display='';
nName.focus();
return false;
}
else if(f2){
alert(msg1+nName.value+msg2);
nName.style.display='';
nName.focus();
return false;
}
else{
fileObj.Copy(newF);
success=1;
}
if(!f2&&nName.value!=''&&nName.value!=fName){
newF=proj+rep+nName.value;
fileObj.Copy(newF)
success=1;
}
}
if(success==1){
nName.style.display='none';
nName.value='';
alert('File "'+fInp+'" successfully copied to "'+newF+'".')
}
}
onerror=function(errnum){
msg='Error:n';
if(!fso.FileExists(fInp)){
msg='Error concerning file "'+fInp+'":n';
}
alert(msg+errnum);
return true
}
-->
</SCRIPT>
</head>
<body>
<form name="myform" action="#">
<input type="file" name="file1" size="50"><br>
<input name="newName" style="display:none"><br>
<input type="button" value="copy file to network drive" onclick="copyF('S:\project1\','report\',this.form.newName.value)">
</form>
</body>
</html>
Cheers - PitSo you are saying the <input name...> is actually a textfield for user to enter a new filename?[/QUOTE]This textfield only shows up, if the file selected in the <input type="file"> already exists in the destination folder. I put that into the script to avoid overwriting files. So if a file with the name of the one selected by the user already exists, the user can type a different name into the text field and the selected file will not be saved with the original name, but with the name specified in the text field.
str += "var errM;";
str += "var fso;";
str += "var fInp;";
str += "function copyF(proj,rep,filename,dispname){";
str += "var success=0;";
str += "fInp=filename;";
str += "var nName=dispname;";
str += "alert(proj+rep+filename);";
str += "if(fInp==''){";
str += "alert('You did not select a file to be copied!');";
str += "return false;";
str += "}";
str += "fso = new ActiveXObject('Scripting.FileSystemObject');";
str += "fileObj = fso.GetFile(fInp);";
str += "if(fileObj.Size>0){";
str += "fName=fileObj.Name;";
str += "newF=proj+rep+fName;";
str += "f1=fso.FileExists(newF);";
str += "f2=fso.FileExists(proj+rep+nName.value);";
str += "msg1='A file with the name ';";
str += "msg2=' already exists in the destination folder "+proj+rep+" !Please enter a different name into the textfield.';";
str += "if(f1&&(nName.value=='' || nName.value==fName)){";
str += "alert(msg1+fName+msg2);";
str += "nName.style.display='';";
str += "nName.focus();";
str += "return false;";
str += "}";
str += "else if(f2){";
str += "alert(msg1+nName.value+msg2);";
str += "nName.style.display='';";
str += "nName.focus();";
str += "return false;";
str += "}";
str += "else{";
str += "fileObj.Copy(newF);";
str += "success=1;";
str += "}";
str += "if(!f2&&nName.value!=''&&nName.value!=fName){";
str += "newF=proj+rep+nName.value;";
str += "fileObj.Copy(newF);";
str += "success=1;";
str += "}";
str += "}";
str += "if(success==1){";
str += "nName.style.display='none';";
str += "nName.value='';";
str += "alert('File "+fInp+" successfully copied to "+newF+".')}";
str += "}";
str += "onerror=function(errnum){";
str += "msg='Error:';";
str += "if(!fso.FileExists(fInp)){";
str += "msg='Error concerning the file "+fInp+":';";
str += "}";
str += "alert(msg+errnum);";
str += "return true;";
str += "}";
str += "</script></head>";
str += "<body bgcolor='#cae1ff' text='#3d59ab'>";
str += "<center><h1>Welcome, "+id.toUpperCase()+"!</h1></center>";
// Select upload destination
str += "<center><u><h2>Select upload destination...</h2></u></center>";
str += "<center><h4><table border='2' cellpadding='1' cell spacing='1' width='65%'><tr><td>"+
"<center><form name='project'><u><h3>Select the type of project:</h3></u><select name='projectlist'><option value='#'>"+
"Select an option below...</option><optgroup label='CRT projects'>"+ // Select project
"<option value='S:/QE/Common/QEWeb/Project/CRT_Project/A02'>A02</option>"+
"<option value='S:/QE/Common/QEWeb/Project/CRT_Project/EMG2K3'>EMG2K3</option>"+
"<option value='S:/QE/Common/QEWeb/Project/CRT_Project/EMG2K4'>EMG2K4</option>"+
"<option value='S:/QE/Common/QEWeb/Project/CRT_Project/EMG_PP2'>EMG PP2</option>"+
"<option value='S:/QE/Common/QEWeb/Project/CRT_Project/Esplanade_Salsa'>Esplanade Salsa</option>"+
"<option value='S:/QE/Common/QEWeb/Project/CRT_Project/ITV'>ITV</option>"+
"<option value='S:/QE/Common/QEWeb/Project/CRT_Project/L04'>L04</option>"+
"<option value='S:/QE/Common/QEWeb/Project/CRT_Project/L05ATSC'>L05ATSC</option>"+
"<option value='S:/QE/Common/QEWeb/Project/CRT_Project/L05HD'>L05HD</option>"+
"<option value='S:/QE/Common/QEWeb/Project/CRT_Project/PTV'>PTV</option></optgroup>"+
"<optgroup label='LCD projects'></optgroup><select></form></center>"+
"<td><center><form name='report'><u><h3>Select the type of report or certificate:</h3></u><select name='reportlist'>"+
"<option value='#'>Select an option below...</option>"+ // Select report
"<option value='/CB-Cert_CB-Report'>CB certificate & report</option>"+
"<option value='/EMC_FCC_Report'>EMC_FCC report </option>"+
"<option value='/DFU'>DFU</option>"+
"<option value='/Country_Certificate'>Country certificates</option>"+
"<option value='/Circuit_Diagram'>Circuit diagrams</option></select></form></center>"+
"</td></tr></table></h4></center>";
// Searching
str += "<input type=button onclick='window.location=document.project.projectlist.options"+
"[document.project.projectlist.selectedIndex].value+document.report.reportlist.options"+
"[document.report.reportlist.selectedIndex].value' value='Searching...'>";
// // Select the upload file
str += "<center><u><h2>Select the file for uploading...</h2></u>";
str += "<form name='upload' id='upload' action='#' method='post' enctype='multipart/form-data'>"+
"<input type='file' id='upfile' name='upfile' size='40'><br>";
str += "<input type='text' size='20' name='newName' style='display:none'><br>";
str += "<input type='button' value='Copy file to network drive' "+
"onclick='javacript:copyF(document.project.projectlist.options[document.project.projectlist.selectedIndex].value,"+
"document.report.reportlist.options[document.report.reportlist.selectedIndex].value,"+
"document.upload.upfile.value,document.upload.newName)'></form></center></body></html>";
writeln(str);
}
[i]Originally posted by Huang Youyi [/i]Thanks!!
[B]Relax and enjoy the coming weekend![/B][/QUOTE]
0.1.9 — BETA 5.18