通过Windows Forms c#将文件上传到Blob

时间:2016-03-17 15:59:08

标签: c# azure blob azure-storage azure-blob-storage

我正在处理连接到Blob数据库的应用程序,每当我尝试从我的PC中选择一个文件并上传它时,我都会抛出异常。这是代码:

StorageCredentials storageCredentials = new StorageCredentials("filep2pstorage", connectionString);
CloudStorageAccount sotrageAccount = new CloudStorageAccount(storageCredentials, false);
CloudBlobClient _client = sotrageAccount.CreateCloudBlobClient();
CloudBlobContainer container = _client.GetContainerReference("filep2pshare");
container.CreateIfNotExists();

OpenFileDialog dialog = new OpenFileDialog();

dialog.Filter = "Text Files (.txt)|*.txt|All Files (*.*)|*.*";
dialog.FilterIndex = 1;
dialog.Multiselect = true;

string filename = dialog.FileName;                 
string path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);

dialog.InitialDirectory = path;
DialogResult result = dialog.ShowDialog();

if (result == DialogResult.OK)
{
    CloudBlockBlob blob = container.GetBlockBlobReference(filename);

    using (var fileStream = File.OpenRead(path + filename))
    {
        blob.UploadFromStream(fileStream);
    }
}

基本上,这行代码在这里:

 CloudBlockBlob blob = container.GetBlockBlobReference(filename);

 using (var fileStream = File.OpenRead(path + filename))
 {
      blob.UploadFromStream(fileStream);
 }

不接受文件名。我是破坏点,文件名只是设置为“”。

1 个答案:

答案 0 :(得分:2)

您需要在if中移动以下代码行。在当前位置获取它时,它不会在对话框中设置。

string filename = dialog.FileName;