我上传的多个文件使用的BeginGetRequestStream的HttpWebRequest但我想要更新的进度控制我已经写虽然我后的数据流。

应该如何这样做,我有试着打电话给调度.BeginInvoke(如下文),从循环内,推动数据进入流,但它锁的浏览器,直至其完成,因此它似乎是在某种形式的工作人员/ui线程的僵局。

这是一段代码的很多我在做什么:

class RequestState
{
    public HttpWebRequest request;  // holds the request
    public FileDialogFileInfo file; // store our file stream data

    public RequestState( HttpWebRequest request, FileDialogFileInfo file )
    {
        this.request = request;
        this.file = file;
    }
}

private void UploadFile( FileDialogFileInfo file )
{
    UriBuilder ub = new UriBuilder( app.receiverURL );
    ub.Query = string.Format( "filename={0}", file.Name );

    // Open the selected file to read.
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create( ub.Uri );
    request.Method = "POST";

    RequestState state = new RequestState( request, file );
    request.BeginGetRequestStream( new AsyncCallback( OnUploadReadCallback ), state );
}

private void OnUploadReadCallback( IAsyncResult asynchronousResult )
{
    RequestState state = (RequestState)asynchronousResult.AsyncState;
    HttpWebRequest request = (HttpWebRequest)state.request;

    Stream postStream = request.EndGetRequestStream( asynchronousResult );
    PushData( state.file, postStream );
    postStream.Close();

    state.request.BeginGetResponse( new AsyncCallback( OnUploadResponseCallback ), state.request );
}

private void PushData( FileDialogFileInfo file, Stream output )
{
    byte[] buffer = new byte[ 4096 ];
    int bytesRead = 0;

    Stream input = file.OpenRead();
    while( ( bytesRead = input.Read( buffer, 0, buffer.Length ) ) != 0 )
    {
        output.Write( buffer, 0, bytesRead );
        bytesReadTotal += bytesRead;

        App app = App.Current as App;
        int totalPercentage = Convert.ToInt32( ( bytesReadTotal / app.totalBytesToUpload ) * 100 );

        // enabling the following locks up my UI and browser
        Dispatcher.BeginInvoke( () =>
        {
            this.ProgressBarWithPercentage.Percentage = totalPercentage;
        } );
    }
}
有帮助吗?

解决方案

我要说的是,我不认为Silverlight2的HttpWebRequest支持流,因为该请求的数据被入缓冲存储器。它已经有一段时间,因为我最后一次看着它,因此,我回去看如果测试2的支持。嗯,事实证明它不会。我很高兴我回去读之前,说明。你可以让它通过设置AllowReadStreamBuffering到错误的。你有没有设置这种财产上你的HttpWebRequest?这可能会导致你的块。

编辑,发现了另一个参考。你可能需要按照这种办法通过打破文件进块。这是写的最后一个月,因此我不敢肯定它会的工作中测试2或没有。

其他提示

谢谢,我会看看那些链接,我正在考虑分割我的数据,无论如何,似乎是唯一的办法我可以得到任何合理的进度报告出来的。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top