Silverlight requests HttpHandler to obtain data

Source: Internet
Author: User

1) configure the parameter "UploadHandlerName = Ajax/HttpUploadHandler. ashx" in initParams ".

2) write the request code in the method for adding files in the Silverlight program. It is worth noting that the first is the path, and the second is to use this. Dispatcher. BeginInvoke to update the interface after obtaining the data (otherwise an exception will occur ). The Code is as follows:

Public class RequestStates
{
/// <Summary>
/// Current request
/// </Summary>
Public HttpWebRequest CurrentWebRequest {get; set ;}
/// <Summary>
/// Current User File
/// </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 ))
{
// Obtain the path of the Handler class to be processed
UriBuilder httpHandlerUrlBuilder = new UriBuilder (new CustomUri (Configuration. Instance. CheckExtensionsHandlerName ));
// Set Request Parameters
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) + "&");
// Create a request
HttpWebRequest request = (HttpWebRequest) WebRequest. Create (httpHandlerUrlBuilder. Uri );
Request. Method = "POST ";
// Set custom information for asynchronous requests
Var requestStates = new RequestStates ()
{
CurrentUserFile = userFile,
CurrentWebRequest = request
};
// Start asynchronous 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 ());
// Obtain the data returned by the Httphandler class
String responsestring = reader. ReadToEnd ();
Reader. Close ();
Decimal maxSize = Convert. ToDecimal (responsestring );
// Check for the file size limit (retriable)
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} class files cannot be uploaded! ",
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 );
});
}
}

Slice

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.