?
Solved

AJAX Upload

Posted on 2013-11-24
9
Medium Priority
?
674 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
[X]
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
  • 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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 2000 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

801 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