Programmatically cancel file

3 posts, 1 answers
  1. christina noel
    christina noel avatar
    42 posts
    Member since:
    Mar 2010

    Posted 23 May 2012 Link to this post


    In order to enable our users to drag and drop files into the Silverlight RadUpload on FireFox on a Mac, I've had to set up a complicated system where the drop gets detected by javascript and the file gets transformed into a data format that I can pass up to the Silverlight control.

    I then use that data to make a MemoryStream that I can create a RadUploadSelectedFile from, and add it to the CurrentSession:
    RadUploadSelectedFile f = new RadUploadSelectedFile(new MemoryStream(data), name);

    After each file is added I call PrepareSelectedFilesForUpload so that the user can see the file added and know that progress is being made on the set of files they dropped.

    This process is generally working great. However, sometimes the javascript fails because the file is too large. When this happens the data comes back null, so I have no data to make a MemoryStream with and no idea how large the file was. I would like to be able to add an UploadItem to the Uploader anyway, but flag it with the "Too Large" error message so the user knows what happened to the file they picked. I definitely don't want to waste any time or processor power trying to upload the non-existant file, so I need the actual upload to skip it.

    I've tried making a RadUploadSelectedFile from a zero-length MemoryStream, which gave me an UploadItem (yay!). However, adding it to the "TooLargeFiles" collection didn't seem to do anything -- it didn't show an error message to the user, and it uploaded a zero-length file to my server.

    Is there a way to do what I want? I'm willing to handle any function I need to to get this to happen.

    Thanks for your help!
  2. Answer
    Tina Stancheva
    Tina Stancheva avatar
    3299 posts

    Posted 28 May 2012 Link to this post

    Hi Christina,

    Can you apply a MaxFileSize limitation on the RadUpload control? If you can do that, you can use this value to create an empty array with a larger size and use it to create the RadUploadSelectedFile:
    <telerik:RadUpload x:Name="xRadUpload" MaxFileSize="1000000"
                 ... />

    byte[] emptyArray = new byte[xRadUpload.MaxFileSize+100];
    MemoryStream str = new MemoryStream(emptyArray);
    RadUploadSelectedFile file = new RadUploadSelectedFile(str, "File");

    This way as soon as you hit Upload, a notification icon and a tooltip with an error message will be displayed.

    Please give this a try and let us know if it works for you.

    All the best,
    Tina Stancheva
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

  3. christina noel
    christina noel avatar
    42 posts
    Member since:
    Mar 2010

    Posted 30 May 2012 Link to this post

    This is a solution to the problem I described.

    However, this solution is not acceptable for another reason that I didn't make clear in my original description: If this same "too large" file is selected via the Browse button, the upload should work instead of failing because the file size was so large. Your implementation will take away the option of our FireFox-on-Mac users to use the Browse button to get around the file size limit imposed by the complex workaround needed for FireFox-on-Mac drag and drop.

    Fortunately, a little more digging and discussion on our end has found a method for doing this:
    1) When the 0-byte memory stream is detected, find the upload item using FileNameUploadItem.
    2) Set the ErrorMessage property of the Upload Item (giving an immediate error warning) and attach a Validate event handler to the Upload Item.
    3) In the Validate event handler, set the eventargs ErrorMessage property and set Cancel=true (stopping the actual upload).

    I'm marking this issue resolved because we now have a working solution. Thank you for your assistance.

Back to Top