2011-04-16 57 views
1

我有一個位於網頁上的Silverlight應用程序。它允許用戶將多個文檔上傳到服務器。當我開始上傳一個文件到服務器,我會做出一個處理程序的調用一個POST請求,我把它塊,如果該文件是太大用下面的代碼:Silverlight從處理程序接收響應

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); 
} 

我想我的處理程序返回我的文檔名稱(我爲處理程序中的每個文檔生成一個新文件名,因爲我們有一個正在使用的文檔命名標準)。所以,我編寫我的AsyncCallback這樣,我期待的AsyncState會回到我以爲我settting在處理器頭:

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客戶端應用程序交談?

任何幫助,將不勝感激。提前致謝!

回答

1

如果您的處理程序將文檔名稱作爲Response主體的一部分而不是Header發回,您可能會有更多的運氣。

試試這個代碼,看看你會得到什麼:

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」); 
+0

感謝,這工作!從另一天把我的頭靠在桌子上,挽救了我。 – Mark 2011-04-18 14:47:58

相關問題