Error Handling in ProcessStream

5 posts, 0 answers
  1. Robert
    Robert avatar
    12 posts
    Member since:
    Jul 2010

    Posted 31 Oct 2012 Link to this post

    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;
                }
            }
  2. Robert
    Robert avatar
    12 posts
    Member since:
    Jul 2010

    Posted 31 Oct 2012 Link to this post

    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 :-(
  3. DevCraft banner
  4. Tina Stancheva
    Admin
    Tina Stancheva avatar
    3298 posts

    Posted 05 Nov 2012 Link to this post

    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.

  5. Robert
    Robert avatar
    12 posts
    Member since:
    Jul 2010

    Posted 05 Nov 2012 Link to this post

    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"

  6. Tina Stancheva
    Admin
    Tina Stancheva avatar
    3298 posts

    Posted 08 Nov 2012 Link to this post

    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.

Back to Top
DevCraft banner