This project has moved. For the latest updates, please go here.

Show errors from Handler in UI

Sep 6, 2011 at 8:28 AM

Hi,

 

Thanks for such a great component.

I used it in my SharePoint site and it works fine. One doubt.

Could you please tell me how to show that the upload is failed in the component itself if there is an error in handler while uploading?

Is this possible without changing the SL component's source code?

 

Thanks again :)

Coordinator
Sep 6, 2011 at 11:01 AM

Hi,

I'm not sure what you mean. The uploader already shows an error for a file that couldn't be uploaded (if an exception occured in the http handler).

Michiel

Sep 7, 2011 at 4:21 AM

Hi,

I want to find a way to show the upload failed if any exception occurs  at handler. This does not happen by itself.

I read in some other discussion that the control shows upload failed when a not OK status comes from the HTTP Handler.

So I coded the handler in such a way that it sets the response code as 500 when any exception occurs at the handler.

This works fine :)

 

Do you think I can change the error message in the component(Upload Failed) without changing the Silver light component code?

Or can I get what is the error returned from the server for each file uploaded / failed to upload in client side(May be with JavaScript?

 

Thanks for replying :)

John

Coordinator
Sep 7, 2011 at 6:02 AM

Hi,

The error message shown in the Silverlight component is embedden in a resource file. So you'll have to edit and recompile the Silverlight code, but it's not a big change.

If you want to show a different message for each error, you will need to change the code.

Michiel

Aug 8, 2012 at 12:53 PM
Edited Aug 8, 2012 at 12:55 PM

Disclaimer, I haven't tested this too much, I only just wrote it so...

It seems like this would be a common request, and it's something that I needed to do for my project. I've never done Silverlight development before, but I found it incredibly easy to understand Michiel's well-written code. 

The way I've done it, you simply need to modify the HttpUploadHandler to return a response that starts with the word "error" (or "ERROR", or "Error") after any block is received, and the file upload will immediately stop processing and display the response from the http handler. For example, suppose you don't want to accept files of type .xls, and for whatever reason it's better for you to test this rule on the server. You could change the start of the HttpHandler to look like this:

 

                string tempPath = GetTempFilePath(tempFileName);
                string targetPath = GetTargetFilePath(_fileName);

                string ext = System.IO.Path.GetExtension(_fileName).ToLower();
                if (ext == ".xls")
                {
                    _httpContext.Response.Write("Error: Files of type 'xls' are invalid");
                    return;
                }
                

 

For a more realistic example, you could change the Catch(Exception e) block of the HttpHandler and it will then display all exceptions in the Silverlight control:

            catch (Exception e)
            {
                Debug.WriteLine(e.ToString());
                _httpContext.Response.Write("Error: " + e.Message);
                // throw;    <- don't need this anymore
		return;
            }

For this to work on the Silverlight side, all you need to do is modify the HttpFileUploader.cs file, and change the function:

private void ReadHttpResponseCallback(IAsyncResult asynchronousResult)

Change this:

 

StreamReader reader = new StreamReader(webResponse.GetResponseStream());
string responsestring = reader.ReadToEnd();
reader.Close();

 

To this:

 

StreamReader reader = new StreamReader(webResponse.GetResponseStream());
string responsestring = reader.ReadToEnd();
reader.Close();
// If there was an error with the file, close the file reader and show the error
if (!string.IsNullOrEmpty(responsestring) && responsestring.StartsWith("error", StringComparison.OrdinalIgnoreCase))
{
  _file.FileStream.Close();
  _file.FileStream.Dispose();
  _file.ErrorMessage = responsestring;
  _uiDispatcher.BeginInvoke(delegate() {
	if (_file.State != Enums.FileStates.Deleted)
	_file.State = Enums.FileStates.Error;
   });
  return;
}

Hope that works for you!

Cheers,
Chris

Coordinator
Aug 8, 2012 at 1:00 PM

Thanks! That should work indeed. Only a small code change :)

Michiel