This is a migrated thread and some comments may be shown as answers.

FileUploaded event not firing with custom upload handler

1 Answer 157 Views
Upload
This is a migrated thread and some comments may be shown as answers.
Nisi
Top achievements
Rank 1
Nisi asked on 25 Sep 2009, 08:16 PM
Hi,

I have a custom upload handler which stores uploaded files to a database. Almost all the code works, the file ends up in the db and the file chunking is handled correctly. The problem I'm running into is after the file is done uploading on the client side event 'RadUpload_FileUploaded'  is never hit even though the the file progress reaches 100%, it just hangs. The file is there in its entirety but the control doesn't return to its 'file picking' state

Upload.xaml
<telerik:RadUpload Margin="43,28,155,0" 
                            Filter="All Files(*.*)|*.*" 
                            FilterIndex="0" IsAutomaticUpload="false" OverwriteExistingFiles="True" BufferSize="100000" 
                            UploadServiceUrl="~/Upload.ashx" 
                            TargetFolder="Temp" FileCountExceeded="RadUpload_FileCountExceeded" 
                            FilesSelected="RadUpload_FilesSelected" FileTooLarge="RadUpload_FileTooLarge" 
                            FileUploaded="RadUpload_FileUploaded" FileUploadFailed="RadUpload_FileUploadFailed" 
                            FileUploadStarting="RadUpload_FileUploadStarting" 
                            ProgressChanged="RadUpload_ProgressChanged" 
                            UploadCanceled="RadUpload_UploadCanceled" UploadFinished="RadUpload_UploadFinished" 
                            UploadPaused="RadUpload_UploadPaused" UploadResumed="RadUpload_UploadResumed" 
                            UploadSizeExceeded="RadUpload_UploadSizeExceeded" 
                            UploadStarted="RadUpload_UploadStarted" VerticalAlignment="Top" Height="165" d:LayoutOverrides="VerticalAlignment" /> 
                     

