0

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

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 + "%)";
    }

这里wcUploaderWebClient类的对象。

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

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

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

4

0 回答 0