Silverlight收到处理程序的响应

如何解决Silverlight收到处理程序的响应

| 我有一个坐在网页上的Silverlight应用程序。它允许用户将多个文档上载到服务器。当我开始将文档上传到服务器时,我将使用POST请求调用处理程序,如果文件太大,则使用以下代码将其分块发送:
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,buffer.Length)) != 0 && tempTotal + bytesRead < ChunkSize)
    {
        requestStream.Write(buffer,bytesRead);
        requestStream.Flush();
        Bytesuploaded += bytesRead;
        tempTotal += bytesRead;

        if (UploadProgressChanged != null)
        {
            var percent = (int) ((Bytesuploaded/(double) FileLength)*100);
            var args = new UploadProgressChangedEventArgs(percent,bytesRead,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);
}
我希望处理程序返回我的文档名称(我为处理程序中的每个文档生成一个新的文件名,因为我们有正在使用的文档命名标准)。所以我这样编码了我的AsyncCallback,我期望AsyncState将返回我以为我在处理程序中设置的标头:
private void ReadCallback(IAsyncResult asynchronousResult)
{
    var documentName = ((System.Net.browserHttpWebRequest)asynchronousResult.AsyncState).Headers[\"documentname\"].ToString();

    if (Bytesuploaded < FileLength)
        UploadFileEx();
    else
    {
        Status = FileUploadStatus.Complete;
    }
}
这是我的处理程序:
public void ProcessRequest(HttpContext context) {
      Context = context;
      UploadFile();
      //context.Request.Headers.Add(\"documentname\",\"testfilename.pdf\");
      context.Response.Headers.Add(\"documentname\",\"testfilename.pdf\"); 
}
这不是我想的那样。我究竟做错了什么?我在处理程序中设置标题错误吗?我的管理员可以通过另一种方式与Silverlight客户端应用进行对话吗? 任何帮助,将不胜感激。提前致谢!     

解决方法

如果您的处理程序将文档名称作为“响应”正文的一部分而不是作为“标头”发送回去,则可能会更幸运。 试试下面的代码,看看会得到什么:
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;
    }
}
并在ProcessRequest中:
Context.Response.Write(“Document Name”);
    

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?