upload csv file to blob through c# .net and delete it after entering the data into database

StackOverflow https://stackoverflow.com/questions/22618603

  •  20-06-2023
  •  | 
  •  

سؤال

How can I upload a .csv file into blob and then read it from back-end and delete it after the data is entered into database?

As I am new to this, I don't have much knowledge of it. I have gone through the azure website and tried their code but it doesn't work for me.

Thanks in advance.

هل كانت مفيدة؟

المحلول

First Create your container in Azure and get your AccountName and Access Key to your ur blob Storage.

Follow this link : http://azure.microsoft.com/en-us/documentation/articles/storage-dotnet-how-to-use-blobs-20/

Than in your visual studio create a class file and follow code given below :

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;
    }
}

Controller :

[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();
}  

Hope this Helps.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top