<Html>
<head>
</head>
<body>
<form enctype= "multipart/form-data " runat= "server ">
<p>
Select File1: <br />
<input id= "File1 " type= "file " runat= "Server " />
<br />
Select File2: <br />
<input id= "File2 " type= "file " runat= "Server " />
<br />
Select File3: <br />
<input id= "File3 " type= "file " runat= "Server " />
<br />
Select File4: <br />
<input id= "File4 " type= "file " runat= "Server " />
</p>
<p>
<input id= "Submit1 " type= "submit " value= "Upload Files "
runat= "Server " onserverclick= "SubmitButton_Click " />
<br />
</p>
<span id= "Span1 " runat= "Server "> </span>
</form>
</body>
</Html>
**/**///// <summary>
/// WebClIEnt上傳文件至服務器
/// </summary>
/// <param name= "fileNamePath "> 文件名,全路徑格式 </param>
/// <param name= "uriString "> 服務器文件夾路徑 </param>
private void UpLoadFile(string fileNamePath,string uriString)
...{
string fileName = fileNamePath.Substring(fileNamePath.LastIndexOf( "\ ") + 1);
NewFileName = DateTime.Now.ToString( "yyMMddhhmmss ") + DateTime.Now.Millisecond.ToString() + fileNamePath.Substring(fileNamePath.LastIndexOf( ". "));
string fileNameExt = fileName.Substring(fileName.LastIndexOf( ". ") + 1);
if(uriString.EndsWith( "/ ") == false) uriString = uriString + "/ ";
uriString = uriString + NewFileName;
/**//**//**//// 創建WebClIEnt實例
WebClient myWebClient = new WebClIEnt();
myWebClIEnt.Credentials = CredentialCache.DefaultCredentials;
// 要上傳的文件
FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
//FileStream fs = OpenFile();
BinaryReader r = new BinaryReader(fs);
try
...{
//使用UploadFile方法可以用下面的格式
//myWebClIEnt.UploadFile(uriString, "PUT ",fileNamePath);
byte[] postArray = r.ReadBytes((int)fs.Length);
Stream postStream = myWebClIEnt.OpenWrite(uriString, "PUT ");
if(postStream.CanWrite)
...{
postStream.Write(postArray,0,postArray.Length);
}
else
...{
AppHelper.MessageService.ShowError( "文件目前不可寫! ");
}
postStream.Close();
}
catch
...{
AppHelper.MessageService.ShowError( "文件上傳失敗,請稍候重試~ ");
}
}
循環調用UpLoadFile方法就可以了
for (int 1=0;i < this.ListBox1.Items.Count;i++)
...{
UpLoadFile(this.ListBox1.Items(i).Text,url);
}
<script language= "Javascript ">
function add()
...{
var myFiles=document.getElementById( "myFiles ");
var filediv=document.createElement( "div ");
filediv.innerHtml= " <input type= "file " /> ";
myFiles.appendChild(filediv);
var thisFile=filediv.childNodes[0];
thisFile.click();
if(checkRepeat(thisFile.value))
...{
myFiles.removeChild(filediv);
return;
}
var mySelect=document.getElementById( "mySelect ");
var option=new Option(thisFile.value,thisFile.value);
mySelect.options.add(option);
mySelect.options.selectedIndex=mySelect.options.length-1;
mySelect.size++;
}
function remove()
...{
var mySelect=document.getElementById( "mySelect ");
var selectedIndex=mySelect.selectedIndex;
if(selectedIndex==-1)
...{
alert( "請選擇要移除的文件! ");
return;
}
mySelect.options.remove(selectedIndex);
if(selectedIndex==0)
mySelect.options.selectedIndex=0;
else
mySelect.options.selectedIndex=selectedIndex-1;
mySelect.size--;
var myFiles=document.getElementById( "myFiles ");
myFiles.removeChild(myFiles.childNodes[selectedIndex]);
}
function checkRepeat(filepath)
...{
var myOptions=document.getElementById( "mySelect ").options;
for(i=0;i <myOptions.length;i++)
...{
if(myOptions[i].value==filepath)
...{
alert( "該文件已存在列表當中,請不要重復選擇! ");
return true;
}
}
return false;
}
</script>