Search code examples
asp.netsessionajaxcontroltoolkitviewstateasyncfileupload

Ajax Control Toolkit AsyncFileUploader Control and viewstate/session Issue


in my project i need to upload files so i decided to use the uploader provided by asp.net ajax controls AsyncFileUPloader control. there are four blocks. every block contains two such uploaders so i decided to utilize the power of asp.net web user controls. i wrapped the required form fields in my user control called DesignUploader.ascx now i have to put the four instances of this control on my aspx page please refer the snap below

enter image description here

my problem starts here i have to insert the fileurl to the database and each of the block generates unique id and id value changes after uploading the file to the server. i noticed that viewstate does not work for me in case of asyncfileuploader it clears the viewstate because it does the secret postback to the server behind the scenes. now only option left for me is to use session but when user uploads the files in two blocks one after another then filepath from second/third consecutive blocks overwrite my session. i don't know how many blocks user can use to upload the designs he may use 1 only or he may use all four.

There would be a final submit button in the bottom of the page on click of which i have to insert data to database.

so when i tried to save the data to database the session stores the value of the recently uploaded file path for all the records my problem lies here

i don't know if i was able to describe my problem in correct manner or not please excuse me if it is not clear and post comment if required.

Note: I can not change the UI because client insists for this only :(

any quick work around would be appreciated much

Thanks Devjosh


Solution

  • I believe you saving file path to session in a wrong way and it's impossible to recognize where is an error without code. All the way, in my opinion better don't persist file path in session but use client side for that purpose instead. You can add two hidden fields to DesignUploader.ascx control and set their values in UploadedComplete event handler.

    public partial class DesignUploader : System.Web.UI.UserControl
    {
        private static readonly string AppDataPath = HttpContext.Current.Server.MapPath("~/App_Data/");
    
        public string FirstFilePath
        {
            get
            {
                return Server.UrlDecode( FirstFilePathHiddenField.Value);
            }
        }
    
        public string SecondFilePath
        {
            get
            {
                return Server.UrlDecode(SecondFilePathHiddenField.Value);
            }
        }
    
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
    
            FirstFileUpload.UploadedComplete += FirstFileUpload_UploadedComplete;
            SecondileUpload.UploadedComplete += SecondileUpload_UploadedComplete;
        }
    
        void FirstFileUpload_UploadedComplete(object sender, AjaxControlToolkit.AsyncFileUploadEventArgs e)
        {
            var fullPath = Path.Combine(AppDataPath, Path.GetFileName(e.FileName));
            FirstFileUpload.SaveAs(fullPath);
            SaveFilePathToHiddenField(FirstFilePathHiddenField.ClientID, fullPath);
        }
    
        void SecondileUpload_UploadedComplete(object sender, AjaxControlToolkit.AsyncFileUploadEventArgs e)
        {
            var fullPath = Path.Combine(AppDataPath, Path.GetFileName(e.FileName));
            SecondileUpload.SaveAs(fullPath);
            SaveFilePathToHiddenField(SecondFilePathHiddenField.ClientID, fullPath);
        }
    
        private void SaveFilePathToHiddenField(string fieldId, string pathValue)
        {
            var script = string.Format("top.$get('{0}').value = '{1}';", fieldId, Server.UrlEncode(pathValue));
            ScriptManager.RegisterStartupScript(this, this.GetType(), "setPath", script, true);
        }
    }