Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class BlobStorageService
- {
- public CloudBlobContainer GetCloudBlobContainer()
- {
- CloudStorageAccount storageAccount = CloudStorageAccount.Parse(System.Web.Configuration.WebConfigurationManager.AppSettings["BlobSetting"]);
- CloudBlobClient blobclient = storageAccount.CreateCloudBlobClient();
- CloudBlobContainer blobcontainer = blobclient.GetContainerReference("mycontainer");
- if (blobcontainer.CreateIfNotExists())
- {
- blobcontainer.SetPermissions(new BlobContainerPermissions { PublicAccess = BlobContainerPublicAccessType.Blob });
- }
- return blobcontainer;
- }
- public string GetReadData(string filename)
- {
- // Retrieve storage account from connection string.
- CloudStorageAccount storageAccount = CloudStorageAccount.Parse(System.Web.Configuration.WebConfigurationManager.AppSettings["BlobSetting"]);
- // Create the blob client.
- CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
- // Retrieve reference to a previously created container.
- CloudBlobContainer container = blobClient.GetContainerReference("mycontainer");
- // Retrieve reference to a blob named "myblob.csv"
- CloudBlockBlob blockBlob2 = container.GetBlockBlobReference(filename);
- string text;
- using (var memoryStream = new MemoryStream())
- {
- blockBlob2.DownloadToStream(memoryStream);
- text = System.Text.Encoding.UTF8.GetString(memoryStream.ToArray());
- }
- return text;
- }
- }
- [HttpPost]
- public ActionResult UploadDevicesToRegister11(HttpPostedFileBase userDetailCsvfile)
- {
- BlobStorageService df = new BlobStorageService();
- if (userDetailCsvfile.ContentLength > 0)
- {
- //To upload file on Blob
- CloudBlobContainer blobContainer = df.GetCloudBlobContainer();
- CloudBlockBlob blob = blobContainer.GetBlockBlobReference(userDetailCsvfile.FileName);
- blob.UploadFromStream(userDetailCsvfile.InputStream);
- //To read File from Blob
- blobContainerRead = df.GetReadData(userDetailCsvfile.FileName);
- }
- return View();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement