gpt4 book ai didi

c# - 将 blob 和容器列出到列表框 C#

转载 作者:行者123 更新时间:2023-12-03 03:12:22 25 4
gpt4 key购买 nike

我已经使用 WCF 创建了一个 Azure 云服务,我试图在其中列出我的容器和 blob。我已经能够列出我的容器和 blob。我想做的是,当我在列表框中选择一个容器时,它将显示它在另一个列表框中包含的 blob。

这是我列出容器的代码:

    public List<string> ListContainer()
{
List<string> blobs = new List<string>();

// Retrieve storage account from connection string.
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
CloudConfigurationManager.GetSetting("BlobConnectionString"));

// Create the blob client.
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

// Retrieve reference to a previously created container.
CloudBlobContainer container = blobClient.GetContainerReference("mycontainer");

//Get the list of the blob from the above container

IEnumerable<CloudBlobContainer> containers = blobClient.ListContainers();

foreach (CloudBlobContainer item in containers)
{

blobs.Add(string.Format("{0}", item.Uri));
}

return blobs;
}

列出我的 blob 的代码:

        public List<string> ListBlob(string folder)
{
List<string> blobs = new List<string>();

// Retrieve storage account from connection string.
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
CloudConfigurationManager.GetSetting("BlobConnectionString"));

// Create the blob client.
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

// Retrieve reference to a previously created container.
CloudBlobContainer container = blobClient.GetContainerReference(folder);


//Get the list of the blob from the above container
IEnumerable<IListBlobItem> blobList = container.ListBlobs();

//Display the names on the page
string names = string.Empty;

foreach (IListBlobItem item in blobList)
{
blobs.Add(string.Format("Directory {0}", item.Uri));
}

在我的 Web 表单中,我在页面加载中调用 ListContainer 方法:

 protected void Page_Load(object sender, EventArgs e)
{
BlobService list = new BlobService();
ListBox2.DataSource = list.ListContainer();
ListBox2.DataBind();

}

当我加载项目时,我的容器列在 ListBox 2 中。我需要的是,当我单击容器时,它将显示它包含在另一个 ListBox 中的 blob。我尝试了以下操作,但没有任何反应:

protected void ListBox2_SelectedIndexChanged(object sender, EventArgs e)
{
if (ListBox2.SelectedItem.Equals("http://127.0.0.1:10000/devstorageaccount1/mycontainer"))
{
BlobService list = new BlobService();
ListBox1.DataSource = list.ListBlob("mycontainer");
ListBox1.DataBind();
}

else
{
//Error Message
}

最佳答案

如果索引已更改,则在回发到服务器期间将调用事件处理程序 ListBox2_SelectedIndexChanged。为了在用户选择新项目时启动回发,您需要在 aspx 标记中的 ListBox2 上设置 AutoPostBack="True" (请参阅此 link详情):

<asp:ListBox ID="ListBox2" runat="server" AutoPostBack="True" 
OnSelectedIndexChanged="ListBox2_SelectedIndexChanged" />

关于c# - 将 blob 和容器列出到列表框 C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35380847/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com