Exemplo n.º 1
0
 protected virtual void OnUploadComplete(UploadEventArgs e)
 {
     if (UploadCompleted != null)
     {
         UploadCompleted(this, e);
     }
 }
Exemplo n.º 2
0
 protected virtual void OnProgressChanged(UploadEventArgs e)
 {
     if (ProgressChanged != null)
     {
         ProgressChanged(this, e);
     }
 }
Exemplo n.º 3
0
        private void WriteNextChunck(UploadData upload)
        {
            try
            {
                Thread.Sleep(20);
                if ((upload.FileStream.Length - upload.BytesWritten) >= 16 * 1024)
                {
                    upload.Buffer = new byte[16 * 1024];
                }
                else
                {
                    // Last part
                    upload.Buffer = new byte[upload.FileStream.Length - upload.BytesWritten];
                }

                upload.FileStream.Read(upload.Buffer, 0, (int)upload.Buffer.Length);
                upload.PostStream.BeginWrite(upload.Buffer, 0, upload.Buffer.Length, BeginWriteCallback, upload);
            }
            catch (Exception ex)
            {
                m_uploadData.Upload.UploadInfo.StatusCode = HttpStatusCode.NotFound;
                m_uploadData.Upload.UploadInfo.Exception  = new Exception("Buffer write failed: " + ex.Message);
                var argsStopped = new UploadEventArgs();
                argsStopped.Upload = m_uploadData.Upload;
                upload.FileStream.Close();
                upload.PostStream.Close();
                OnUploadComplete(argsStopped);
            }
        }
Exemplo n.º 4
0
        private void GetRequestStreamCallback(IAsyncResult asynchronousResult, UploadData uploadData)
        {
            try
            {
                HttpWebRequest request    = (HttpWebRequest)asynchronousResult.AsyncState;
                Stream         postStream = request.EndGetRequestStream(asynchronousResult);

                postStream.Write(uploadData.HeaderBytes, 0, uploadData.HeaderBytes.Length);

                var args = new UploadEventArgs();
                args.Upload   = uploadData.Upload;
                args.Progress = 1;
                OnProgressChanged(args);
                uploadData.PostStream = postStream;

                WriteNextChunck(uploadData);
            }
            catch (Exception ex)
            {
                m_uploadData.Upload.UploadInfo.StatusCode = HttpStatusCode.NotFound;
                m_uploadData.Upload.UploadInfo.Exception  = new Exception("Header write failed: " + ex.Message);
                var argsStopped = new UploadEventArgs();
                argsStopped.Upload = m_uploadData.Upload;
                m_uploadData.FileStream.Close();
                m_uploadData.PostStream.Close();
                OnUploadComplete(argsStopped);
            }
        }
Exemplo n.º 5
0
        public async Task StartUpload(Photo upload, Uri uri, Dictionary <string, string> parameters)
        {
            try
            {
                m_isStopped = false;
                Stream fileStream = null;
                if (App.SaveToCameraRollEnabled)
                {
                    fileStream = GetBitmapFromMediaLib(upload);
                }
                else
                {
                    fileStream = (await upload.File.OpenReadAsync()).AsStreamForRead();
                }
                var uploadData = new UploadData();

                boundarystr = "---------------------------" + DateTime.Now.Ticks.ToString("x");
                string para          = GetParamsString(parameters);
                string headAndParams = para + String.Format(headerString, HttpUtility.UrlEncode(upload.Title));

                var headerBytes = System.Text.Encoding.UTF8.GetBytes(headAndParams);
                var footerBytes = Encoding.UTF8.GetBytes("\r\n--" + boundarystr + "--\r\n");

                uploadData.Upload       = upload;
                uploadData.FileStream   = fileStream;
                uploadData.FooterBytes  = footerBytes;
                uploadData.HeaderBytes  = headerBytes;
                uploadData.BytesWritten = 0;
                m_uploadData            = uploadData;
                m_request        = (HttpWebRequest)WebRequest.Create(uri);
                m_request.Method = "POST";
                m_request.AllowWriteStreamBuffering = false;
                m_request.ContentType   = string.Format(contentType, boundarystr);
                m_request.ContentLength = fileStream.Length + headerBytes.Length + footerBytes.Length;
                var asyncResult = m_request.BeginGetRequestStream((ar) => { GetRequestStreamCallback(ar, uploadData); }, m_request);
            }
            catch (Exception ex)
            {
                m_uploadData.Upload.UploadInfo.StatusCode = HttpStatusCode.NotFound;
                m_uploadData.Upload.UploadInfo.Exception  = new Exception("Start upload failed: " + ex.Message);
                var argsStopped = new UploadEventArgs();
                argsStopped.Upload = m_uploadData.Upload;
                m_uploadData.FileStream.Close();
                m_uploadData.PostStream.Close();
                OnUploadComplete(argsStopped);
            }
        }
