RadUpload Control with Silverlight fails to upload pdf and word files

4 posts, 0 answers
  1. Julio
    Julio avatar
    8 posts
    Member since:
    Feb 2012

    Posted 17 Jul 2013 Link to this post

    Hi to all; I have an application which uses the radupload control to upload files into a SQL Database and we have found in certain situations after clicking the button upload the radupload fails to upload and instead of showing cancel pause buttons, it shows cancel and browse.

    After doing some testing we have found that pdf and word files are the one causing the issue.

    The following is the xaml for the RadUpload file
    <telerik:RadUpload x:Name="ruAttachment"
             Grid.Row="0"
             Margin="10"
             Width="320"
             MinWidth="320"
             HorizontalAlignment="Left"
             VerticalAlignment="Stretch"
             IsAutomaticUpload="False"
             UploadServiceUrl="../AttachmentsPOUploadHandler.ashx"
             IsEnabled="{Binding Content.CanUploadAttachment, Source={StaticResource ViewModelProxy}}"
             IsAppendFilesEnabled="False"
             OverwriteExistingFiles="True"
             SingleFilePerPostRequest="True"
             IsMultiselect="False"
             FilesSelected="RuAttachmentFilesSelected"
             FileUploadStarting="RuAttachmentFileUploadStarting"
             FileUploaded="RuAttachmentFileUploaded"
             FileUploadFailed="RuAttachment_OnFileUploadFailed"
             ItemContainerStyle="{StaticResource OWRadUploadItemStyle}"
             Visibility="{Binding Content.CanExecuteEdit, Source={StaticResource ViewModelProxy},Converter={StaticResource BooleanToVisibilityValueConverter}}" />

    This the C# for all 4 events :
    private void RuAttachmentFileUploadStarting(object sender, FileUploadStartingEventArgs e)
            {
                e.FileParameters.Add("OrderId", ViewModel.SelectedOrder.Id);
                e.FileParameters.Add("UserId", ClaimsIdentitySessionManager.Current.User.Identity.Name);
                e.FileParameters.Add("FileName", ViewModel.SelectedFile.Name);
                e.FileParameters.Add("Extension", ViewModel.SelectedFile.Extension);
     
                if (ViewModel.SelectedDocumentType == null || ViewModel.SelectedDocumentType.Id == 0)
                {
                }
                else
                    e.FileParameters.Add("DocTypeId", ViewModel.SelectedDocumentType.Id);
            }
     
            private void RuAttachmentFileUploaded(object sender, FileUploadedEventArgs e)
            {
                ViewModel.LoadAttachments();
                if (OrderSubmitted != null) // Refresh the attachments grid
                    OrderSubmitted(this, null);
            }
     
            private void RuAttachmentFilesSelected(object sender, FilesSelectedEventArgs e)
            {
                var uploadControl = sender as RadUpload;
                // Remove and add validate event handler
                uploadControl.RemoveHandler(RadUploadItem.ValidateEvent, new UploadValidateEventHandler(OnValidateAttchment));
                uploadControl.AddHandler(RadUploadItem.ValidateEvent, new UploadValidateEventHandler(OnValidateAttchment), false);
                ViewModel.SelectedFile = e.SelectedFiles[0].File;
                Thread.Sleep(2000);
            }
     
            private void RuAttachment_OnFileUploadFailed(object sender, FileUploadFailedEventArgs e)
            {
     
            }

    This is the ashx C# code:
    public class AttachmentsPOUploadHandler : RadUploadHandler
       {
           #region Fields
           #endregion
     
           #region Properties
           #endregion
     
           #region Methods
     
           public override bool SaveChunkData(string filePath, long position, byte[] buffer, int contentLength, out int savedBytes)
           {
               bool result = false;
               savedBytes = 0;
     
               if (this.IsFinalFileRequest())
               {
                   OWEntities context = new OWEntities();
     
                   int orderId = 0;
     
                   if (int.TryParse(this.GetQueryParameter("OrderId"), out orderId))
                   {
                       //get order from database
                       POHeader order = context.POHeaders.FirstOrDefault(o => o.Id == orderId);
     
                       //get the document extention detail based on file extension from database                  
                       string extension = this.GetQueryParameter("Extension").Trim('.').ToUpper();
                       DocumentExtension docExtension = context.DocumentExtensions.FirstOrDefault(d => d.ExtType.Trim().ToUpper().Equals(extension));
     
                       //create attachment
     
                       if (docExtension != null)
                       {
                           POAttachment attachment = new POAttachment()
                           {
                               CreatedById = this.GetQueryParameter("UserId"),
                               CreatedDate = DateTime.Now,
                               DocExt = extension,
                               DocExtId = docExtension.Id,
                               DocName = this.GetQueryParameter("FileName"),
                               DocSize = contentLength,
                               DocTypeId = Convert.ToInt16(this.GetQueryParameter("DocTypeId")),
                               POFile = new POFile()
                               {
                                   CreatedById = this.GetQueryParameter("UserId"),
                                   CreatedDate = DateTime.Now,
                                   Document = buffer,
                                   POHeaderId = orderId
                               },
                               POHeaderId = orderId
                           };
     
                           context.POAttachments.AddObject(attachment);
                           context.SaveChanges();
                       }
     
                       result = true;
                   }
     
               }
     
               return result;
           }
     
           #endregion
     
       }


    One last thing even the radupload control is configured with SingleFilePerPostRequest = "True"  in the ashx the following conditions always is false this.IsFinalFileRequest()

  2. Kiril Vandov
    Admin
    Kiril Vandov avatar
    324 posts

    Posted 22 Jul 2013 Link to this post

    Hello Julio,

    When you are trying to upload bigger files, they are divided into smaller pieces and passed to the SaveChunkData method as parameter "byte[] buffer". That is why when you try to upload a big file and the SaveChunkData method returns false the upload will fail and the this.IsFinalFileRequest() will never be true. The IsFinalFileRequest() is true only when the last chunk of the uploaded file is passed to the SaveChunkData. The SaveChunkData should always return true if you want to proceed the next chunk of information.

    Given the above information you could modify you SaveChunkData to return true. Also you could use a List<byte[]>() to collect the buffer received in the SaveChunkData and combine the whole file once the last peace of the file is processed, like follows:
    static List<byte[]> FileChunks = new List<byte[]>();
     
    public override bool SaveChunkData(string filePath, long position, byte[] buffer, int contentLength, out int savedBytes)
    {
      FileChunks.Add(buffer);
        if (this.IsFinalFileRequest())
            {
             byte[] File = this.CombineFile();
                     FileChunks = new List<byte[]>();
    ...
            }
            return true;
    }
     
     
    private byte[] CombineFile()
    {
        var arrayLenght = FileChunks.Sum(x => x.Length);
        byte[] array = new byte[arrayLenght];
        var arrayOffset = 0;
        foreach (var arr in FileChunks)
        {
            arr.CopyTo(array, arrayOffset);
            arrayOffset += arr.Length;
        }
        return array;
    }
    and assign the new File array to your POFiles.Document property.

    I hope this information helps.

    Kind regards,
    Kiril Vandov
    Telerik
    TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for SILVERLIGHT.
    Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
    Sign up for Free application insights >>
  3. DevCraft banner
  4. Julio
    Julio avatar
    8 posts
    Member since:
    Feb 2012

    Posted 24 Jul 2013 Link to this post

    Thank you for your reply, I will try the code but I still have one question, about the "big files" when I test this, the files are not bigger than 100KB, the same size as the file the actually work.

    I what I am trying to say is:

    If I try to upload a Order.xlsx  of 151KB  the control works, the file gets uploaded.
    If I try to upload an Screen.jpg of 400KB the control works, the file gets uploaded.
    If I try to upload a invoice.pdf of 101 KB the control fails, the file doesn't get uploaded and the control shows again the browse button.
    If I try to upload a letter.docx of 101 KB the control fails, the file doesn't get uploaded and the control shows again the browse button.

    This situation was the one that make me think there is a issue with those type of files. 

  5. Tina Stancheva
    Admin
    Tina Stancheva avatar
    3298 posts

    Posted 29 Jul 2013 Link to this post

    Hi Julio,

    The RadUpload control doesn't restrict the upload of files with specific extensions. This is why I'm not sure what might be causing this issue. If you'd like you can try uploading the same files in our RadUpload sample demo.

    You can also check to make sure that the settings of the server where you try to upload the files allow files with extensions of type docx and pdf to be saved.

    Regards,
    Tina Stancheva
    Telerik
    TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for SILVERLIGHT.
    Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
    Sign up for Free application insights >>
Back to Top