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

RadUpload replacement of "File Being Used" error message

17 Answers 183 Views
Upload
This is a migrated thread and some comments may be shown as answers.
mathieu
Top achievements
Rank 1
mathieu asked on 11 Aug 2011, 03:14 PM
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?

17 Answers, 1 is accepted

Sort by
0
Petar Mladenov
Telerik team
answered on 16 Aug 2011, 04:32 PM
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 >>

0
mathieu
Top achievements
Rank 1
answered on 16 Aug 2011, 04:59 PM
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!
0
Accepted
Petar Mladenov
Telerik team
answered on 19 Aug 2011, 02:10 PM
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 >>

0
mathieu
Top achievements
Rank 1
answered on 24 Aug 2011, 10:28 PM
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
0
Petar Mladenov
Telerik team
answered on 30 Aug 2011, 11:26 AM
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 >>

0
mathieu
Top achievements
Rank 1
answered on 31 Aug 2011, 09:25 PM
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!
0
Petar Mladenov
Telerik team
answered on 02 Sep 2011, 03:50 PM
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 >>

0
Jonx
Top achievements
Rank 2
answered on 03 Apr 2012, 03:46 AM
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...
0
Tina Stancheva
Telerik team
answered on 04 Apr 2012, 12:05 PM
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 >>
0
Harpreet
Top achievements
Rank 1
answered on 07 May 2012, 05:40 AM
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.
0
Tina Stancheva
Telerik team
answered on 07 May 2012, 11:58 AM
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 >>

0
Hans
Top achievements
Rank 1
answered on 20 Mar 2013, 08:57 AM
public void OnFilesSelected(object sender, FilesSelectedEventArgs e)
{
    if (e.SelectedFiles[0].Size == 0)
    {
        // file is in use
    }
}
0
Rakesh
Top achievements
Rank 1
answered on 20 Sep 2013, 12:40 PM
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;
        }

0
Kiril Vandov
Telerik team
answered on 25 Sep 2013, 09:34 AM
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 >>
0
Brian Sayatovic
Top achievements
Rank 1
answered on 17 Sep 2014, 01:59 AM
  • 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.

0
Brian Sayatovic
Top achievements
Rank 1
answered on 17 Sep 2014, 02:02 AM
Nevermind.  I see it now in 2014.x.x.x DLLs
0
Kiril Vandov
Telerik team
answered on 17 Sep 2014, 12:53 PM
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.
 
Tags
Upload
Asked by
mathieu
Top achievements
Rank 1
Answers by
Petar Mladenov
Telerik team
mathieu
Top achievements
Rank 1
Jonx
Top achievements
Rank 2
Tina Stancheva
Telerik team
Harpreet
Top achievements
Rank 1
Hans
Top achievements
Rank 1
Rakesh
Top achievements
Rank 1
Kiril Vandov
Telerik team
Brian Sayatovic
Top achievements
Rank 1
Share this question
or