Secure Silverlight File Upload Best Practices for Developers
Troubleshooting Common Silverlight File Upload Errors
1. File selection fails or dialog doesn’t open
- Cause: Security restrictions require file selection to be initiated by a user action (e.g., button click).
- Fixes: Ensure the OpenFileDialog is created and shown inside the click event handler. Avoid opening it from background threads or non-user-initiated events.
2. File size limits or uploads silently fail
- Cause: Server-side size restrictions or proxy/Web server limits (IIS maxRequestLength, requestFiltering maxAllowedContentLength). Silverlight client may not report detailed errors.
- Fixes: Increase server limits (IIS web.config), implement chunked uploads on client and server, and add server-side logging to capture rejected requests.
3. Timeout or connection errors during upload
- Cause: Large files, slow networks, or server timeouts.
- Fixes: Increase server timeout settings, implement resumable/chunked uploads, show progress and allow retry, and use a background keep-alive if needed.
4. Cross-domain or CORS-like issues
- Cause: Silverlight enforces cross-domain policy; server lacks clientaccesspolicy.xml or crossdomain.xml.
- Fixes: Place a properly configured clientaccesspolicy.xml at the root of the uploading server (and crossdomain.xml if Flash compatibility needed). Ensure XML allows the Silverlight application’s domain and the required HTTP methods.
5. Authentication or authorization failures
- Cause: Missing auth tokens/cookies on requests, or upload endpoint requires credentials not provided by Silverlight.
- Fixes: Configure Silverlight to include cookies/credentials (use BrowserHttpWebRequest or set AllowReadStreamBuffering appropriately), or implement token-based authentication passed with the upload request. Verify server logs for ⁄403 responses.
6. Incorrect MIME types or corrupted file data
- Cause: Improper stream handling, text vs. binary encoding, or incorrect Content-Type headers.
- Fixes: Read files as binary streams (use FileInfo.OpenRead), set correct Content-Type, and ensure no accidental string conversions or encoding transformations occur.
7. Progress events not firing or incorrect progress
- Cause: Using APIs that don’t surface progress or buffering behavior.
- Fixes: Use WebClient or HttpWebRequest with explicit upload progress handling; enable buffering appropriately and wire UploadProgressChanged/UploadProgress events.
8. Exceptions with specific APIs (e.g., WebClient, HttpWebRequest)
- Cause: API misuse or threading issues.
- Fixes: Follow API patterns: use BeginGetRequestStream/EndGetRequestStream for async HttpWebRequest, handle WebClient events on the UI thread, and catch/inspect WebException for response details.
9. Server-side parsing errors
- Cause: Multipart/form-data boundaries missing or incorrect when building requests manually.
- Fixes: Use tested libraries or frameworks to parse multipart requests; if manually constructing, ensure boundary strings and headers follow RFC 2388.
10. Lack of useful error messages
- Cause: Silent client failures or suppressed server responses.
- Fixes: Add detailed server logging, return JSON error objects with status codes, catch and display WebException.Response content in Silverlight for diagnostics.
Quick checklist to diagnose an upload problem
- Reproduce with a small file.
- Check browser/Network tab and server logs for HTTP status codes.
- Verify clientaccesspolicy.xml/crossdomain.xml presence.
- Confirm server size/timeout settings.
- Inspect request headers and payload (use a proxy tool).
- Test authentication flow and cookies.
- Add detailed try/catch and log exception responses.
Leave a Reply