Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

AJAX Upload

Posted on 2013-11-24
9
635 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

0
Comment
Question by:RecipeDan
  • 5
  • 4
9 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 39675083
That sounds like e.State is not Success, but there is not an exception.  Can you check the value of e.State?
0
 
LVL 1

Author Comment

by:RecipeDan
ID: 39675258
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

0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 39675280
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?
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 1

Author Comment

by:RecipeDan
ID: 39676481
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

0
 
LVL 1

Author Comment

by:RecipeDan
ID: 39676496
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
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 39676613
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...
0
 
LVL 1

Author Comment

by:RecipeDan
ID: 39679060
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.
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 500 total points
ID: 39679153
Well, let's review the AjaxFileUpload.UploadedComplete event:

AjaxFileUpload Demonstration
http://www.asp.net/ajaxLibrary/AjaxControlToolkitSampleSite/AjaxFileUpload/AjaxFileUpload.aspx

UploadedComplete - Raised on the server when a file is uploaded successfully. In this event an instance of AjaxFileUploadEventArgs is passed in the argument that contains file name, size and content type.

This is a server side event, so you should be able to interact with the form.  I am not disputing the claim, since I just don't use the AJAX Control Toolkit (I work with ASP.NET MVC web sites).   I am not the best expert on this type of problem.  I am just saying that it doesn't make any sense.

You might want to try the OnClientUploadComplete event, as shown here:

Uploading file asynchronously using Ajax AsyncFileUpload control
http://www.mallstuffs.com/blogs/BlogDetails.aspx?BlogId=231&BlogType=Technical&Topic=Uploading%20file%20asynchronously%20using%20Ajax%20AsyncFileUpload%20control

        <ajaxToolkit:AsyncFileUpload ID="AsyncFileUpload1" runat="server" CompleteBackColor="yellow" UploadingBackColor="Green" BackColor="AliceBlue"
OnClientUploadError="uploadError" OnClientUploadStarted="StartUpload" OnClientUploadComplete="UploadComplete" UploaderStyle="Modern" OnUploadedComplete="ProcessUpload" ThrobberID="spanUploading"  />


        function UploadComplete(sender, args)  {
            var filename = args.get_fileName(); 
            var contentType = args.get_contentType(); 
            var text = "Size of " + filename + " is " + args.get_length() + " bytes"; 
            if (contentType.length > 0)  {
                text += " and content type is '" + contentType + "'."; 
            } 
            document.getElementById('lblStatus').innerText = text; 
        } 

Open in new window

0
 
LVL 1

Author Comment

by:RecipeDan
ID: 39735790
Sorry for not getting back to you sooner. I tried your suggestion and it does work. Thank you for your assistance.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question