Advertisement
Guest User

Untitled

a guest
Apr 24th, 2014
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.14 KB | None | 0 0
  1. public class BlobStorageService
  2. {
  3. public CloudBlobContainer GetCloudBlobContainer()
  4. {
  5. CloudStorageAccount storageAccount = CloudStorageAccount.Parse(System.Web.Configuration.WebConfigurationManager.AppSettings["BlobSetting"]);
  6. CloudBlobClient blobclient = storageAccount.CreateCloudBlobClient();
  7. CloudBlobContainer blobcontainer = blobclient.GetContainerReference("mycontainer");
  8. if (blobcontainer.CreateIfNotExists())
  9. {
  10. blobcontainer.SetPermissions(new BlobContainerPermissions { PublicAccess = BlobContainerPublicAccessType.Blob });
  11. }
  12. return blobcontainer;
  13. }
  14.  
  15. public string GetReadData(string filename)
  16. {
  17. // Retrieve storage account from connection string.
  18. CloudStorageAccount storageAccount = CloudStorageAccount.Parse(System.Web.Configuration.WebConfigurationManager.AppSettings["BlobSetting"]);
  19.  
  20. // Create the blob client.
  21. CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
  22.  
  23. // Retrieve reference to a previously created container.
  24. CloudBlobContainer container = blobClient.GetContainerReference("mycontainer");
  25.  
  26. // Retrieve reference to a blob named "myblob.csv"
  27. CloudBlockBlob blockBlob2 = container.GetBlockBlobReference(filename);
  28.  
  29. string text;
  30. using (var memoryStream = new MemoryStream())
  31. {
  32. blockBlob2.DownloadToStream(memoryStream);
  33. text = System.Text.Encoding.UTF8.GetString(memoryStream.ToArray());
  34. }
  35.  
  36. return text;
  37. }
  38. }
  39.  
  40. [HttpPost]
  41. public ActionResult UploadDevicesToRegister11(HttpPostedFileBase userDetailCsvfile)
  42. {
  43. BlobStorageService df = new BlobStorageService();
  44.  
  45. if (userDetailCsvfile.ContentLength > 0)
  46. {
  47. //To upload file on Blob
  48. CloudBlobContainer blobContainer = df.GetCloudBlobContainer();
  49. CloudBlockBlob blob = blobContainer.GetBlockBlobReference(userDetailCsvfile.FileName);
  50. blob.UploadFromStream(userDetailCsvfile.InputStream);
  51.  
  52. //To read File from Blob
  53. blobContainerRead = df.GetReadData(userDetailCsvfile.FileName);
  54. }
  55.  
  56. return View();
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement