We help IT Professionals succeed at work.

AJAX Upload

RecipeDan
RecipeDan asked
on
797 Views
Last Modified: 2013-12-23
Hello:

I am using .Net 4.5. When I use the AJAX FileUpload,the files upload to the directory ok but the Label1.Text and Label2.Text do not fire. I do not get an error message even though I use try catch.

    <form id="form1" runat="server">
        <asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server" />
    <div>
        <asp:AjaxFileUpload ID="AjaxFileUpload1" AllowedFileTypes="mp4" OnUploadComplete="AjaxFileUpload1_UploadComplete" runat="server" />
    </div>
        <br />
        <br />
        <asp:Label ID="Label1" runat="server" />
        <br />
        <br />
        <asp:Label ID="Label2" runat="server" />
    </form>

Open in new window

        protected void AjaxFileUpload1_UploadComplete(object sender, AjaxControlToolkit.AjaxFileUploadEventArgs e)
        {
            try
            {
                AjaxFileUpload1.SaveAs(MapPath("~/App_Data/" + e.FileName));
                if (e.State == AjaxControlToolkit.AjaxFileUploadState.Success)
                {
                    Label1.Text = e.FileName;
                    Label2.Text = e.FileSize.ToString();
                }
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message.ToString());
            }
        }

Open in new window

Comment
Watch Question

CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2008

Commented:
That sounds like e.State is not Success, but there is not an exception.  Can you check the value of e.State?

Author

Commented:
Yes e.State is not a success so when I took it out, it give me the same result the files were uploaded but nothing happened with Label1 and Label2. I even tried something like this Label1.Text = "Successful Upload!"; thinking it maybe the e.FileName. Still no result or error.

            try
            {
                AjaxFileUpload1.SaveAs(MapPath("~/App_Data/" + e.FileName));
                    Label1.Text = e.FileName;
                    Label2.Text = e.FileSize.ToString();
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message.ToString());
            }

Open in new window

CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2008

Commented:
What happens if you are in debug mode, and you comment out the try/catch block?  If you put a breakpoint on the SaveAs line, does it step passed that line to the Label1.Text line?

Author

Commented:
I deleted the try catch and commented out the upload. The files do not upload to the folder but the labels do not display either.

I added a button just to see if where was a problem with the labels and when I click on the button the labels show there respective texts.

    <form id="form1" runat="server">
        <asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server" />
    <div>
        <asp:AjaxFileUpload ID="AjaxFileUpload1" AllowedFileTypes="mp4" OnUploadComplete="AjaxFileUpload1_UploadComplete" runat="server" />
    </div>
        <br />
        <br />
        <asp:Button ID="ButtonTest" Text="Test" runat="server" OnClick="ButtonTest_Click" />
        <br />
        <br />'
        <asp:Label ID="Label1" runat="server" />
        <br />
        <br />
        <asp:Label ID="Label2" runat="server" />
    </form>

Open in new window

    public partial class Test : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        protected void AjaxFileUpload1_UploadComplete(object sender, AjaxControlToolkit.AjaxFileUploadEventArgs e)
        {
                    //AjaxFileUpload1.SaveAs(MapPath("~/App_Data/" + e.FileName));
                    Label1.Text = e.FileName;
                    Label2.Text = e.FileSize.ToString();
        }

        protected void ButtonTest_Click(object sender, EventArgs e)
        {
            Label1.Text = "Label 1 Works!";
            Label2.Text = "Label 2 Works!";
        }
    }

Open in new window

Author

Commented:
I do not think its possible...read the comments in this article

http://stephenwalther.com/archive/2013/04/30/april-2013-release-of-the-ajax-control-toolkit
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2008

Commented:
Is that the version that you are using now?  Maybe it is because I am a little tired, but I missed something important that you saw...

Author

Commented:
Basically from this comment...


Michael Teper       

 May 1, 2013 at 8:09 pm      

Stephen, looking at the upload control, it seems it can only be used for uploading the file, and cannot be used in combination with a form (i.e. set a few controls, select file(s) for upload, submit). Is this true? If so, that’s a very limiting design choice and makes the control far less useful that I was hoping.

-Michael

      Stephen.Walther       

 May 1, 2013 at 9:21 pm      

@Michael – yes, the AjaxFileUpload is async. It would be hard (impossible?) to display progress if we did not make it async. Also, we could not break the file into multiple chunks.

      Michael Teper       

 May 2, 2013 at 3:46 pm      

True, but as we discussed on Twitter, you can keep it async and still do a post at the end of upload.
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2008
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
Sorry for not getting back to you sooner. I tried your suggestion and it does work. Thank you for your assistance.

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.