public string GetThumbnail(int id, System.IO.Stream strm) { string fileName = this.DB.Products.Where(a => a.ProductID == id).Select(a => a.ThumbnailPhotoFileName).FirstOrDefault(); if (string.IsNullOrEmpty(fileName)) { return(""); } System.Transactions.TransactionOptions top = new System.Transactions.TransactionOptions(); top.Timeout = TimeSpan.FromSeconds(60); top.IsolationLevel = System.Transactions.IsolationLevel.Serializable; using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required, top)) using (DbConnection conn = DBConnectionFactory.GetRIAppDemoConnection()) { byte[] bytes = new byte[64 * 1024]; string fldname = "ThumbNailPhoto"; BlobStream bstrm = new BlobStream(conn as SqlConnection, "[SalesLT].[Product]", fldname, string.Format("WHERE [ProductID]={0}", id)); bstrm.Open(); int cnt = bstrm.Read(bytes, 0, bytes.Length); while (cnt > 0) { strm.Write(bytes, 0, cnt); cnt = bstrm.Read(bytes, 0, bytes.Length); } bstrm.Close(); scope.Complete(); } return(fileName); }
public void SaveThumbnail(int id, string fileName, System.IO.Stream strm) { var product = this.DB.Products.Where(a => a.ProductID == id).FirstOrDefault(); if (product == null) { throw new Exception("Product is not found"); } TransactionOptions topts = new System.Transactions.TransactionOptions(); topts.Timeout = TimeSpan.FromSeconds(60); topts.IsolationLevel = System.Transactions.IsolationLevel.Serializable; using (TransactionScope trxScope = new TransactionScope(TransactionScopeOption.Required, topts)) using (DbConnection conn = DBConnectionFactory.GetRIAppDemoConnection()) { System.IO.BinaryReader br = new System.IO.BinaryReader(strm); byte[] bytes = br.ReadBytes(64 * 1024); string fldname = "ThumbNailPhoto"; BlobStream bstrm = new BlobStream(conn as SqlConnection, "[SalesLT].[Product]", fldname, string.Format("WHERE [ProductID]={0}", id)); bstrm.InitColumn(); bstrm.Open(); while (bytes != null && bytes.Length > 0) { bstrm.Write(bytes, 0, bytes.Length); bytes = br.ReadBytes(64 * 1024);; } bstrm.Close(); br.Close(); trxScope.Complete(); } product.ThumbnailPhotoFileName = fileName; this.DB.SubmitChanges(); }
// Get a page of a page blob. // Return true on success, false if unable to create, throw exception on error. public bool GetPage(string containerName, string blobName, int pageOffset, int pageSize, out string content) { content = null; try { CloudBlobContainer container = BlobClient.GetContainerReference(containerName); CloudPageBlob blob = container.GetPageBlobReference(blobName); BlobStream stream = blob.OpenRead(); byte[] data = new byte[pageSize]; stream.Seek(pageOffset, SeekOrigin.Begin); stream.Read(data, 0, pageSize); content = new UTF8Encoding().GetString(data); stream.Close(); return(true); } catch (StorageClientException ex) { if ((int)ex.StatusCode == 404) { return(false); } throw; } }
public T Get() { BlobStream stream = blob.OpenRead(); T t = Deserialize(stream); stream.Close(); return(t); }
public static void UploadBlobFile(byte[] fileBytes, string fileName) { try { string storageAccountConnection = string.Empty; storageAccountConnection = ConfigurationManager.AppSettings["StorageAccount.ConnectionString"].ToString(); // If you want to use Windows Azure cloud storage account, use the following // code (after uncommenting) instead of the code above. cloudStorageAccount = CloudStorageAccount.Parse(storageAccountConnection); // Create the blob client, which provides // authenticated access to the Blob service. blobClient = cloudStorageAccount.CreateCloudBlobClient(); string deploymentPackageFolderString = string.Empty; deploymentPackageFolderString = ConfigurationManager.AppSettings["DeploymentPackageFolder"].ToString(); // Get the container reference. blobContainer = blobClient.GetContainerReference(deploymentPackageFolderString); // Create the container if it does not exist. blobContainer.CreateIfNotExist(); // Set permissions on the container. containerPermissions = new BlobContainerPermissions(); // This sample sets the container to have public blobs. Your application // needs may be different. See the documentation for BlobContainerPermissions // for more information about blob container permissions. containerPermissions.PublicAccess = BlobContainerPublicAccessType.Blob; blobContainer.SetPermissions(containerPermissions); blob = blobContainer.GetBlobReference(fileName); // Open a stream using the cloud object using (BlobStream blobStream = blob.OpenWrite()) { blobStream.Write(fileBytes, 0, fileBytes.Count()); blobStream.Flush(); blobStream.Close(); } } catch (System.Exception ex) { Logger.Write(string.Format("Error in UploadBlobFile() Error: {0}", ex.Message)); } }
public T GetIfExists(Exception e) { try { BlobStream stream = blob.OpenRead(); T t = Deserialize(stream); stream.Close(); return(t); } catch (Exception) { throw e; } }
public void SaveThumbnail2(int id, string fileName, Func <Stream, Task> copy) { var product = DB.Products.Where(a => a.ProductID == id).FirstOrDefault(); if (product == null) { throw new Exception("Product is not found"); } var topts = new TransactionOptions(); topts.Timeout = TimeSpan.FromSeconds(60); topts.IsolationLevel = IsolationLevel.Serializable; using (var trxScope = new TransactionScope(TransactionScopeOption.Required, topts)) using (var conn = DBConnectionFactory.GetRIAppDemoConnection()) { var fldname = "ThumbNailPhoto"; var bstrm = new BlobStream(conn as SqlConnection, "[SalesLT].[Product]", fldname, string.Format("WHERE [ProductID]={0}", id)); bstrm.InitColumn(); bstrm.Open(); try { if (!copy(bstrm).Wait(10000)) { throw new Exception("Write stream timeout"); } } finally { bstrm.Close(); } trxScope.Complete(); } product.ThumbnailPhotoFileName = fileName; DB.SaveChanges(); }