locked
AsyncFileUpload Saving FileName RRS feed

  • Question

  • User-191253613 posted

    Hi all,

    I have a AsyncFileUpload control where I have everything working perfectly. However, I am saving the filename to an xml file as an image list to display in a GridView.

    The only problem I am having is that no matter what I do the xml element being created is duplicated. Below is my code behind for AsyncFileUpload with the XmlDocument statement. Anyone have any idea how I can stop this from happening?

    I know it has something to do with being in the AsyncFileUpload code because the xml works fine when tested elsewhere.

    Thanks!!

    Here is what happens with the xml doc. Notice I get 2 elements created...

    <?xml version="1.0" encoding="utf-8"?>
    <ImageLinks>
         <ImageLink>~/Images/Uploads/ATT-Ad-1B.PNG</ImageLink>
         <ImageLink>~/Images/Uploads/ATT-Ad-1B.PNG</ImageLink> 
    </ImageLinks>

    here is my code behind in c#...

    protected void Page_Load(object sender, EventArgs e)
    {
         AsyncFileUpload1.UploadedComplete += new EventHandler<AsyncFileUploadEventArgs>(AsyncFileUpload1_UploadedComplete);
         AsyncFileUpload1.UploadedFileError += new EventHandler<AsyncFileUploadEventArgs>(AsyncFileUpload1_UploadedFileError);
    }

    void AsyncFileUpload1_UploadedComplete(object sender, AsyncFileUploadEventArgs e)
    {
         ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "size", "top.$get(\"" + lblUploadStatus.ClientID + "\").innerHTML = '~/Images/Uploads/" +              Path.GetFileName(e.FileName) + "';", true);

         XmlDocument doc = new XmlDocument();
         doc.Load(Server.MapPath("~/xml/ImageLinksList.xml"));

         XmlElement imagelinkElem = doc.CreateElement("ImageLink");
         imagelinkElem.InnerText = "~/Images/Uploads/" + Path.GetFileName(e.FileName);
         doc.DocumentElement.PrependChild(imagelinkElem);

         doc.Save(Server.MapPath("~/xml/ImageLinksList.xml"));

         string savePath = MapPath("~/Images/Uploads/" + Path.GetFileName(e.FileName));
         AsyncFileUpload1.SaveAs(savePath);
    }

    void AsyncFileUpload1_UploadedFileError(object sender, AsyncFileUploadEventArgs e)
    {
         ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "error", "top.$get(\"" + lblUploadStatus.ClientID + "\").innerHTML = '<img src='../Images/design/status-red-check-      unavailable.jpg' style='vertical-align:middle' /> Error: " + e.StatusMessage + "';", true);
    }

    Sunday, March 9, 2014 4:50 PM

Answers

  • User-760709272 posted

    Put a breakpoint on the event to see if it is firing twice rather than writing two nodes.  Also your code won't work if people upload files at the same time.  Also, for this code

    innerHTML = '~/Images/Uploads

    "~" only works for server-side controls, if you look at the source of your page you'll see it has literally written out the "~/". 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, March 9, 2014 5:58 PM

All replies

  • User-760709272 posted

    Put a breakpoint on the event to see if it is firing twice rather than writing two nodes.  Also your code won't work if people upload files at the same time.  Also, for this code

    innerHTML = '~/Images/Uploads

    "~" only works for server-side controls, if you look at the source of your page you'll see it has literally written out the "~/". 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, March 9, 2014 5:58 PM
  • User-191253613 posted

    AidyF,

    Thanks! I was so consumed by the problem I was having, I didn't even notice I had included the '~/'. As far as multiple uploads at one time, that's not a problem. I am developing a business app where the administrator will be the only one to upload files. It's not like the users will be uploading.

    It was firing twice, but I couldn't figure out why. I corrected the problem by using the AJAXFileUpload control, rather than the AsyncFileUpload control. Everything is working perfect now.

    Thanks again for you help! I am amazed at how quickly you guys respond!

    Russ

    Sunday, March 9, 2014 7:35 PM