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 ExtensionsModulesModulesHttpFileCollection not getting reset to zero.HttpFileCollection not getting reset to zero.
Previous
 
Next
New Post
4/19/2011 8:48 AM
 
To all:

I am using the code below to design a "File Uploader" for my users.  All this works great.  The user starts with one file upload box and can add up to six more if desired.  The files get a GUID identifier appended to their names during upload.  After the upload, the File Upload Box count gets set back to one.

The problem is that after the files are uploaded and the File Upload Box is reset back to one, and is blank, if the user hits page refresh or reload, the same group of files that he just uploaded gets uploaded again.  Why?

How do I ensure the HttpFileCollection gets reset back to zero after the upload is complete?

Thank you.

Here's the code:   First the HTML then the code behind:

<div>
    <div>
    
    </div><br />
    <hr />
    <br />
    <table class="tableStyle" cellpadding="15" >
        <tr>
            <td>
                <div style="border-style: inset; border-width: 4px; width: 100%; height: 100%;" >
                    <br />
                    <asp:Label ID="lblFileUpload" runat="server" Text="Add File(s) To Be Uploaded: (MAX 6)" Font-Bold="true"></asp:Label><br />
                    <br />
                    <br />
                    <p id="upload-area" >
                        <input id="FileField" type="File" runat="server" size="45" />
                    </p>
                    <input id="ButtonAdd" type="button" value="Add File" onclick="addFileUploadBox()" class="buttonStyle" /><br />
                    <br />
                    <p align="right">
                        <asp:Button ID="btnUpload" runat="server" Text="Upload Now" OnClick="btnUpload_Click" CssClass="buttonStyle" />
                    </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;


//                            Limit user to 6 upload boxes maximum.
                            if (!addFileUploadBox.lastAssignedId || addFileUploadBox.lastAssignedId < 105)
                            {
                                var newLine = document.createElement("br");
                                uploadArea.appendChild(newLine);


                                var newUploadBox = document.createElement("input");
                                newUploadBox.type = "file";
                                newUploadBox.size = "45";
                                if (!addFileUploadBox.lastAssignedId)
                                    addFileUploadBox.lastAssignedId = 100;


                                newUploadBox.setAttribute("id", "FileField" + addFileUploadBox.lastAssignedId);
                                newUploadBox.setAttribute("name", "FileField" + addFileUploadBox.lastAssignedId);
                                uploadArea.appendChild(newUploadBox);
                                addFileUploadBox.lastAssignedId++;
                            }
                            else {
                                return false;
                            }


                        }
       
                    </script>
                </div>
</div>
</div>

Code Behind:

protected void btnUpload_Click(object sender, EventArgs e)
        {


            HttpFileCollection uploads = HttpContext.Current.Request.Files;
            for (int i = 0; i < uploads.Count; i++)
            {
                HttpPostedFile upload = uploads[i];


                if (upload.ContentLength == 0)
                    continue;

                string c = Guid.NewGuid().ToString() + Path.GetFileName(upload.FileName);
                try
                {
                    upload.SaveAs(Server.MapPath("~/" + "UserUploads/" + c));
                    Span1.InnerHtml = "Upload(s) successful.";
                }
                catch(Exception ex)
                {
                    Span1.InnerHtml = "Upload(s) FAILED. ERROR: " + ex.Message.ToString();                
                }

            }
 
New Post
4/20/2011 7:40 AM
 

WHAT am I missing?

I have now read numerous articles and tried many sample code sets, and I still have not found anyone that addresses the issue I have here.

The multiple file uploading works perfectly. But no one addresses the issue of a page reload / refresh that causes the last group of files that was uploaded to be uploaded again.

HOW DO I AVOID THIS?
 
New Post
4/20/2011 9:55 AM
 
Preventing duplicate form submission on refresh or when the user clicks the back button and manually submits the form a second time is one of the more difficult ASP.Net issues to solve. That is especially true in your case since a refresh (which pulls the page from the browser cache) will start the upload of the files all over again - before any code you might add to your server-side btnUpload click handler has a chance to stop it.

Probably the easiest solution (though far from fool proof as it does not prevent the user from hitting the back button then re-submitting) would be to do a Response.Redirect back to either the same page or to a new page ("Thank you for uploading, etc. . . . ") at the end of your btnUpload click handler. Here's the one to the same page:

Response.Redirect (Request.Url.ToString(), False)

Note that doing the above will retain any query string parameters but cause loss of any ViewState.


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/20/2011 4:32 PM
 

Bill,

Once again, thank you. That is going to work quite nicely in my Module. I'm going to direct the user to nice "File Upload Completed Successfully / Failed" page.

Glad people like you are out there.


Thank you.
 
Previous
 
Next
HomeHomeDevelopment and...Development and...Building ExtensionsBuilding ExtensionsModulesModulesHttpFileCollection not getting reset to zero.HttpFileCollection not getting reset to zero.


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