Solved

AJAX Upload

Posted on 2013-11-24
9
621 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

920 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now