RadUpload replacement of "File Being Used" error message

18 posts, 1 answers
  1. mathieu
    mathieu avatar
    4 posts
    Member since:
    Apr 2011

    Posted 11 Aug 2011 Link to this post

    Hello,

    I am using the RadUploadHandler to let the user upload files to my application, but if the file the user tries to upload is open (for exemple by Excel), I get a message "The process cannot access the file 'My_file.csv' because it is being used by another process.". I would like to change that message, so I tried hooking onto the event FileUploadedFailed to rewrite the ErrorMessage property as such:

    <telerik:RadUpload AllowDrop="True"
                      IsAutomaticUpload="True"
                      UploadServiceUrl="/FileUploadHandler.ashx"
                      FileUploaded="radUpload_FileUploaded"
                      FileUploadFailed="radUpload_FileUploadFailed" />

    private void radUpload_FileUploadFailed(object sender, FileUploadFailedEventArgs e)
    {
    e.ErrorMessage = "my useful message";
    }

    But it turns out the radUpload_FileUploadFailed method never gets called when the upload fails because of a file already open. How can I rewrite the error message?
  2. Petar Mladenov
    Admin
    Petar Mladenov avatar
    2891 posts

    Posted 16 Aug 2011 Link to this post

    Hello Mathieu,

     There is no event that fires in such situation (upload failure due to opened file). So you try to iterate over the files that are to be uploaded and check whether they are open. Then you can show your own MessageBox and prevent the default Message. Please let us know if such kind of approach satisfies you.

    All the best,
    Petar Mladenov
    the Telerik team

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

  3. DevCraft banner
  4. mathieu
    mathieu avatar
    4 posts
    Member since:
    Apr 2011

    Posted 16 Aug 2011 Link to this post

    Hi Petar,

    Showing a pop-up instead of the default message would work in my case, but I don't know how to open (or even try to open) the file as my application runs in a browser and as such, I don't have direct access to the file that I try to open. The only way I know to open a file in Silverlight is to use the OpenFileDialog, but that is being taken cared of by the RadUpload control. I also tried this but it sent me a security exception as I thought:
    private void radUpload1_FileUploadStarting(object sender, FileUploadStartingEventArgs e)
    {
    var isReadOnly = (e.SelectedFile.File.Attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly;
    }

    Where can I iterate over the file to check for this?

    Thanks for your help!
  5. Answer
    Petar Mladenov
    Admin
    Petar Mladenov avatar
    2891 posts

    Posted 19 Aug 2011 Link to this post

    Hi Mathieu,

    Please first excuse me for not being clear enough last time. We investigate this further and it appears that there is no elegant way to accomplish this. There is a workaround but please note that this is very bad code:

    private void radUpload1_FilesSelected(object sender, Telerik.Windows.Controls.FilesSelectedEventArgs e)
           {
               Dispatcher.BeginInvoke(() =>
                   {
                       foreach (var item in this.radUpload1.Items)
                       {
                           if ((item as RadUploadItem).ErrorMessage.StartsWith("The process cannot access the file"))
                           {
                               MessageBox.Show("File is open!");
                           }
                       }
                   }
               );
           }
    So basically , we determine this scenario as not supported. Please again excuse us for the inconvenience caused.Regards,
    Petar Mladenov
    the Telerik team

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

  6. mathieu
    mathieu avatar
    4 posts
    Member since:
    Apr 2011

    Posted 24 Aug 2011 Link to this post

    Hi Petar,

    I'll take any bad code that works in this case, it's something we really need. However your example doesn't compile in my project, ErrorMessage or Message seems to be a private (or protected or internal) property that I can't access or set, and using reflection isn't an option since we're not using higher trust policies. I see it when I debug with Intellisense, but trying to read or set it using the keyword dynamic throws a SecurityException because it's not public. Will there by any enhancement or another trick I could use that would help me in this case?

    Thanks,
    Mathieu
  7. Petar Mladenov
    Admin
    Petar Mladenov avatar
    2891 posts

    Posted 30 Aug 2011 Link to this post

    Hi Mathieu,

     What is the RadControl's version that you use ? This code works fine with the Official Q2 2011 Release. The ErrorMessage  property has been marked as public several Queues ago, so probably that's why it does not compile at your side.

    Best wishes,
    Petar Mladenov
    the Telerik team

    Thank you for being the most amazing .NET community! Your unfailing support is what helps us charge forward! We'd appreciate your vote for Telerik in this year's DevProConnections Awards. We are competing in mind-blowing 20 categories and every vote counts! VOTE for Telerik NOW >>

  8. mathieu
    mathieu avatar
    4 posts
    Member since:
    Apr 2011

    Posted 31 Aug 2011 Link to this post

    Hi Petar,

    I tried your code once again and it worked, I must have done something wrong the first time I tried. Thanks for your help!
  9. Petar Mladenov
    Admin
    Petar Mladenov avatar
    2891 posts

    Posted 02 Sep 2011 Link to this post

    Hi Mathieu,

     I'm glad to hear that you have succeeded. Please feel free to ask if you need further assistance.We would be glad to help you.

    Best wishes,
    Petar Mladenov
    the Telerik team

    Thank you for being the most amazing .NET community! Your unfailing support is what helps us charge forward! We'd appreciate your vote for Telerik in this year's DevProConnections Awards. We are competing in mind-blowing 20 categories and every vote counts! VOTE for Telerik NOW >>

  10. Jonx
    Jonx avatar
    258 posts
    Member since:
    Jul 2012

    Posted 02 Apr 2012 Link to this post

    It's true that it's working but why not offer a cleaner way to deal with this? Like provide an event or bubble this error in the FileUploadFailed event... which would be a breaking change I guess...
  11. Tina Stancheva
    Admin
    Tina Stancheva avatar
    3298 posts

    Posted 04 Apr 2012 Link to this post

    Hi John,

    Thank you for your feedback on the matter. We can see your point and this is why we decided to log your sugegstion as a feature request in our PITS. If the item gathers enough popularity, we will definitely consider its implementation.

    I also updated your Telerik account for this feedback.

    Regards,
    Tina Stancheva
    the Telerik team
    Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
  12. Harpreet
    Harpreet avatar
    4 posts
    Member since:
    Aug 2012

    Posted 07 May 2012 Link to this post

    Hi Petar,

    I am using Silverlight Rad controls product version 2011.1.0502.1040 .
    I have faced same problem with my RadUpload control.I have applied code fix suggested by you.
    private void radUpload1_FilesSelected(object sender, Telerik.Windows.Controls.FilesSelectedEventArgs e)
           {
               Dispatcher.BeginInvoke(() =>
                   {
                       foreach (var item in this.radUpload1.Items)
                       {
                           if ((item as RadUploadItem).ErrorMessage.StartsWith("The process cannot access the file"))
                           {
                               MessageBox.Show("File is open!");
                           }
                       }
                   }
               );
           }
    It worked fine at that time.But now the new problem occurred.Localization is implemented in my application.
    When I change localization to German "de-DE" .Then the RadUpload error message become localized and the code stop working.
    Do we have any alternative way of this.Like  "we can stop the localization at control level" or "we can get error type when file is in use" .

    Please provide me suggestions regarding this problem.

    Thanks & Regards,
    Harpreet Singh.
  13. Tina Stancheva
    Admin
    Tina Stancheva avatar
    3298 posts

    Posted 07 May 2012 Link to this post

    Hi Harpreet,

    Unfortunately the ErrorMessage comes as a string so you can't use its type or any other similar approach. You'll have to extend the logic to look for the localized string as well.

    But you can vote for the PITS item to increase its priority, as its implementation will expose an event that you will be able to handle in order to implement your custom logic.

    Kind regards,
    Tina Stancheva
    the Telerik team

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

  14. Hans
    Hans avatar
    26 posts
    Member since:
    May 2012

    Posted 20 Mar 2013 Link to this post

    public void OnFilesSelected(object sender, FilesSelectedEventArgs e)
    {
        if (e.SelectedFiles[0].Size == 0)
        {
            // file is in use
        }
    }
  15. Rakesh
    Rakesh avatar
    2 posts
    Member since:
    Jul 2013

    Posted 20 Sep 2013 Link to this post

    Hello,
    We are also facing this issue. When trying to upload a word file that is already open, upload fails. In this case we would like to display error message.
    To do it, Instead of relying on the exact string returned by  "RadUploadItem.ErrorMessage" (which is localized string) can we just check if that string is non-empty (indicating error) or empty (indicating no error), as shown in the below code
    Means if "RadUploadItem.ErrorMessage" is empty there is no error and RadUpload control can upload file.
    If RadUploadItem.ErrorMessage is nonempty string (content of the string is ignored) RadUpload can not access the file and upload it.

    Thank You,
    Rakesh

    private bool CheckForErrors()
            {
                bool errsFound = false;
                int count = this.radUpload.Items.Count;
     
                for (int i = 0; i < count; ++i)
                {
                    RadUploadItem radUploadItem = this.radUpload.Items[i] as RadUploadItem;
                    if (radUploadItem != null)
                    {
                        string err = radUploadItem.ErrorMessage;
                        if (string.IsNullOrWhiteSpace(err) == false)
                        {
                            errsFound = true;
                            break;
                        }
                    }
                    else
                    {
                        errsFound = true;
                        break;
                    }
                }
                return errsFound;
            }

  16. Kiril Vandov
    Admin
    Kiril Vandov avatar
    324 posts

    Posted 25 Sep 2013 Link to this post

    Hello Rakesh,

    You could use FilesSelected event of the RadUpload and access the Items of the upload control. Unfortunately there is no elegant way to accomplish this and you need to access the items like follows:
    private void RadUpload1_FilesSelected(object sender, FilesSelectedEventArgs e)
    {
        Dispatcher.BeginInvoke(() =>
        {
            var radUpload = sender as RadUpload;
            foreach (var item in radUpload.Items)
            {
                if ((item as RadUploadItem).ErrorMessage != string.Empty)
                {
                    MessageBox.Show("File is open!");
                }
            }
        });
    }
    I hope this information helps.

    Please excuse us for any inconvenience caused.

    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 >>
  17. Brian Sayatovic
    Brian Sayatovic avatar
    91 posts
    Member since:
    Nov 2009

    Posted 16 Sep 2014 in reply to Kiril Vandov Link to this post

    • I'm confused by this post because everyone seems to have a solution that works.  But that solution doesn't work for me.

      The exception I'm seeing is a System.Security.SecurityException.  It's being by the constructor of the RadUploadSelectedFile class (which internally calls its own Update() method).  Because the constructor is encountering the exception, the RadUploadSelectedFile is never instantiated, so it never gets added to the Items collection of the RadUpload, so I can never inspect the error message.

      The full stack looks like this:

      > mscorlib.dll!System.Security.CodeAccessSecurityEngine.ThrowSecurityException(System.Reflection.RuntimeAssembly asm, System.Security.PermissionSet granted, System.Security.PermissionSet refused, System.RuntimeMethodHandleInternal rmh, System.Security.Permissions.SecurityAction action, object demand, System.Security.IPermission permThatFailed) + 0x77 bytes
        mscorlib.dll!System.Security.CodeAccessSecurityEngine.ThrowSecurityException(object assemblyOrString, System.Security.PermissionSet granted, System.Security.PermissionSet refused, System.RuntimeMethodHandleInternal rmh, System.Security.Permissions.SecurityAction action, object demand, System.Security.IPermission permThatFailed) + 0x65 bytes
        mscorlib.dll!System.Security.CodeAccessSecurityEngine.CheckHelper(System.Security.PermissionSet grantedSet, System.Security.PermissionSet refusedSet, System.Security.CodeAccessPermission demand, System.Security.PermissionToken permToken, System.RuntimeMethodHandleInternal rmh, object assemblyOrString, System.Security.Permissions.SecurityAction action, bool throwException) + 0x144 bytes
        mscorlib.dll!System.Security.CodeAccessSecurityEngine.CheckHelper(object notUsed, System.Security.PermissionSet grantedSet, System.Security.PermissionSet refusedSet, System.Security.CodeAccessPermission demand, System.Security.PermissionToken permToken, System.RuntimeMethodHandleInternal rmh, System.Reflection.RuntimeAssembly asm, System.Security.Permissions.SecurityAction action) + 0x30 bytes
        [Managed to Native Transition]
        mscorlib.dll!System.Security.CodeAccessSecurityEngine.Check(System.Security.CodeAccessPermission cap, ref System.Threading.StackCrawlMark stackMark) + 0x20 bytes
        mscorlib.dll!System.Security.CodeAccessPermission.Demand() + 0x2f bytes
        mscorlib.dll!System.IO.FileStream.Init(string path, System.IO.FileMode mode, System.IO.FileAccess access, int rights, bool useRights, System.IO.FileShare share, int bufferSize, System.IO.FileOptions options, Microsoft.Win32.Win32Native.SECURITY_ATTRIBUTES secAttrs, string msgPath, bool bFromProxy, bool useLongPath, bool checkHost) + 0x388 bytes
        mscorlib.dll!System.IO.FileStream.FileStream(string path, System.IO.FileMode mode, System.IO.FileAccess access, System.IO.FileShare share, int bufferSize, System.IO.FileOptions options, string msgPath, bool bFromProxy) + 0x3d bytes
        mscorlib.dll!System.IO.FileStream.FileStream(string path, System.IO.FileMode mode, System.IO.FileAccess access, System.IO.FileShare share, int bufferSize, bool useAsync) + 0x3a bytes
        mscorlib.dll!System.IO.FileInfo.OpenRead() + 0x5f bytes
        Telerik.Windows.Controls.Input!Telerik.Windows.Controls.RadUploadSelectedFile.Update() + 0x52 bytes
        Telerik.Windows.Controls.Input!Telerik.Windows.Controls.RadUploadSelectedFile.RadUploadSelectedFile(System.IO.FileInfo file) + 0x60 bytes
        Telerik.Windows.Controls.Input!Telerik.Windows.Controls.RadUpload.ShowFileDialog() + 0x1da bytes
        Telerik.Windows.Controls.Input!Telerik.Windows.Controls.RadUpload.BrowseButton_Click(object sender, System.EventArgs e) + 0x2a bytes
        System.Windows.dll!System.Windows.Controls.Primitives.ButtonBase.OnClick() + 0x3d bytes

    As you can see, this originates from the click handler of the button in RadUpload.  I can't tell for sure, but it appears the exception goes to limbo, but for sure, the item never gets added.

  18. Brian Sayatovic
    Brian Sayatovic avatar
    91 posts
    Member since:
    Nov 2009

    Posted 16 Sep 2014 in reply to Brian Sayatovic Link to this post

    Nevermind.  I see it now in 2014.x.x.x DLLs
  19. Kiril Vandov
    Admin
    Kiril Vandov avatar
    324 posts

    Posted 17 Sep 2014 Link to this post

    Hello Brian,

    I am glad that you have found a solution that works for you. However I would like to ask you to open a new ticket if you have further questions, doing so we can easily track your communication history and better handle your concrete scenario. You can provide a link to the topic you used to take code-snippets or project if you used any.

    Kind regards,
    Kiril Vandov
    Telerik
     
    Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
     
Back to Top
DevCraft banner