Silverlight 4 RC 文件上传,上传进度为 : how to?

标签 silverlight file upload silverlight-4.0

据说 Silverlight 4 RC 的新功能之一是它现在支持上传进度。

我假设这意味着可以在没有“分块”的情况下制作上传文件进度条,但我不知道该怎么做,那么我们该怎么做呢?源代码示例会很棒。

谢谢!

最佳答案

好吧,经过大量的尝试,我明白了:

   private void UploadFile(string url, CustomPostDataInfo pdi)
    {

        // Use the client http stack!

        //HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
        HttpWebRequest webRequest = (HttpWebRequest)WebRequestCreator.ClientHttp.Create(new Uri(url));

        webRequest.AllowWriteStreamBuffering = false; // <-- this enables upload progress reporting!

        webRequest.Method = "POST";
        webRequest.ContentType = "multipart/form-data; boundary=" + pdi.Boundary; // Boundary only needed for multipart form ada

        // Calculate our response length
        webRequest.ContentLength = pdi.FormDataHeader.Length + pdi.File2Upload.Length + pdi.FormDataFooter.Length; // Calculate the length of your entire message here, required

        pdi.request = webRequest;

        webRequest.BeginGetRequestStream(new AsyncCallback(WriteToStreamCallback), pdi);
    }

    private void WriteToStreamCallback(IAsyncResult asynchronousResult)
    {
        CustomPostDataInfo pdi = (AmazonS3PostDataInfo)asynchronousResult.AsyncState;
        HttpWebRequest webRequest = pdi.request;
        Stream requestStream = webRequest.EndGetRequestStream(asynchronousResult);
        UTF8Encoding encoding = new UTF8Encoding();

        UpdateShowProgress(false, "Uploading file..."); // Your custom update event - make sure to use a Dispatcher to update on the UI thread as this is running on a separate thread.

        // Write any pre file data if needed
        // ...

        // Write our file data
        {
            // Read chunks of this file
            byte[] buffer = new Byte[1024 * 32];
            Stream fileStream = pdi.File2Upload.OpenRead();
            int bytesRead = 0;
            while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
            {
                requestStream.Write(buffer, 0, bytesRead);
                requestStream.Flush(); // Will block until data is sent

                bytesUploaded += bytesRead;

                //Show the progress change
                UpdateShowProgress(false, "Uploading file...");
            }
        }

        // Write any post file data
        // ...

        UpdateShowProgress(false, "Uploaded, waiting for response...");

        requestStream.Close();

        // Get the response from the HttpHandler
        webRequest.BeginGetResponse(new AsyncCallback(ReadHttpResponseCallback), webRequest);

    }

    private void ReadHttpResponseCallback(IAsyncResult asynchronousResult)
    {
        HttpWebRequest webRequest = (HttpWebRequest)asynchronousResult.AsyncState;
        HttpWebResponse webResponse = (HttpWebResponse)webRequest.EndGetResponse(asynchronousResult);
        StreamReader reader = new StreamReader(webResponse.GetResponseStream());

        string response = reader.ReadToEnd(); // Get the result

        reader.Close();

        UpdateShowProgress(true, response);
    }

关于Silverlight 4 RC 文件上传,上传进度为 : how to?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2529558/

相关文章:

c# - 在 Silverlight 4 中使用 ComAutomationFactory 遍历 Word 文档字段

c# - Silverlight 4 中的排序集合类?

Javascript For 循环减慢进度事件监听器触发

silverlight - 如何为 Silverlight 编译 C++/CLI 代码?

c++ - 从文件 C++ 中替换整行

linux - 如何在连接之前插入新行?

c# - 在 C# 中,如何从域外访问域上的文件共享?

image - Magento 产品上传图片未出现

php - WordPress 文件上传没有图像

asp.net - Url 编码 # (%23) 在 ASP.NET 应用程序中导致 404