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

Error Handling in ProcessStream

4 Answers 66 Views
Upload
This is a migrated thread and some comments may be shown as answers.
Robert
Top achievements
Rank 1
Robert asked on 31 Oct 2012, 07:31 PM
I am using the RadUpload in a SharePoint 2010 page to set property values and upload documents to a document library. 

The question I have is:  If an error occurs within the try/catch block of ProcessStream(), the error shown in the Silverlight client is "Handler not found or execution of the handler failed".  How should an error be handled that occurs during the ProcessStream()?  (I've tried adding this.AddReturnFileParam as well as this.AddReturnParam, but on the FileUploadFailed event, the e.HandlerData.CustomData is empty.) 

A related question:  If you choose to try to upload a new file in the RadUpload control after a failure in the ProcessStream(), it will appear to start uploading, but never upload the file.  The FileUploadFailed event files, but the UploadFinished never fires - so it appears as through the previous process is hanging and not allowing subsequent uploads.  How do I terminate the previous process to allow a new attempt after a failed upload?

I've posted reduced code below.

public override bool SaveChunkData(string filePath, long position, byte[] buffer, int contentLength, out int savedBytes)
        {
            try
            {
                requestUrl = this.GetQueryParameter("requestUrl");
                docLibName = this.GetQueryParameter("docLibName");
                currentFolderName = this.GetQueryParameter("currentFolderName");
 
                //get dynamic property list
                propertyList = this.GetQueryParameter("propertyList");
 
                bool result;
                this.PrepareAccountSession();
                if (!this.PrepareStorageFolder())
                {
                    result = false;
                }
 
                string FilePath = this.GetFilePath();
                result = base.SaveChunkData(FilePath, position, buffer, contentLength, out savedBytes);
 
                return result;
            }
            catch (Exception ex)
            {
                this.AddReturnParam(RadUploadConstants.ParamNameMessage, ex.Message);
                this.AddReturnFileParam(RadUploadConstants.ParamNameMessage, ex.Message);
                string fileName = this.GetQueryParameter(RadUploadConstants.ParamNameFileName);
                this.AddReturnFileParam(RadUploadConstants.ParamNameSuccess, false);
                this.AddReturnFileParam(RadUploadConstants.ParamNameFileName, fileName);
                this.AddReturnFileParam(RadUploadConstants.ParamNameFinalFileRequest, true);
                savedBytes = 0;
                return false;
            }
        }


public override void ProcessStream()
        {
            try
            {
                base.ProcessStream();
                for (int i = 0; i < Request.Files.Count; i++)
                {
                    HttpPostedFile file = Request.Files[i];
                    string fileName = this.Request.Form[i.ToString() + "_RadUAG_fileName"]; // GetFileName();
                    string fullPath = this.GetTargetFolder();
                    string filename = fullPath + "\\" + fileName;
 
                    if (file.ContentLength != 0)
                    {
                        file.SaveAs(filename);
                        FileStream stream = new FileStream(filename, FileMode.Open);
                        SPSite site = new SPSite(requestUrl);
                        using (SPSite elevatedSite = new SPSite(requestUrl))
                        {
                            using (SPWeb web = elevatedSite.OpenWeb())
                            {
                                web.AllowUnsafeUpdates = true;
                                SPFile newFile;
 
                                //using the below method for any property types such as multichoice will throw the error:
                                //"Only String, INT, and DateTime datatypes can be used as the value in Properties."
                                //at Microsoft.SharePoint.Utilities.SPUtility.UpdateArrayFromHashtable(Object& o, Hashtable ht)
                                //Hashtable hashtable = new Hashtable();
                                //hashtable.Add(propKey, propValue);
                                //Hashtable properties = hashtable;
 
                                if (currentFolderName == "/")
                                {
                                    //use root folder
                                    SPDocumentLibrary library = (SPDocumentLibrary)web.Lists[docLibName];
                                    SPFolder rootFolder = library.RootFolder;
 
                                    //use this method if using hashtable for properties
                                    //SPFile newFile = rootFolder.Files.Add(string.Format("{0}/{1}", rootFolder.Url, fileName), stream, properties, true);
 
                                    //use this method if altering the list item after upload
                                    newFile = rootFolder.Files.Add(string.Format("{0}/{1}", rootFolder.Url, fileName), stream, true);
                                }
                                else
                                {
                                    SPFolder currentSPFolder = web.GetFolder(currentFolderName);
                                    newFile = currentSPFolder.Files.Add(string.Format("{0}/{1}", currentSPFolder.Url, fileName), stream, true);
                                }
 
                                //set item properties
                                if (newFile.Item != null)
                                {
                                    SPListItem listItem = newFile.Item;
 
                                    //assign column values here
 
                                    //throw error when column does not exist
                                    listItem["SomeBadColumn"] = "FOO";
 
                                    listItem.Update();
                                    newFile.Update();
 
                                }
 
                                stream.Close();
                            }
                        }
                    }
                } //for
 
            }
            catch (Exception ex)
            {
        this.AddReturnParam(RadUploadConstants.ParamNameMessage, ex.Message);
                this.AddReturnFileParam(RadUploadConstants.ParamNameMessage, ex.Message);
 
                string fileName = this.Request.Form[RadUploadConstants.ParamNameFileName];
                string filePath = this.GetTargetFolder();
 
                this.AddReturnFileParam(RadUploadConstants.ParamNameSuccess, false);
                this.AddReturnFileParam(RadUploadConstants.ParamNameTargetFolder, filePath);
                this.AddReturnFileParam(RadUploadConstants.ParamNameFileName, fileName);
                this.AddReturnFileParam(RadUploadConstants.ParamNameTargetPhysicalFolder, filePath);
                this.AddReturnFileParam(RadUploadConstants.ParamNameFinalFileRequest, true);
                return;
            }
        }

4 Answers, 1 is accepted

Sort by
0
Robert
Top achievements
Rank 1
answered on 31 Oct 2012, 08:06 PM
So, after reading more post - I've moved all of my SharePoint processing into the SaveChunkData function.  although this hasn't solved the issue with being able to "re-try" uploads, I can now get my error message back to the client.

It would be really helpful if Telerik could create a new sample for everyone.  The documentation/forums/KB are all over the place :-(
0
Tina Stancheva
Telerik team
answered on 05 Nov 2012, 11:50 AM
Hi Robert,

I agree that there are still resources pointing to overriding the ProcessStrream method, but we did our best to point our customers towards overriding the SaveChunkData() method instead. The reasons behind that change were first described in this KB article. This is why I wanted to ask you if you can list the resources that pointed you in the wrong direction so that we can update them accordingly.

In the meantime, we've uploaded a sample solution demonstrating how to upload files in a SharePoint directory in this forum thread.

Unfortunately, I am not sure why the UploadFinished event isn't fired - it might be caused by your SaveChunkData implementation. But even in that case, you can call the RadUpload.CancelUplod() method to reset the control and prepare it for a new upload operation. However, you'll need to delay the method execution to make sure the upload is ready to clear its items and reset its state - for example you can use a Dispatcher to cancel the failed upload operation:
private void RadUpload1_FileUploadFailed(object sender, FileUploadFailedEventArgs e)
{
    Dispatcher.BeginInvoke(() => { (sender as RadUpload).CancelUpload(); });
}
 
I hope this information helps. Let me know if I can further assist you.

Kind regards,
Tina Stancheva
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

0
Robert
Top achievements
Rank 1
answered on 06 Nov 2012, 12:30 AM
I would have to go back to find all of the different references I read- but let's start with the two samples you just provided.  They contradict each other.  The sample solution for file uploads into SharePoint ("Assistance with Upload and SharePoint 2010 ") overwrites ProcessStream - directly contradicting the KB you provide which states "If you have an overwritten version of ProcessStream you should re-write it again"

0
Tina Stancheva
Telerik team
answered on 08 Nov 2012, 09:48 AM
Hello Robert,

You're right that the forum thread starts with a suggestion to override the ProcessStream method, but that is due to the fact that the initial approach was based on an outdated example. I actually send the forum thread as at its bottom I have attached an updated version of the solution demonstrating how to move the custom logic from the PrecessStream() to the SaveChunkData() method.

All the best,
Tina Stancheva
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

Tags
Upload
Asked by
Robert
Top achievements
Rank 1
Answers by
Robert
Top achievements
Rank 1
Tina Stancheva
Telerik team
Share this question
or