1)在initParams配置参数“UploadHandlerName=Ajax/HttpUploadHandler.ashx”。
2)在Silverlight程序中的添加文件的方法中编写请求代码。这块值得注意的,一是路径,二是获取到数据后,要使用this.Dispatcher.BeginInvoke来更新界面(否则会出异常)。具体代码如下:
public class RequestStates
{
/// <summary>
/// 当前请求
/// </summary>
public HttpWebRequest CurrentWebRequest { get; set; }
/// <summary>
/// 当前用户文件
/// </summary>
public UserFile CurrentUserFile { get; set; }
}
private void AddFile(FileInfo file)
{
string fileName = file.Name;
var userFile = new UserFile { FileName = file.Name, FileStream = file.OpenRead() };if (!string.IsNullOrEmpty(Configuration.Instance.CheckExtensionsHandlerName))
{
//获取处理的Handler类的路径
UriBuilder httpHandlerUrlBuilder = new UriBuilder(new CustomUri(Configuration.Instance.CheckExtensionsHandlerName));
//设置请求的参数
httpHandlerUrlBuilder.Query = string.Format("{2}file={0}&Extension={1}", HttpUtility.UrlEncode(userFile.FileName), HttpUtility.UrlEncode(Path.GetExtension(userFile.FileName)), string.IsNullOrEmpty(httpHandlerUrlBuilder.Query) ? string.Empty : httpHandlerUrlBuilder.Query.Remove(0, 1) + "&");
//创建请求
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(httpHandlerUrlBuilder.Uri);
request.Method = "POST";
//设置自定义信息,用于异步请求
var requestStates = new RequestStates()
{
CurrentUserFile = userFile,
CurrentWebRequest = request
};
//开始异步请求
request.BeginGetRequestStream(new AsyncCallback(RequestReady), requestStates);
}
else
{
_files.Add(userFile);
}
}
private void RequestReady(IAsyncResult asynchronousResult)
{
var requestStates = (RequestStates)asynchronousResult.AsyncState;
HttpWebRequest webRequest = requestStates.CurrentWebRequest;
Stream requestStream = webRequest.EndGetRequestStream(asynchronousResult);
requestStream.Close();
webRequest.BeginGetResponse(new AsyncCallback(ResponseReady), requestStates);
}
void ResponseReady(IAsyncResult asyncResult)
{
var requestStates = (RequestStates)asyncResult.AsyncState;
HttpWebRequest webRequest = requestStates.CurrentWebRequest;
var userFile = requestStates.CurrentUserFile;
var webResponse = (HttpWebResponse)webRequest.EndGetResponse(asyncResult);
var reader = new StreamReader(webResponse.GetResponseStream());
//获取Httphandler类返回的数据
string responsestring = reader.ReadToEnd();
reader.Close();
decimal maxSize = Convert.ToDecimal(responsestring);
//Check for the file size limit (configurable)
if (maxSize == 0 || (userFile.FileStream.Length / 1024) <= maxSize)
{
this.Dispatcher.BeginInvoke(
() => _files.Add(userFile)
);}
else if (maxSize < 0)
{
this.Dispatcher.BeginInvoke(
delegate()
{
var messageWindow = new MessageChildWindow
{
Message =
string.Format("不允许上传{0}类文件!",
Path.GetExtension(userFile.FileName))
};
messageWindow.Show();if (MaximumFileSizeReached != null)
MaximumFileSizeReached(this, null);
});
}
else
{
this.Dispatcher.BeginInvoke(
() =>
{
var messageWindow = new MessageChildWindow
{
Message =
string.Format("{0}{1}{2}KB.",
Path.GetExtension(userFile.FileName),
UserMessages.MaxFileSize, (maxSize).ToString())
};
messageWindow.Show();if (MaximumFileSizeReached != null)
MaximumFileSizeReached(this, null);
});
}
}
效果图片