1)在initParams配置参数“UploadHandlerName=Ajax/HttpUploadHandler.ashx”。
2)在Silverlight程序中的添加文件的方法中编写请求代码。这块值得注意的,一是路径,二是获取到数据后,要使用this.Dispatcher.BeginInvoke来更新界面(否则会出异常)。具体代码如下:
public class RequestStates
{
///
/// 当前请求
///
public HttpWebRequest CurrentWebRequest { get; set; }
///
/// 当前用户文件
///
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);
});
}
}
效果图片
创造与魔法 安卓版v1.0.0750
创造与魔法是一款开放世界手游,在游戏中玩家可探索这个奇妙的世
创造与魔法修改版 最新版v1.0.0750
创造与魔法无限点券版是款探索冒险游戏,该款游戏的操作还是蛮自
战争与文明官方版本 安卓版v1.7.16
战争与文明是一款由上海邮通科技有限公司开发的战争策略游戏,这
迷你世界0元领皮肤无限迷你币版 最新安卓版v1.43.0
迷你世界0元购买皮肤版是这款开放沙盒冒险建造游戏的特殊破解版
创造与魔法无限经验版 安卓版v1.0.0750
创造与魔法无限经验版是款可以改造环境,整个游戏的自由度还是蛮