Esempio n. 1
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);
            }
        }
Esempio n. 2
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);
            }
        }
Esempio n. 3
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);
            }
        }
Esempio n. 4
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);
     }
 }