I am trying to develop a module that will allow a user to upload files to a folder on my server. I am developing in Visual Web Dev 2010 Epxress, .Net 3.5 Framework. I used the module project template developed by Chris Hammond to start this project. For my file upload box(es) in the View.ascx page I am following an example put out by Joe Stagner of Microsoft that demonstrates how to use basic HTML input elements of type "file" and a small bit of javascript to allow the user to dynamically add additional file upload boxes before submitting the upload(s). I know there are the ASP FileUpload and AJAX AsyncFileUpload components, but I trying to LEARN this simple way first.
The problem I'm having is with the code behind for the click event of the Upload button. Following Joe's example, I am using an HttpFileCollection type variable "uploads" to iterate through the file upload boxes to get the filenames to upload. The problem is that I cannot get System.Web.HttpContext.Current.Request.Files to properly reflect the files added for uploading whenever I am trying to use the code shown below in my DNN Module Development. However, all of the code below works fine in a basic Website project created in Web Dev 2010 Express.
How do I access the HttpContext.Current.Request.Files in DNN module development?? Here's the code, first the code behind, then the HTML and Java
Thank you
protected void btnUpload_Click(object sender, EventArgs e)
{
HttpFileCollection uploads = System.Web.HttpContext.Current.Request.Files;
for (int i = 0; i < (uploads.Count); i++)
{
if (uploads[i].ContentLength > 0)
{
String c = System.IO.Path.GetFileName(uploads[i].FileName);
try
{
FileField.PostedFile.SaveAs(Server.MapPath("~/" + "UserUploads/") + c);
Span1.InnerHtml = "File(s) Uploaded Successfully.";
}
catch (Exception exc)
{
Span1.InnerHtml = "ERROR: " + exc.Message.ToString();
}
}
}
}
HTML and JAVASCRIPT
<div style="text-align: center;">
<div style="background-color:Teal; width:550px;">
<br />
<asp:Label ID="lblFileUpload" runat="server" Text="Add Files To Be Uploaded:" Font-Bold="true" ></asp:Label><br />
<br /><br />
<p id="upload-area">
<input id="FileField" type="File" runat="server" size="60" />
</p>
<input id="ButtonAdd" type="button" value="Add File" onclick="addFileUploadBox()" />
<p>
<asp:Button ID="btnUpload" runat="server" Text= "Upload Now" OnClick="btnUpload_Click" />
</p>
<span id="Span1" style="color:Red" runat="server"></span><br /><br />
<script type="text/javascript">
function addFileUploadBox() {
if (!document.getElementById || !document.createElement)
return false;
var uploadArea = document.getElementById("upload-area");
if (!uploadArea)
return;
var newLine = document.createElement("br");
uploadArea.appendChild(newLine);
var newUploadBox = document.createElement("input");
newUploadBox.type = "file";
newUploadBox.size = "60";
if (!addFileUploadBox.lastAssignedId)
addFileUploadBox.lastAssignedId = 100;
newUploadBox.setAttribute("id", "FileField" + addFileUploadBox.lastAssignedId);
newUploadBox.setAttribute("name", "FileField" + addFileUploadBox.lastAssignedId);
uploadArea.appendChild(newUploadBox);
addFileUploadBox.lastAssignedId++;
}
</script>
</div>
<br /><br />
</div>