1
如何将.csv文件上传到blob,然后从后端读取并在数据输入数据库后将其删除?通过c#.net将csv文件上传到blob,并在将数据输入到数据库后将其删除
由于我是新手,对此我并不了解。 我曾经在Azure网站上经历过,并尝试过他们的代码,但它不适合我。
在此先感谢。
如何将.csv文件上传到blob,然后从后端读取并在数据输入数据库后将其删除?通过c#.net将csv文件上传到blob,并在将数据输入到数据库后将其删除
由于我是新手,对此我并不了解。 我曾经在Azure网站上经历过,并尝试过他们的代码,但它不适合我。
在此先感谢。
首先在Azure中创建容器,并将您的AccountName和Access Key移动到您的blob存储中。
请点击此链接: http://azure.microsoft.com/en-us/documentation/articles/storage-dotnet-how-to-use-blobs-20/
比你的视觉工作室创建一个类文件,并按照下面的代码给出:
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();
}
希望这有助于。
感谢您的详细解释兄弟......我明白了......我对容器有问题...... –