private void postFile_GetRequestStreamCallback(IAsyncResult asynchronousResult) { UploadState state = (UploadState)asynchronousResult.AsyncState; state.statusCB("Sending File"); // End the operation try { Stream postStream = state.request.EndGetRequestStream(asynchronousResult); const int BUFFER = 4096; byte[] buffer = new byte[BUFFER]; FileStream encodedFileStream = File.OpenRead(state.filename); int bytes_read = 0; long total_read = 0; bytes_read = encodedFileStream.Read(buffer, 0, BUFFER); while (bytes_read > 0) { total_read += bytes_read; postStream.Write(buffer, 0, bytes_read); bytes_read = encodedFileStream.Read(buffer, 0, BUFFER); state.progCB(((double)total_read / (double)state.totalBytes) * 100.0); } postStream.Close(); encodedFileStream.Close(); File.Delete(state.filename); } catch (Exception e) { state.failureCB(e, "Upload Problem"); return; } state.uploadDoneCB(); // Start the asynchronous operation to get the response state.request.BeginGetResponse(new AsyncCallback(GetResponseCallback), state); }
public void sendFile(string uuid, string file_path, string title, string description, UploadState state) { state.statusCB("Encoding File"); NameValueCollection upload_components = bundleFile(uuid, file_path, title, description); FileStream encodedFileStream = File.OpenRead(upload_components.Get("packed_file")); // this is where we will send it string uri_pattern = "https://{0}/pcpbridge/upload"; string uri = String.Format(uri_pattern, server); long bufferLength = encodedFileStream.Length; // Create the request object HttpWebRequest bridgeSubmitRequest = (HttpWebRequest)WebRequest.Create(uri); bridgeSubmitRequest.ContentType = "multipart/form-data; boundary=" + upload_components.Get("Boundary"); bridgeSubmitRequest.Method = "POST"; bridgeSubmitRequest.KeepAlive = true; bridgeSubmitRequest.Credentials = System.Net.CredentialCache.DefaultCredentials; bridgeSubmitRequest.ContentLength = bufferLength; state.request = bridgeSubmitRequest; state.filename = upload_components.Get("packed_file"); state.totalBytes = bufferLength; encodedFileStream.Close(); bridgeSubmitRequest.BeginGetRequestStream(new AsyncCallback(postFile_GetRequestStreamCallback), state); }
private void upload_click(object sender, EventArgs e) { if (filename.Equals("")) { OpenFileDialog fileChooser = new OpenFileDialog(); fileChooser.Multiselect = false; if (fileChooser.ShowDialog() != DialogResult.OK) { MessageBox.Show("User has cancelled the upload process, terminating program."); forceClose(); return; } filename = fileChooser.FileName; } // Validate input, launch update status_display.Text = "Uploading"; string uuid = ((Workflow)workflow_combo.Items[workflow_combo.SelectedIndex]).uuid; string title = this.title.Text.Trim(); string description = this.description.Text.Trim(); Console.WriteLine(uuid); state = new UploadState(); state.uploadDoneCB = this.DoneWithUpload; state.progCB = this.Progress; state.statusCB = this.ReportStatus; state.responseDoneCB = this.DoneWithUploadResponse; state.failureCB = this.ExceptionNotify; Console.WriteLine(this.filename); pcpBridge.sendFile(uuid, this.filename, title, description, state); }