1

I have a SharePoint app with a custom action. With this custom action I want to upload the selected file in the SharePoint document library to a container in the azure blob storage.

How can I upload the file to azure?

I've tried sending the url of the file to a page on the host web and put the file in a stream using the url but I get an error 401 unauthorized.

1 Answer 1

0

Once You have the file stream, you can use the below code to upload file to blob storage.

string azureConnString = "YourAzureStorageConnStringName";
    CloudBlobClient blobClient;
    CloudStorageAccount storageAccount;
    CloudBlobContainer container;
    CloudBlockBlob blockBlob;
    public bool AddFileToBlob(System.IO.Stream stream, string fileName, string containerName)
    {
        byte[] bytesarr =  GetByteFromStream(stream); //Convert the stream to byte array

        using (MemoryStream memoryStrem = new MemoryStream(bytesarr))
        {

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

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

            // Retrieve reference to a previously created container.
            container = blobClient.GetContainerReference(containerName);
            container.CreateIfNotExists();

            // Retrieve reference to a blob named "myblob".
            blockBlob = container.GetBlockBlobReference(fileName);
            if (!blockBlob.Exists())
                blockBlob.UploadFromStream(memoryStrem);


            return true;
        }
    }
2
  • Maybe I didn't formulate my question right. I know about that part. The problem is getting the file from sharepoint into a stream.
    – Marijn
    Commented Dec 6, 2013 at 13:58
  • Can you please post your code that is giving you unauthorized error? Are you using a high trust provider hosted app ? Commented Dec 9, 2013 at 6:13

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.