1

I am working on application that is connected to a Blob Database and I am having an exception being thrown whenever I try to pick a file from my PC and upload it. This is the code:

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

Basically, this line of code here:

 CloudBlockBlob blob = container.GetBlockBlobReference(filename);

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

Does not take the file name. I was breakpointing and the filename is just set to "".

1 Answer 1

2

You need to move the following line of code inside the if. It is not set in the dialog when you get it at the current location.

string filename = dialog.FileName;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.