locked
Progress Indicator and File Upload RRS feed

  • Question

  • Hi all,

    Hope somone can help. I have class that has only one method as follows

    //the file that we want to upload
    string FilePath="d:\somefile.pdf";
    int Offset = 0; // starting offset.
    
    //define the chunk size
    int ChunkSize = 65536; // 64 * 1024 kb
    
    //define the buffer array according to the chunksize.
    byte[] Buffer = new byte[ChunkSize];
    //opening the file for read.
    FileStream fs = new FileStream(FilePath, FileMode.Open, FileAccess.Read);
    //creating the ServiceSoapClient which will allow to connect to the service.
    WSservice.ServiceSoapClient soap_client = new WSservice.ServiceSoapClient();
    try
    {
    long FileSize = new FileInfo(FilePath).Length; // File size of file being uploaded.
    // reading the file.
    fs.Position = Offset;
    int BytesRead = 0;
    while (Offset != FileSize) // continue uploading the file chunks until offset = file size.
    {
    BytesRead = fs.Read(Buffer, 0, ChunkSize); // read the next chunk 
    	// (if it exists) into the buffer. 
    	// the while loop will terminate if there is nothing left to read
    // check if this is the last chunk and resize the buffer as needed 
    // to avoid sending a mostly empty buffer 
    // (could be 10Mb of 000000000000s in a large chunk)
    if (BytesRead != Buffer.Length)
    {
    ChunkSize = BytesRead;
    byte[] TrimmedBuffer = new byte[BytesRead];
    Array.Copy(Buffer, TrimmedBuffer, BytesRead);
    Buffer = TrimmedBuffer; // the trimmed buffer should become the new 'buffer'
    }
    // send this chunk to the server. it is sent as a byte[] parameter, 
    // but the client and server have been configured to encode byte[] using MTOM. 
    bool ChunkAppened = soap_client.UploadFile(Path.GetFileName(FilePath), Buffer, Offset);
    if (!ChunkAppened)
    {
    break;
    }
    
    // Offset is only updated AFTER a successful send of the bytes. 
    Offset += BytesRead; // save the offset position for resume
    }
    }
    catch (Exception ex)
    {
    }
    finally
    {
    fs.Close();
    }
    
    
    I want to show a progress indicator on my UI when the file is being uploaded. I am going to add a percent counter to this code but how and where should i bind the progress indicators value so that the progress bar will increment when the file is uploading.
    Thanks in advance.
    A
    
    
    Wednesday, October 24, 2012 5:15 PM

All replies