当我使用 WebClient 和 Silverlight 4.0 调用 aspx 页面时,无法执行 Response.Redirect

发布于 2024-10-02 00:43:09 字数 2197 浏览 0 评论 0原文

我正在开发一个 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 进行重定向吗?

先感谢您

I am working on a Silverlight Project.
When i saved a jpg picture into a memorystream to save it
into the Context.InputStream, it is working fine.
I am calling an aspx page who thread the upload into the server.

But i can not do a "response.redirect" or "server.transfer" when the upload is done or failed .
Is it because I call my aspx page from Silverlight using WebClient ?

Please find the code in Silverlight below :

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

On the aspx page, I have this code

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

I have tried response.redirection("uploadOk.aspx",false) too and it is not working.
I got the following exception “[System.Threading.ThreadAbortException] = {Unable to evaluate
expression because the code is optimized or a native frame is on top of the call stack.}”

Do you have an idea how i can do a redirection using a WebClient ?

Thank you in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

极度宠爱 2024-10-09 00:43:09

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

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

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

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

I believe your problem is that the upload fails but because it is on a different thread, SL does not show the correct error. Try adding code to log the error when it happens and see what the error is. You can do this on the server as well.

I suspect the problem is header is already written so it cannot redirect.

Try this since I think the problem is 100-continue:

 c.Headers.Add( HttpRequestHeader.KeepAlive, "false");
 c.Headers.Add(HttpRequestHeader.Expect, "");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文