maverick786us
August 29th, 2007, 02:45 AM
I've made a web user control to upload multiple files onto the server. The control is designed in such a way that the user can add as many file uploaders as he can he by clicking on add button.
The controls get added at the runtime using JAVASCRIPT
<script language="javascript" type="text/javascript">
function Button_onclick() {
var filebutton = '<br> <input name="File" type="file" lang="en-us" style="width: 248px"/>';
document.getElementById('fileUploader').insertAdjacentHTML("beforeEnd", filebutton);
}
</script>
This is the control which I am adding on the button click
<p id="fileUploader" ><input type="file" lang="en-us" name="File" style="width: 248px"/></p></td>
.........................................................................................................
.........................................................................................................
<input id="btn_AddMore" type="button" value="Add More" onclick="return Button_onclick();"/>
But when i click on upload it does'nt upload a single file. I am using a loop that will use HttpCollectionList object to count the number of controls and upload accordingly.
protected void UploadFiles()
{
/// Obtain the uploaded files list
HttpFileCollection fileList = HttpContext.Current.Request.Files;
Response.Write(fileList);
/// Define the new message to be displayed
StringBuilder uploadMsg = new StringBuilder("Upload files as follows: " + "<br>");
/// Upload Each file inthe file list
for (int i = 0; i < fileList.Count; i++)
{
/// Obtain the current uploaded file
HttpPostedFile hPostedFile = fileList[i];
string szFileName = Path.GetFileName(hPostedFile.FileName);
if (!string.IsNullOrEmpty(szFileName))
{
///upload the file
hPostedFile.SaveAs(MapPath("./HardFiles/UploadedFiles"));
///add the file to the database
uploadMsg.Append("File name:" + szFileName + "<br>");
}
}
}
Everytime the value of fileList.Count is 0 no matter how many controls I add. Can someone help me with this??
Thanks in Advance
The controls get added at the runtime using JAVASCRIPT
<script language="javascript" type="text/javascript">
function Button_onclick() {
var filebutton = '<br> <input name="File" type="file" lang="en-us" style="width: 248px"/>';
document.getElementById('fileUploader').insertAdjacentHTML("beforeEnd", filebutton);
}
</script>
This is the control which I am adding on the button click
<p id="fileUploader" ><input type="file" lang="en-us" name="File" style="width: 248px"/></p></td>
.........................................................................................................
.........................................................................................................
<input id="btn_AddMore" type="button" value="Add More" onclick="return Button_onclick();"/>
But when i click on upload it does'nt upload a single file. I am using a loop that will use HttpCollectionList object to count the number of controls and upload accordingly.
protected void UploadFiles()
{
/// Obtain the uploaded files list
HttpFileCollection fileList = HttpContext.Current.Request.Files;
Response.Write(fileList);
/// Define the new message to be displayed
StringBuilder uploadMsg = new StringBuilder("Upload files as follows: " + "<br>");
/// Upload Each file inthe file list
for (int i = 0; i < fileList.Count; i++)
{
/// Obtain the current uploaded file
HttpPostedFile hPostedFile = fileList[i];
string szFileName = Path.GetFileName(hPostedFile.FileName);
if (!string.IsNullOrEmpty(szFileName))
{
///upload the file
hPostedFile.SaveAs(MapPath("./HardFiles/UploadedFiles"));
///add the file to the database
uploadMsg.Append("File name:" + szFileName + "<br>");
}
}
}
Everytime the value of fileList.Count is 0 no matter how many controls I add. Can someone help me with this??
Thanks in Advance