Ad

Why Are File Uploads Via Silverlight 2 Limited To Only .TXT Files?

- 1 answer

I have a Silverlight 2 app that sends a byte array to a Silverlight-enabled WCF service. However, (unless I try to upload a .txt file) the service's SaveFile() method is never reached and I get an error: "The remote server returned an error: NotFound"

Am I missing something really obvious? Why can't I upload .doc files? Why only .txt?

UPDATE: I've tried attaching debugger for CLR exceptions, but that didn't help. I am still not able to see any errors besides the 404. Here's my code:

in Page.xaml.cs:

OpenFileDialog dlg = new OpenFileDialog();

if (dlg.ShowDialog().Value)
{
  byte[] fileContent = new byte[dlg.File.Length];

  using (FileStream fs = dlg.File.OpenRead())
  {
    fs.Read(fileContent, 0, Convert.ToInt32(fs.Length));
    fs.Close();
  }

  Service1.Service1Client srv = new SL1.Service1.Service1Client();

  srv.SaveFileCompleted += (sender1, e1) =>
  {
    foo.Text = "Uploaded!";
  };

  srv.SaveFileAsync(dlg.File.Name, fileContent);
}

in Service1.svc.cs:

[OperationContract]
public string SaveFile(string fileName, byte[] fileContent)
{
  string ret = String.Empty;

  try
  {
    string target = @"c:\debug123\" + fileName;

    if (File.Exists(target))
    {
      File.Delete(target);
    }

    File.WriteAllBytes(target, fileContent);

    ret = "OK";
  }
  catch (Exception ex)
  {
    ret = ex.ToString();
  }

  return ret;
}

Anyone see anything wrong with this?

Ad

Answer

FYI, I figured out my problem and this article solved it. "By default the largest message that can be sent to a service from the client is 8124 bytes." So I had to increase the limit via the bindings config settings.

But now my main issue is how to get an appropriate error message, instead of the 404. I'll have to research that some more, but hope someone else can come up with a good answer (unfortunately breaking on CLR exceptions didn't help me).

UPDATE: Upon further reading of the same article, I discovered that the WCF Service Configuration Utility that is included in the Windows SDK can help determine the underlying problem.

Ad
source: stackoverflow.com
Ad