Ejemplo n.º 1
0
 /// <summary>
 /// Upload file to server.
 /// </summary>
 /// <param name="filepath">Path to the file to upload.</param>
 /// <param name="token">Upload token.</param>
 /// <param name="callback">Delegate to run after upload is complete.</param>
 public static void UploadAsync(string filepath, Token token, Action callback)
 {
     // Old
     /*Thread thread = new Thread(new ThreadStart(() =>
     {
         Debug.WriteLine("Uploading Async");
         Upload(filepath, token);
         Debug.WriteLine("Upload done, running callback");
         callback();
     }));
     thread.Start();*/
 }
Ejemplo n.º 2
0
        /// <summary>
        /// Upload file to server.
        /// </summary>
        /// <param name="filepath">Path to the file to upload.</param>
        /// <param name="token">Upload token.</param>
        public static void Upload(string filepath, Token token)
        {
            //Library from http://aspnetupload.com/Upload-File-POST-HttpWebRequest-WebClient-RFC-1867.aspx
            //string url = HOST + "/upload";
            //ProgressForm.Instance.BeginInvoke(new Action(() => { ProgressForm.Instance.Show(); }), null);

            ProgressForm.Show();

            bool tryagain = false;
            do
            {
                try
                {
                    string url = "http://qpaste.s3.amazonaws.com/";
                    UploadFile[] files = new UploadFile[]
                    {
                        new UploadFile(filepath, "file", token.storage.s3Policy.conditions.mime)
                    };
                    NameValueCollection form = new NameValueCollection();
                    form["key"] = token.storage.s3Policy.conditions.key;
                    form["AWSAccessKeyId"] = token.storage.s3Key;
                    form["Policy"] = token.storage.s3PolicyBase64;
                    form["Signature"] = token.storage.s3Signature;
                    form["Bucket"] = token.storage.s3Policy.conditions.bucket;
                    form["acl"] = token.storage.s3Policy.conditions.acl;
                    form["Content-Type"] = token.storage.s3Policy.conditions.mime;
                    form["Content-Disposition"] = token.storage.s3Policy.conditions.disposition;

                    string response = Krystalware.UploadHelper.HttpUploadHelper.Upload(url, files, form, new Action<int>((int progress) => {
                        ProgressForm.Value = progress;
                    }));
                    UploadDone(token.token);

                    tryagain = false;
                }
                catch
                {
                    //string output = new StreamReader(ex.Response.GetResponseStream()).ReadToEnd();
                    //Console.WriteLine("Exception, suck it");
                    DialogResult result = MessageBox.Show("An error occurred when qPaste was uploading your file!\nDo you want to try again?",
                        "Upload error",
                        MessageBoxButtons.YesNoCancel,
                        MessageBoxIcon.Error);

                    tryagain = result.Equals(DialogResult.Yes);
                }
            } while (tryagain);

            ProgressForm.Hide();
        }