错误:此时无法启动异步操作

时间:2013-07-22 08:56:32

标签: c# windows-phone-7 windows-phone-8 webclient

我正在尝试在网络服务器上传文件。这是我的代码

private void Submit1_ServerClick(object sender, System.EventArgs e)
    {
        if ((File1.PostedFile != null) && (File1.PostedFile.ContentLength > 0))
        {
            string fn = System.IO.Path.GetFileName(File1.PostedFile.FileName);
            string SaveLocation = Server.MapPath("Data") + "\\" + fn;

           // byte[] imageBytes = File.ReadAllBytes(SaveLocation);
            try
            {
                Uri webAddress = new Uri("http://www.mysiteurl.com/testupload.php");

                wcUploader.UploadProgressChanged += new UploadProgressChangedEventHandler(wcUploader_UploadProgressChanged);
                wcUploader.UploadFileCompleted += new UploadFileCompletedEventHandler(wcUploader_UploadFileCompleted);
                wcUploader.UploadFileAsync(webAddress,"PUT",fn);
                //Response.Write("The file has been uploaded.");
            }
            catch (Exception ex)
            {
                Response.Write("Error: " + ex.Message);
                //Note: Exception.Message returns a detailed message that describes the current exception. 
                //For security reasons, we do not recommend that you return Exception.Message to end users in 
                //production environments. It would be better to return a generic error message. 
            }
        }
        else
        {
            Response.Write("Please select a file to upload.");
        }
    }

    void wcUploader_UploadFileCompleted(object sender, UploadFileCompletedEventArgs e)
    {
        Response.Write("The file has been uploaded.");
    }

    void wcUploader_UploadProgressChanged(object sender, UploadProgressChangedEventArgs e)
    {
        TextBox1.Text = (string)e.UserState + "\n\n"
                                 + "Uploaded " + e.BytesSent + "/" + e.TotalBytesToSend
                                 + "b (" + e.ProgressPercentage + "%)";
    }

此处 wcUploader WebClient 类的对象。

当我执行此代码时,我收到此错误

  

错误:此时无法启动异步操作。异步操作只能在异步处理程序或模块中启动,或者在页面生命周期中的某些事件中启动。如果在执行页面时发生此异常,请确保将页面标记为<%@ Page Async =“true”%>。

基本上我正在尝试在Web服务器上传文件。上传时我想显示上传的进度。那么,我该如何解决这个错误呢?我的代码出了什么问题?

0 个答案:

没有答案