Asp.net Silverlight从处理程序接收响应
我有一个Silverlight应用程序,它位于网页上。它允许用户将多个文档上载到服务器。当我开始将文档上传到服务器时,我会通过POST请求调用处理程序,如果文件太大,我会将其分块发送,代码如下:Asp.net Silverlight从处理程序接收响应,asp.net,silverlight,silverlight-4.0,handler,Asp.net,Silverlight,Silverlight 4.0,Handler,我有一个Silverlight应用程序,它位于网页上。它允许用户将多个文档上载到服务器。当我开始将文档上传到服务器时,我会通过POST请求调用处理程序,如果文件太大,我会将其分块发送,代码如下: private void UploadFileEx() { Status = FileUploadStatus.Uploading; var temp = FileLength - BytesUploaded; var ub = new UriBuilder(_urlServe
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);
}
这不像我想的那样有效。我做错了什么?我在处理程序中设置的标题是否错误?我的处理程序是否有其他方式与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;
}
}
如果处理程序将文档名作为响应体的一部分而不是作为标题发回,您可能会更幸运 尝试以下代码,看看您得到了什么:
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;
}
}
谢谢,成功了!把我从又一天的头撞在桌子上的事中救了出来。谢谢,这奏效了!把我从又一天的头撞到桌子上救了出来。
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;
}
}
Context.Response.Write(“Document Name”);