Products

Solutions

Resources

Partners

Community

Blog

About

QA

Ideas Test

New Community Website

Ordinarily, you'd be at the right spot, but we've recently launched a brand new community website... For the community, by the community.

Yay... Take Me to the Community!

Welcome to the DNN Community Forums, your preferred source of online community support for all things related to DNN.
In order to participate you must be a registered DNNizen

HomeHomeDevelopment and...Development and...Building ExtensionsBuilding ExtensionsModulesModulesHow to access HttpContext in my module development.How to access HttpContext in my module development.
Previous
 
Next
New Post
4/13/2011 9:42 PM
 
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> 

 
New Post
4/14/2011 1:57 AM
 
HI

You can also upload files into your server folder using dnn:urlcontrol.

Thanks
Sibabrata
Mindfire Solutions
 
New Post
4/14/2011 8:44 PM
 
Can you tell me why the System.Web.HttpContext.Current.Request.Files returns 0 files when I add 3 input file boxes and click upload.  This code works in a non DNN sample site, it just doesn't work when I try to do it as a module in DNN.

Why can't I access the HttpContext.Current.Request object?  Do I have to reference some DotNetNuke entity or something?

Please HELP.

 
New Post
4/14/2011 9:32 PM
 
You don't happen to have the btnUpload button control within an AJAX UpdatePanel or have enabled "Supports Partial Rendering" in the module control's registration in Host-->Extensions do you?

Bill, WESNet Designs
Team Lead - DotNetNuke Gallery Module Project (Not Actively Being Developed)
Extensions Forge Projects . . .
Current: UserExport, ContentDeJour, ePrayer, DNN NewsTicker, By Invitation
Coming Soon: FRBO-For Rent By Owner
 
New Post
4/14/2011 9:57 PM
 
Bill,

Thank you , Thank YOU.   The problem was that I had "Supports Partial Page Updates" checked for the module.  That was left over from when I was trying something different with asp components and the Update Panel.  

Thank you so much.  I was beginning to think that developing DNN Mods in Visual Web Dev Express was going to be way difficult and not worth the effort.

I had no idea how to trace that one out since I was getting ZERO files during my debugging sessions.

Thanks again.

Elgin
 
Previous
 
Next
HomeHomeDevelopment and...Development and...Building ExtensionsBuilding ExtensionsModulesModulesHow to access HttpContext in my module development.How to access HttpContext in my module development.


These Forums are dedicated to discussion of DNN Platform and Evoq Solutions.

For the benefit of the community and to protect the integrity of the ecosystem, please observe the following posting guidelines:

  1. No Advertising. This includes promotion of commercial and non-commercial products or services which are not directly related to DNN.
  2. No vendor trolling / poaching. If someone posts about a vendor issue, allow the vendor or other customers to respond. Any post that looks like trolling / poaching will be removed.
  3. Discussion or promotion of DNN Platform product releases under a different brand name are strictly prohibited.
  4. No Flaming or Trolling.
  5. No Profanity, Racism, or Prejudice.
  6. Site Moderators have the final word on approving / removing a thread or post or comment.
  7. English language posting only, please.
What is Liquid Content?
Find Out
What is Liquid Content?
Find Out
What is Liquid Content?
Find Out