开发者

Silverlight receive a response from a handler

开发者 https://www.devze.com 2023-02-26 19:53 出处:网络
I have a Silverlight Application that sits on a web page. It allows a user to upload multiple documents to a server. When I begin uploading a document to the server I will make a call to a handler wit

I have a Silverlight Application that sits on a web page. It allows a user to upload multiple documents to a server. When I begin uploading a document to the server I will make a call to a handler with a POST request and I send it in chunks if the file is too big with the following code:

private void UploadFileEx()
{
    Status = FileUploadStatus.Uploading;
    var temp = FileLength - BytesUploaded;

    var ub = new UriBuilder(_urlServer);
    var complete = temp <= ChunkSize;

    ub.Query = string.Format("{3}locationCode={4}&filename={0}&StartByte={1}&Complete={2}", RockDocumentName, BytesUploaded, complete,
                                string.IsNullOrEmpty(ub.Query) ? string.Empty : string.Format("{0}&", ub.Query.Remove(0, 1)), _locationCode);

    var webrequest = (HttpWebRequest) WebRequest.Create(ub.Uri);
    webrequest.Method = "POST";

    // Begins an asynchronous request for a Stream object to use to write data.
    // The asynchronous callback method uses the EndGetRequestStream method to return the actual stream.
    // This means
    webrequest.BeginGetRequestStream(WriteCallback, webrequest);
}

private void WriteCallback(IAsyncResult asynchronousResult)
{
    var webrequest = (HttpWebRequest) asynchronousResult.AsyncState;
    // End the operation.
    // Here we obtain the stream to write the request
    var requestStream = webrequest.EndGetRequestStream(asynchronousResult);

    var buffer = new Byte[4096];
    int bytesRead;
    var tempTotal = 0;

    Stream fileStream = File.OpenRead();

    fileStream.Position = BytesUploaded;
    while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0 && tempTotal + bytesRead < ChunkSize)
    {
        requestStream.Write(buffer, 0, bytesRead);
        requestStream.Flush();
        BytesUploaded += bytesRead;
        tempTotal += bytesRead;

        if (UploadProgressChanged != null)
        {
            var percent = (int) ((BytesUploaded/(double) FileLength)*100);
            var args = new UploadProgressChangedEventArgs(percent, bytesRead, BytesUploaded, FileLength,
                                                            RockDocumentName);
            Dispatcher.BeginInvoke(() => UploadProgressChanged(this, args));
        }
    }

    fileStream.Close();
    requestStream.Close();

    // Whenever the operation is ready call the ReadCallback method (when this method
    // is called we know that the chunk of file has arrived successfully to the server,
    // it's time to process the next chunk).
    webrequest.BeginGetResponse(ReadCallback, webrequest);
}

I would like my handler to return back my documents name (I generate a new file name for each document in the handler because we have a document naming standard that's being used). So i coded my AsyncCallback like this which I was expecting the AsyncState would return the header I thought I was settting in the handler:

private void ReadCallback(IAsyncResult asynchronousResult)
{
    var documentName = ((System.Net.BrowserHttpWebRequest)asynchronousResult.AsyncState).Headers["documentname"].ToString();

    if (BytesUploaded < FileLength)
        UploadFileEx();
    else
    {
        Status = FileUploadStatus.Complete;
    }
}

Here is my Handler:

public void ProcessRequest(HttpContext context) {
      Context = context;
      UploadFile();
      //context.Request.Headers.Add("documentname", "testfilename.pdf");
      context.Response.Headers.Add("documentname", "testfilename.pdf"); 
}

This isn't working as I had thought. What am I doing wrong? Am I setting the header wrong in my handler? Is there another way for my handler to speak 开发者_如何学运维to the Silverlight client app?

Any help would be appreciated. Thanks in advance!


You may have more luck if your handler sends back the document name as part of the Response body rather than as a Header.

Try this code and see what you get:

private void ReadCallback(IAsyncResult asynchronousResult)
{
    var webRequest = (HttpWebRequest) asynchronousResult.AsyncState;
    var webResponse = (HttpWebResponse) webRequest.EndGetResponse(asynchronousResult);
    var reader = new StreamReader(webResponse.GetResponseStream());
    var documentName = reader.ReadToEnd();
    reader.Close();


    if (BytesUploaded < FileLength)
        UploadFileEx();
    else
    {
        DocumentName = documentName;
        Status = FileUploadStatus.Complete;
    }
}

And in ProcessRequest:

Context.Response.Write(“Document Name”);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号