upload.xaml.cs
public partial class MainPage : UserControl 
    { 
        public MainPage() 
        { 
            // Required to initialize variables 
            InitializeComponent(); 
        } 
         
            private void RadUpload_FileCountExceeded(object sender, FileEventArgs e) 
        { 
            Status_EventEnter("FileCountExceeded"); 
            Status_Write("FileName" + e.SelectedFile.Name); 
            Status_EventEnd("FileCountExceeded"); 
        } 
 
        private void RadUpload_FilesSelected(object sender, FilesSelectedEventArgs e) 
        { 
            Status_EventEnter("FilesSelected"); 
            Status_Write("FileCount: " + e.SelectedFiles.Count); 
            Status_Write("FirstFile Name: " + e.SelectedFiles[0].Name); 
            Status_EventEnd("FilesSelected"); 
        } 
 
        private void RadUpload_FileTooLarge(object sender, FileEventArgs e) 
        { 
            Status_EventEnter("FileTooLarge"); 
            Status_Write("File: " + e.SelectedFile.Name + " | " + e.SelectedFile.Size); 
            Status_EventEnd("FileTooLarge"); 
        } 
 
        private void RadUpload_FileUploaded(object sender, FileUploadedEventArgs e) 
        { 
            Status_EventEnter("FileUploaded"); 
            Status_Write("Uploaded File:" + e.SelectedFile.Name + " | " + e.HandlerData.IsSuccess); 
            Status_EventEnd("FileUploaded"); 
        } 
 
        private void RadUpload_FileUploadFailed(object sender, FileUploadFailedEventArgs e) 
        { 
            Status_EventEnter("FileUploadFailed"); 
            e.ErrorMessage = "My Message"
            Status_Write("Failed Message:" + e.HandlerData.Message); 
            Status_EventEnd("FileUploadFailed"); 
        } 
 
        private void RadUpload_FileUploadStarting(object sender, FileUploadStartingEventArgs e) 
        { 
            RadUpload upload1 = sender as RadUpload; 
            Status_EventEnter("FileUploadStarting"); 
            e.FileParameters.Add("myParam1", "myParam1Value"); 
            Status_Write("Uploading File:" + e.SelectedFile.Name + " | " + e.UploadData.FileName); 
            Status_EventEnd("FileUploadStarting"); 
        } 
 
        private void RadUpload_ProgressChanged(object sender, EventArgs e) 
        { 
            RadUpload upload1 = sender as RadUpload; 
            Status_EventEnter("ProgressChanged"); 
            DumpCurrentSession(sender); 
            Status_EventEnd("ProgressChanged"); 
             
        } 
 
        private void DumpCurrentSession(object sender) 
        { 
            RadUpload upload1 = sender as RadUpload; 
            Status_Write("Current Progress:" + upload1.CurrentSession.CurrentProgress.ToString()); 
            Status_Write("Current File:" + upload1.CurrentSession.CurrentFile.Name); 
            Status_Write("Current FileProgress:" + upload1.CurrentSession.CurrentFileProgress); 
            Status_Write("TotalFilesCount:" + upload1.CurrentSession.TotalFilesCount); 
            Status_Write("TotalFileSize:" + upload1.CurrentSession.TotalFileSize); 
            Status_Write("UploadedBytes:" + upload1.CurrentSession.UploadedBytes); 
            Status_Write("UploadedFilesCount:" + upload1.CurrentSession.UploadedFiles.Count); 
        } 
 
        private void RadUpload_UploadCanceled(object sender, EventArgs e) 
        { 
            RadUpload upload1 = sender as RadUpload; 
            Status_EventEnter("UploadCanceled"); 
            Status_Write("Current File is:" + upload1.CurrentSession.CurrentFile.Name); 
            Status_EventEnd("UploadCanceled"); 
        } 
 
        private void RadUpload_UploadFinished(object sender, EventArgs e) 
        { 
            Status_EventEnter("UploadFinished"); 
            DumpCurrentSession(sender); 
            Status_EventEnd("UploadFinished"); 
        } 
 
        private void RadUpload_UploadPaused(object sender, EventArgs e) 
        { 
            Status_EventEnter("UploadPaused"); 
            Status_EventEnd("UploadPaused"); 
        } 
 
        private void RadUpload_UploadResumed(object sender, EventArgs e) 
        { 
            Status_EventEnter("UploadResumed"); 
            Status_EventEnd("UploadResumed"); 
        } 
 
        private void RadUpload_UploadSizeExceeded(object sender, RoutedEventArgs e) 
        { 
            Status_EventEnter("UploadSizeExceeded"); 
            Status_EventEnd("UploadSizeExceeded"); 
        } 
 
        private void RadUpload_UploadStarted(object sender, UploadStartedEventArgs e) 
        { 
            Status_EventEnter("UploadStarted"); 
            Status_EventEnd("UploadStarted"); 
        } 
 
        
 
        private void Status_EventEnd(string p) 
        { 
            TextOutput.Text = "-/----" + p + "\n" + TextOutput.Text; 
        } 
 
        private void Status_Write(string p) 
        { 
            TextOutput.Text = "-" + p + "\n" + TextOutput.Text; 
        } 
 
        private void Status_EventEnter(string p) 
        { 
            TextOutput.Text = "-----" + p + "\n" + TextOutput.Text; 
        } 
 
        private void btnClear_Click(object sender, System.Windows.RoutedEventArgs e) 
        { 
            TextOutput.Text = ""
        } 
    } 

