Exemple #1
0
 private void uploadProgress(ServerConnectorUploadProgressChangedEventArgs e)
 {
     this.Invoke((MethodInvoker) delegate {
         _picPreview.BackgroundImage = Properties.Resources.icon_24_em_up;
         _lblStatus.Text             = "(" + dtxCore.Utilities.formattedSize(e.bytes_sent) + "/" + dtxCore.Utilities.formattedSize(e.total_bytes_to_send) + ")";
         _barProgress.Maximum        = (int)e.total_bytes_to_send;
         _barProgress.Value          = (int)e.bytes_sent;
     });
 }
Exemple #2
0
        private ServerConnectorResponse postFileStream(Uri address, FileStream file_stream)
        {
            byte[]          buffer = new byte[512];
            byte[]          header_bytes;
            Stream          write_stream;
            int             read_length     = 0;
            ThrottledStream throttled_steam = null;
            var             upload_args     = new ServerConnectorUploadProgressChangedEventArgs();

            if (!file_stream.CanRead)            // If we can not read it, what CAN we do?
            {
                throw new ServerConnectorUploadLockedFile();
            }

            HttpWebRequest request = prepareRequest(address);

            createPostBoundry(request);

            string content_split = "Content-Disposition: form-data; name=\"file\"; filename=\"{0}\"\r\nContent-Type: application/octet-stream\r\n\r\n";

            content_split = string.Format(content_split, Path.GetFileName(file_stream.Name));
            header_bytes  = Encoding.UTF8.GetBytes(content_split);

            upload_args.total_bytes_to_send   = file_stream.Length;
            request.ContentLength             = boundry.boundry_start_bytes.Length + boundry.boundry_end_bytes.Length + file_stream.Length + header_bytes.Length;
            request.AllowWriteStreamBuffering = false;

            write_stream = request.GetRequestStream();

            // If the user wants to throttle, throttle the upload stream.
            if (_throttle_bytes_up != ThrottledStream.Infinite)
            {
                throttled_steam = new ThrottledStream(write_stream, _throttle_bytes_up * 1024);
                write_stream    = throttled_steam;
            }

            write_stream.Write(boundry.boundry_start_bytes, 0, boundry.boundry_start_bytes.Length);
            write_stream.Write(header_bytes, 0, header_bytes.Length);

            while ((read_length = file_stream.Read(buffer, 0, buffer.Length)) > 0)
            {
                write_stream.Write(buffer, 0, read_length);
                write_stream.Flush();

                if (throttle_changed && throttled_steam != null)                  // If the upload throttle has changed, update the bandwidth.
                {
                    throttled_steam.MaximumBytesPerSecond = _throttle_bytes_up * 1024;
                    throttle_changed = false;
                }

                if (cancel_request)
                {
                    upload_canceled.Invoke();
                    break;
                }

                // Update any events with this information
                upload_args.bytes_sent += read_length;
                upload_progress.Invoke(upload_args);
            }

            if (cancel_request == false)
            {
                write_stream.Write(boundry.boundry_end_bytes, 0, boundry.boundry_end_bytes.Length);
                write_stream.Close();
            }

            file_stream.Close();

            if (cancel_request)
            {
                cancel_request = false;
                request.Abort();

                _is_active = false;

                // Send out the queued query if any.
                requestQueueAdvance();
                return(new ServerConnectorResponse());
            }
            else
            {
                return(readServerResponse(request));
            }
        }
        private ServerConnectorResponse postFileStream(Uri address, FileStream file_stream)
        {
            byte[] buffer = new byte[512];
            byte[] header_bytes;
            Stream write_stream;
            int read_length = 0;
            ThrottledStream throttled_steam = null;
            var upload_args = new ServerConnectorUploadProgressChangedEventArgs();

            if(!file_stream.CanRead) // If we can not read it, what CAN we do?
                throw new ServerConnectorUploadLockedFile();

            HttpWebRequest request = prepareRequest(address);
            createPostBoundry(request);

            string content_split = "Content-Disposition: form-data; name=\"file\"; filename=\"{0}\"\r\nContent-Type: application/octet-stream\r\n\r\n";
            content_split = string.Format(content_split, Path.GetFileName(file_stream.Name));
            header_bytes = Encoding.UTF8.GetBytes(content_split);

            upload_args.total_bytes_to_send = file_stream.Length;
            request.ContentLength = boundry.boundry_start_bytes.Length + boundry.boundry_end_bytes.Length + file_stream.Length + header_bytes.Length;
            request.AllowWriteStreamBuffering = false;

            write_stream = request.GetRequestStream();

            // If the user wants to throttle, throttle the upload stream.
            if(_throttle_bytes_up != ThrottledStream.Infinite) {
                throttled_steam = new ThrottledStream(write_stream, _throttle_bytes_up * 1024);
                write_stream = throttled_steam;
            }

            write_stream.Write(boundry.boundry_start_bytes, 0, boundry.boundry_start_bytes.Length);
            write_stream.Write(header_bytes, 0, header_bytes.Length);

            while((read_length = file_stream.Read(buffer, 0, buffer.Length)) > 0) {
                write_stream.Write(buffer, 0, read_length);
                write_stream.Flush();

                if(throttle_changed && throttled_steam != null) { // If the upload throttle has changed, update the bandwidth.
                    throttled_steam.MaximumBytesPerSecond = _throttle_bytes_up * 1024;
                    throttle_changed = false;
                }

                if(cancel_request) {
                    upload_canceled.Invoke();
                    break;
                }

                // Update any events with this information
                upload_args.bytes_sent += read_length;
                upload_progress.Invoke(upload_args);
            }

            if(cancel_request == false) {
                write_stream.Write(boundry.boundry_end_bytes, 0, boundry.boundry_end_bytes.Length);
                write_stream.Close();
            }

            file_stream.Close();

            if(cancel_request) {
                cancel_request = false;
                request.Abort();

                _is_active = false;

                // Send out the queued query if any.
                requestQueueAdvance();
                return new ServerConnectorResponse();

            } else {
                return readServerResponse(request);
            }
        }
 private void uploadProgress(ServerConnectorUploadProgressChangedEventArgs e)
 {
     this.Invoke((MethodInvoker)delegate {
         _picPreview.BackgroundImage = Properties.Resources.icon_24_em_up;
         _lblStatus.Text = "(" + dtxCore.Utilities.formattedSize(e.bytes_sent) + "/" + dtxCore.Utilities.formattedSize(e.total_bytes_to_send) + ")";
         _barProgress.Maximum = (int)e.total_bytes_to_send;
         _barProgress.Value = (int)e.bytes_sent;
     });
 }