Chrome中的Silverlight WebClient文件上载问题
在使用Chrome时,我对Silverlight应用程序中的WebClient类有一个奇怪的问题。我可以用IE成功上传文件,但当我使用Chrome时它失败了。当我深入研究它时,我看到WebClient(或Chrome,我不知道谁对此负责)在使用Chrome时向请求添加了“Content-Type:application/x-www-form-urlencoded”标题,但它没有添加到IE上。 由于该标头,服务器端应用程序将抛出以下表达式: System.InvalidOperationException:由于对象的当前状态,操作无效 [InvalidOperationException:由于当前错误,操作无效 对象的状态。]Chrome中的Silverlight WebClient文件上载问题,silverlight,http,webclient,Silverlight,Http,Webclient,在使用Chrome时,我对Silverlight应用程序中的WebClient类有一个奇怪的问题。我可以用IE成功上传文件,但当我使用Chrome时它失败了。当我深入研究它时,我看到WebClient(或Chrome,我不知道谁对此负责)在使用Chrome时向请求添加了“Content-Type:application/x-www-form-urlencoded”标题,但它没有添加到IE上。 由于该标头,服务器端应用程序将抛出以下表达式: System.InvalidOperationExcep
System.Web.HttpValueCollection.ThrowifMaxHttpCollectionKeysExcepended() +2420886 System.Web.HttpValueCollection.FillFromEncodedBytes(字节[]字节,编码编码)+58
System.Web.HttpRequest.FillInFormCollection()+159 [HttpException(0x80004005):URL编码的表单数据无效。] System.Web.HttpRequest.FillInFormCollection()+217
System.Web.HttpRequest.get_Form()+104 以下是我在Silverlight中使用的代码
![private void UploadFile(string fileName, Stream data)
{
var uri = this.ResolveUri(apiMethod, arguments);
ub.Query = string.Format("filename={0}", fileName);
WebClient c = new WebClient();
c.OpenWriteCompleted += (sender, e) =>
{
PushData(data, e.Result);
e.Result.Close();
data.Close();
};
c.OpenWriteAsync(ub.Uri);
}
private void PushData(Stream input, Stream output)
{
byte\[\] buffer = new byte\[4096\];
int bytesRead;
while ((bytesRead = input.Read(buffer, 0, buffer.Length)) != 0)
{
output.Write(buffer, 0, bytesRead);
}
}
使用Chrome的请求失败: 使用IE请求成功:
我的上传功能与上面的示例几乎相同。我在fiddler中看到了与您相同的请求,但它似乎在两种浏览器中都能正常工作(没有错误,也没有上传文件)。你在服务器端做什么?也许这就是问题所在 此外,您可能还可以尝试更改客户端WebClient请求的内容类型
WebClient c = new WebClient();
c.Headers.Add("Content-Type","whatever/you-want");
感谢Doug,这解决了问题(内容类型:“text/html”),但它应该由框架设置,不是吗?@Yucel,如果框架指的是SL,那么许多默认标题设置似乎都留给主机浏览器来控制。有关更多信息,请参阅。其中许多受限头是由承载Silverlight应用程序的web浏览器设置的。