upload.ashx.cs
public class Upload :  Telerik.Windows.RadUploadHandler 
    { 
        public override void ProcessStream() 
        { 
            string fileName = this.Request.Form[RadUploadConstants.ParamNameFileName]; 
 
            string filePath = this.GetFilePath(fileName); 
 
            this.AddReturnParam(RadUploadConstants.ParamNameFinalFileRequest, this.IsFinalFileRequest()); 
 
            if (filePath == null) 
            { 
                this.AddReturnParam(RadUploadConstants.ParamNameSuccess, false); 
                this.AddReturnParam(RadUploadConstants.ParamNameMessage, String.Format("Cannot find the target folder [{0}]", this.GetTargetFolder())); 
                return; 
            } 
            else if (this.IsNewFileRequest() && File.Exists(filePath) && !this.OverwriteExistingFiles) 
            { 
                this.AddReturnParam(RadUploadConstants.ParamNameSuccess, false); 
                this.AddReturnParam(RadUploadConstants.ParamNameMessage, String.Format("File already exists name:[{0}], path:[{1}]", fileName, filePath)); 
                return; 
            } 
 
            byte[] buffer; 
            try 
            { 
                buffer = Convert.FromBase64String(this.Request.Form[RadUploadConstants.ParamNameData]); 
            } 
            catch (System.FormatException) 
            { 
                this.AddReturnParam(RadUploadConstants.ParamNameSuccess, false); 
                this.AddReturnParam(RadUploadConstants.ParamNameMessage, "Bad stream format"); 
                return; 
            } 
 
            long position = Convert.ToInt64(this.Request.Form[RadUploadConstants.ParamNamePosition]); 
            long contentLength = 0
            string guid; 
            FileStream stream = null
            try 
            { 
                PhotoTable.Initialize(); 
                 
                if (this.IsNewFileRequest()) 
                { 
                    guid = Guid.NewGuid().ToString(); 
                    PhotoManager.CreateTempFile(guid, buffer); 
                          
                } 
                else 
                { 
                    int offset = Convert.ToInt32(this.Request.Form[RadUploadConstants.ParamNamePosition]); 
                    //guid = this.Request.Form[RadUploadConstants.ParamNameGuid]; 
                    guid = this.FormChunkTag; 
                    PhotoManager.WriteToTempFile(guid, buffer, offset); 
                } 
 
                 
 
                if (this.IsFinalFileRequest() && this.IsFinalUploadRequest()) 
                { 
                    //this.AddReturnParam(RadUploadConstants.ParamNameFinalFileRequest,true); 
                 
                } 
 
                this.ResultChunkTag =  guid
                contentLength = position + buffer.Length; 
                 
                this.AddReturnParam(RadUploadConstants.ParamNamePosition, contentLength); 
                base.ProcessStream();       
 
                //PhotoManager.AddPhoto(Guid.NewGuid().ToString(), filePath, buffer); 
            } 
            catch (Exception e) 
            { 
                this.AddReturnParam(RadUploadConstants.ParamNameSuccess, false); 
                this.AddReturnParam(RadUploadConstants.ParamNameMessage, String.Format("Cannot open the file for writing [{0}]", e.Message)); 
            } 
            finally 
            { 
                if (stream != null) 
                { 
                    stream.Dispose(); 
                } 
            } 
 
            if (contentLength == 0) 
            { 
                return; 
            } 
 
            buffer = null
 
            this.AddReturnParam(RadUploadConstants.ParamNameSuccess, true); 

Let me know if I can provide any more info

thanks
Channa


1 Answer, 1 is accepted

Sort by
0
Ivan
Telerik team
answered on 30 Sep 2009, 08:54 PM
Hi Mac & Cheese,

Thank you for the code you sent us.

After previewing your code we found that some of return-parameters are missed. Please add the following code at the end of your ProcessStream method:

  this.AddReturnParam(RadUploadConstants.ParamNameSuccess, success); 
 
  if (this.IsFinalFileRequest()) 
  { 
    this.AddReturnParam(RadUploadConstants.ParamNameFileIdent, filePath); 
    this.AddReturnParam(RadUploadConstants.ParamNameFileName, fileName); 
    this.AddReturnParam(RadUploadConstants.ParamNameFilePath, filePath); 
  } 
 
  this.AddReturnParam(RadUploadConstants.ParamNameFinalFileRequest,this.IsFinalFileRequest()); 
 
// end of the ProcessStream method. 

We hope this information will help you.

Regards,
Ivan
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Tags
Upload
Asked by
Nisi
Top achievements
Rank 1
Answers by
Ivan
Telerik team
Share this question
or