【问题标题】:Can not do Response.Redirect when I use WebClient with Silverlight 4.0 to call aspx page当我使用带有 Silverlight 4.0 的 WebClient 调用 aspx 页面时无法执行 Response.Redirect
【发布时间】:2010-11-10 13:34:20
【问题描述】:

我正在从事 Silverlight 项目。 当我将 jpg 图片保存到内存流中以保存时 进入 Context.InputStream,它工作正常。 我正在调用一个将上传线程发送到服务器的 aspx 页面。

但是当上传完成或失败时,我无法执行“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 进行重定向吗?

提前谢谢你

【问题讨论】:

    标签: c# silverlight-4.0 webclient response.redirect server.transfer


    【解决方案1】:

    我相信您的问题是上传失败,但因为它在不同的线程上,SL 没有显示正确的错误。尝试添加代码以在错误发生时记录错误并查看错误是什么。您也可以在服务器上执行此操作。

    我怀疑问题是标头已经写入,因此无法重定向。

    试试这个,因为我认为问题是 100-继续:

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

    【讨论】:

    • 感谢您的回答,但我没有 Silverlight 中的 c.Headers.Add 方法。我只有 AllKeys,AsQueryable,Cast,... 但没有 Add 方法。我认为你是对的问题是因为标题已经写好了。你能帮我解决这个问题吗?提前谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-11
    • 2014-01-06
    • 2014-09-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多