/// <summary> /// Example of an INSERT of a BLOB /// </summary> /// <param name="customerId">The customer ID.</param> /// <param name="itemId">the item ID.</param> /// <exception cref="AceQLException">If any Exception occurs.</exception> public async Task InsertBlobProgressIndicator(int customerId, int itemId) { // Create a transaction because some database engines require autocommit off AceQLTransaction transaction = await connection.BeginTransactionAsync(); try { string sql = "insert into orderlog values " + "(@customer_id, @item_id, @description, " + "@item_cost, @date_placed, @date_shipped, " + "@jpeg_image, @is_delivered, @quantity)"; AceQLCommand command = new AceQLCommand(sql, connection); string userPath = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile); string blobPath = userPath + "\\koala.jpg"; Stream stream = new FileStream(blobPath, FileMode.Open, FileAccess.Read); long length = new FileInfo(blobPath).Length; AceQLConsole.WriteLine("blobPath: " + blobPath); AceQLConsole.WriteLine("insert into orderlog..."); command.Parameters.AddWithValue("@customer_id", customerId); command.Parameters.AddWithValue("@item_id", itemId); command.Parameters.AddWithValue("@description", "Item Description"); command.Parameters.AddWithValue("@item_cost", 99D); command.Parameters.AddWithValue("@date_placed", DateTime.Now); command.Parameters.AddWithValue("@date_shipped", DateTime.Now); command.Parameters.AddWithValue("@jpeg_image", stream, length); command.Parameters.AddWithValue("@is_delivered", 1); command.Parameters.AddWithValue("@quantity", 1); AceQLProgressIndicator progressIndicator = new AceQLProgressIndicator(); connection.SetProgressIndicator(progressIndicator); await command.ExecuteNonQueryAsync(); await transaction.CommitAsync(); } catch (Exception e) { // Transaction must always be terminated by a CommitAsync() or RollbackAsync() await transaction.RollbackAsync(); throw e; } }
/// <summary> /// Uploads a file using a blob reference. /// </summary> /// <param name="url"></param> /// <param name="proxyUri"></param> /// <param name="credentials"></param> /// <param name="timeout"></param> /// <param name="enableDefaultSystemAuthentication"></param> /// <param name="blobId"></param> /// <param name="stream"></param> /// <param name="totalLength"></param> /// <param name="progressIndicator"></param> /// <param name="cancellationToken"></param> /// <param name="useCancellationToken"></param> /// <returns></returns> internal async Task <HttpResponseMessage> UploadAsync(String url, String proxyUri, ICredentials credentials, int timeout, bool enableDefaultSystemAuthentication, String blobId, Stream stream, long totalLength, AceQLProgressIndicator progressIndicator, CancellationToken cancellationToken, bool useCancellationToken) { HttpClientHandler handler = HttpClientHandlerBuilderNew.Build(proxyUri, credentials, enableDefaultSystemAuthentication); ProgressMessageHandler processMessageHander = new ProgressMessageHandler(handler); HttpClient httpClient = new HttpClient(processMessageHander); if (timeout != 0) { long nanoseconds = 1000000 * timeout; httpClient.Timeout = new TimeSpan(nanoseconds / 100); } processMessageHander.HttpSendProgress += (sender, e) => { int num = e.ProgressPercentage; this.tempLen += e.BytesTransferred; if (progressIndicator != null) { if (totalLength > 0 && tempLen > totalLength / 100) { tempLen = 0; } int cpt = progressIndicator.Percent; cpt++; progressIndicator.SetValue(Math.Min(99, cpt)); if (DEBUG) { ConsoleEmul.WriteLine(DateTime.Now + " progressHolder.Progress: " + progressIndicator.Percent); } } else { if (DEBUG) { ConsoleEmul.WriteLine(DateTime.Now + " num: " + num); } } }; StringContent stringContentBlobId = new StringContent(blobId); try { var multipart = new MultipartFormDataContent(); multipart.Add(stringContentBlobId, '"' + "blob_id" + '"'); multipart.Add(new StreamContent(stream), '"' + "file" + '"', '"' + blobId + '"'); if (DEBUG) { ConsoleEmul.WriteLine(); ConsoleEmul.WriteLine("url : " + url); ConsoleEmul.WriteLine("blob_id : " + blobId); } HttpResponseMessage response = null; if (!useCancellationToken) { response = await httpClient.PostAsync(url, multipart).ConfigureAwait(false); } else { response = await httpClient.PostAsync(url, multipart, cancellationToken).ConfigureAwait(false); } // Allows a retry for 407, because can happen time to time with Web proxies if (response.StatusCode.Equals(HttpStatusCode.ProxyAuthenticationRequired)) { while (proxyAuthenticationCallCount < HttpRetryManager.ProxyAuthenticationCallLimit) { proxyAuthenticationCallCount++; if (!useCancellationToken) { response = await httpClient.PostAsync(url, multipart).ConfigureAwait(false); } else { response = await httpClient.PostAsync(url, multipart, cancellationToken).ConfigureAwait(false); } if (!response.StatusCode.Equals(HttpStatusCode.ProxyAuthenticationRequired)) { proxyAuthenticationCallCount = 0; break; } } } if (progressIndicator != null) { progressIndicator.SetValue(100); } return(response); } finally { stream.Dispose(); httpClient.Dispose(); } }