Skip to content

Commit

Permalink
Trigger the UploadProgressChanged event wen uploading a file.
Browse files Browse the repository at this point in the history
	Fixes bug mono#3100.
  • Loading branch information
gonzalop committed Mar 22, 2012
1 parent 1b9640d commit 30dcaff
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
Expand Up @@ -27,8 +27,6 @@
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
#if NET_2_0

using System.ComponentModel;
using System.IO;

Expand Down Expand Up @@ -68,4 +66,4 @@ public class UploadProgressChangedEventArgs : ProgressChangedEventArgs
}
}

#endif

23 changes: 22 additions & 1 deletion mcs/class/System/System.Net/WebClient.cs
Expand Up @@ -563,9 +563,30 @@ private string DetermineMethod (Uri address, string method, bool is_upload)
reqStream.Write (partHeadersBytes, 0, partHeadersBytes.Length);
}
int nread;
long bytes_sent = 0;
long file_size = -1;
long step = 16384; // every 16kB
if (fStream.CanSeek) {
file_size = fStream.Length;
step = file_size / 100;
}
var upload_args = new UploadProgressChangedEventArgs (0, 0, bytes_sent, file_size, 0, userToken);
OnUploadProgressChanged (upload_args);
byte [] buffer = new byte [4096];
while ((nread = fStream.Read (buffer, 0, 4096)) != 0)
long sum = 0;
while ((nread = fStream.Read (buffer, 0, 4096)) > 0) {
reqStream.Write (buffer, 0, nread);
bytes_sent += nread;
sum += nread;
if (sum >= step || nread < 4096) {
int percent = 0;
if (file_size > 0)
percent = (int) (bytes_sent * 100 / file_size);
upload_args = new UploadProgressChangedEventArgs (0, 0, bytes_sent, file_size, percent, userToken);
OnUploadProgressChanged (upload_args);
sum = 0;
}
}

if (needs_boundary) {
reqStream.WriteByte ((byte) '\r');
Expand Down

0 comments on commit 30dcaff

Please sign in to comment.