2

我正在開發Silverlight項目。 當我將jpg圖片保存到內存流中以將它保存到Context.InputStream中時,它工作正常。 我打電話給一個aspx頁面,他將上傳到服務器。當我使用Silverlight 4.0的WebClient調用aspx時,無法執行Response.Redirect頁面

但是,當上傳完成或失敗時,我無法執行「response.redirect」或「server.transfer」。 是因爲我使用WebClient從Silverlight調用我的aspx頁面?

請在下面找到在Silverlight代碼:

private void UploadFile(string fileName, Stream data){ 

UriBuilder ub = new UriBuilder("http://localhost:52544/WebForm1.aspx"); 

//add a parameter filename into the queryString 

ub.Query = string.Format("filename={0}", fileName); 

WebClient c = new WebClient(); 

c.OpenWriteCompleted += (sender, e) => 
    { 

     PushData(data, e.Result); 
     e.Result.Close(); 
     data.Close(); 
    }; 
c.OpenWriteAsync(ub.Uri); 
} 

在aspx頁面,我有這樣的代碼

protected void Page_Load(object sender, EventArgs e) 
     { 
      try 
      {    
       // get the filename 

       string filename = Request.QueryString["filename"].ToString(); 

       // create a file on the server dir 

       using (FileStream fs = File.Create(Server.MapPath("~/AppData/" + filename))) 
       { 
        SaveFile(Request.InputStream, fs); 
       } 

        Response.Redirect("uploadOk.aspx", true); 
       } 
       catch (Exception ex) 
       { 

       } 


     } 


     private bool SaveFile(Stream stream, FileStream fs) 
     { 
      bool isSaved = true; 
      byte[] buffer = new byte[4096]; 
      int bytesRead; 
      try 
      { 
       // copy the stream into the file 

       while ((bytesRead = stream.Read(buffer, 0, buffer.Length)) != 0) 
       { 
        fs.Write(buffer, 0, bytesRead); 
       } 
       isSaved = true; 
      } 
      catch (Exception e) 
      { 
       isSaved = false; 
      } 
      return isSaved; 
     } 
    } 

我試圖response.redirection( 「uploadOk.aspx」,FALSE)它也不起作用。 我得到了以下異常「[System.Threading.ThreadAbortException] = {無法因爲代碼優化或本機框上調用堆棧的頂部,以評估 表達。}」

你有一個想法如何我可以使用WebClient進行重定向?

預先感謝您

回答

0

我相信你的問題是,上傳失敗,而是因爲它是在不同的線程,SL不顯示正確的錯誤。嘗試添加代碼以記錄錯誤發生的時間並查看錯誤是什麼。您也可以在服務器上執行此操作。

我懷疑問題是頭已經寫好,所以它不能重定向。

試試這個,因爲我認爲這個問題是100-繼續:

c.Headers.Add(HttpRequestHeader.KeepAlive, "false"); 
c.Headers.Add(HttpRequestHeader.Expect, ""); 
+0

謝謝您的回答,但我沒有在Silverlight的方法c.Headers.Add。我只有AllKeys,AsQueryable,Cast,...但不添加method.I認爲你是對的問題是因爲頭已經寫好。你能幫我解決這個問題嗎? Thx提前。 – Shimon 2010-11-10 15:10:36

相關問題