【发布时间】:2016-02-11 09:01:34
【问题描述】:
我目前正在使用 C# 和 Azure 存储开发 WCF Rest Web 服务,我需要在本地存储帐户中上传文件。此时,我已经能够在特定容器中上传特定文件,但是,我需要能够选择计算机中的任何文件并将其上传到我选择的容器中。
这里是服务中的上传代码:
[WebInvoke(Method = "GET", UriTemplate = "UploadBlob", ResponseFormat = WebMessageFormat.Json)]
public void UploadBlob()
{
// Connect to the storage account's blob endpoint
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("BlobConnectionString"));
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
// Create the blob storage container
CloudBlobContainer container = blobClient.GetContainerReference("mycontainer");
container.CreateIfNotExists();
// Create the blob in the container
CloudBlockBlob blob = container.GetBlockBlobReference("nature");
using (var fileStream = System.IO.File.OpenRead(@"C:\Image\nature.jpg"))
{
blob.UploadFromStream(fileStream);
}
}
我在这里上传“mycontainer”容器中的“nature.jpg”文件。 在我的网络表单中,我使用以下代码在单击按钮后调用该方法:
protected void Button1_Click(object sender, EventArgs e)
{
BlobService upload = new BlobService();
upload.UploadBlob();
}
这是带有我的按钮和文件上传输入的设计代码:
<asp:FileUpload ID="FileUpload" runat="server" />
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Upload" />
当我点击这个按钮时,nature.jpg 被上传。我需要的是能够在我的计算机上选择一个文件并将其上传到我选择的容器中。
【问题讨论】:
标签: c# wcf azure blob azure-blob-storage