Solved

AJAX Upload

Posted on 2013-11-24
9
611 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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

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 …
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

746 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

12 Experts available now in Live!

Get 1:1 Help Now