Exemplo n.º 6
0
 private void WriteFooter(UploadData upload)
 {
     try
     {
         upload.PostStream.Write(upload.FooterBytes, 0, upload.FooterBytes.Length);
         upload.PostStream.Close();
         var asyncResult = m_request.BeginGetResponse(new AsyncCallback(GetResponseCallback), m_request);
     }
     catch (Exception ex)
     {
         m_uploadData.Upload.UploadInfo.StatusCode = HttpStatusCode.NotFound;
         m_uploadData.Upload.UploadInfo.Exception  = new Exception("Footer write failed: " + ex.Message);
         var argsStopped = new UploadEventArgs();
         argsStopped.Upload = m_uploadData.Upload;
         OnUploadComplete(argsStopped);
     }
 }
Exemplo n.º 7
0
        private void BeginWriteCallback(IAsyncResult ar)
        {
            try
            {
                var upload = ar.AsyncState as UploadData;
                upload.PostStream.EndWrite(ar);
                upload.BytesWritten += upload.Buffer.Length;

                var args = new UploadEventArgs();
                args.Upload   = upload.Upload;
                args.Progress = (int)(((decimal)upload.BytesWritten / (decimal)upload.FileStream.Length) * 100);
                OnProgressChanged(args);

                if (m_isStopped)
                {
                    upload.FileStream.Close();
                    upload.PostStream.Close();
                    m_uploadData.Upload.UploadInfo.StatusCode = HttpStatusCode.NotFound;
                    m_uploadData.Upload.UploadInfo.Exception  = new Exception("Upload stopped");
                    var argsStopped = new UploadEventArgs();
                    argsStopped.Upload = m_uploadData.Upload;
                    OnUploadComplete(argsStopped);
                    return;
                }

                // write next chunck
                if (upload.BytesWritten < upload.FileStream.Length)
                {
                    WriteNextChunck(upload);
                }
                if (upload.BytesWritten >= upload.FileStream.Length)
                {
                    WriteFooter(upload);
                }
            }
            catch (Exception ex)
            {
                m_uploadData.Upload.UploadInfo.StatusCode = HttpStatusCode.NotFound;
                m_uploadData.Upload.UploadInfo.Exception  = new Exception("Upload write failed: " + ex.Message);
                var argsStopped = new UploadEventArgs();
                argsStopped.Upload = m_uploadData.Upload;
                OnUploadComplete(argsStopped);
            }
        }
Exemplo n.º 8
0
        private void GetResponseCallback(IAsyncResult asynchronousResult)
        {
            try
            {
                HttpWebRequest  request        = (HttpWebRequest)asynchronousResult.AsyncState;
                HttpWebResponse response       = (HttpWebResponse)request.EndGetResponse(asynchronousResult);
                Stream          streamResponse = response.GetResponseStream();
                StreamReader    streamRead     = new StreamReader(streamResponse);
                string          responseString = streamRead.ReadToEnd();
                streamResponse.Close();
                streamRead.Close();
                response.Close();

                m_uploadData.FileStream.Close();
                m_uploadData.Upload.UploadInfo.StatusCode = response.StatusCode;

                if (response.StatusCode == HttpStatusCode.NoContent)
                {
                    m_uploadData.Upload.UploadInfo.Exception = null;
                }
                else
                {
                    m_uploadData.Upload.UploadInfo.Exception = new Exception(responseString);
                }
                var args = new UploadEventArgs();
                args.Upload   = m_uploadData.Upload;
                args.Progress = 100;
                OnUploadComplete(args);
            }
            catch (Exception ex)
            {
                m_uploadData.Upload.UploadInfo.StatusCode = HttpStatusCode.NotFound;
                m_uploadData.Upload.UploadInfo.Exception  = ex;
                var args = new UploadEventArgs();
                args.Upload = m_uploadData.Upload;
                OnUploadComplete(args);
            }
        }