0
votes
  public class StorageService
    {
        CloudStorageAccount storageAccount = CloudStorageAccount.Parse("DefaultEndpointsProtocol=https;AccountName=leepiostorage;AccountKey=removed for this post");

        public async Task Upload(string id, Stream data)
        {
            CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
            // Retrieve a reference to a container.
            CloudBlobContainer container = blobClient.GetContainerReference("images");

            await container.CreateIfNotExistsAsync();

            container.SetPermissions(
                new BlobContainerPermissions
                {
                    PublicAccess = BlobContainerPublicAccessType.Blob
                }); 


            CloudBlockBlob blockBlob = container.GetBlockBlobReference(id);

            await blockBlob.UploadFromStreamAsync(data, data.Length);
        }

        public async Task UploadBlogPhoto(string id, Stream data)
        {
            CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
            // Retrieve a reference to a container.
            CloudBlobContainer container = blobClient.GetContainerReference("mycontainer");

            await container.CreateIfNotExistsAsync();

            container.SetPermissions(
                new BlobContainerPermissions
                {
                    PublicAccess = BlobContainerPublicAccessType.Blob
                });


            CloudBlockBlob blockBlob = container.GetBlockBlobReference(id);

            await blockBlob.UploadFromStreamAsync(data, data.Length);
        }
    }

Here is my StorageServices class where I am trying to make use of the second method, "UploadBlogPhoto". The first one works.

Here is the blog controller's method:

[HttpPost]
        public async Task<ActionResult> UploadPhoto(HttpPostedFileBase file)
        {
            if (file != null && file.ContentLength > 0)
            {
                var fileExt = Path.GetExtension(file.FileName);
                if (fileExt.ToLower().EndsWith(".png") || fileExt.ToLower().EndsWith(".jpg") ||
                    fileExt.ToLower().EndsWith(".gif"))
                {
                    var str = "example";
                    await service.UploadBlogPhoto(str, file.InputStream);
                }
            }
            return RedirectToAction("Index");
        }

The view with the script:

 @using (Html.BeginForm("UploadPhoto", "Blog", FormMethod.Post, new { enctype = "multipart/form-data" }))
            {
                <div class="browseimg">
                    <input type="file" class="display-none" name="file" id="files" onchange="this.form.submit()" />
                </div>
            }
            <button class="btn btn-primary width-100p main-bg round-border-bot" id="falseFiles">
                Upload billede
            </button>

 $(document)
       .ready(function () {
           $('#falseFiles')
               .click(function () {
                   $("#files").click();
               });
       });

So the first method uploads to a container called "images" just fine. When I try to simply add another method that uploads to a second container, I get

Object reference not set to an instance of an object.

at

await service.UploadBlogPhoto(str, file.InputStream);

I've triple checked everything, any ideas? Thanks in advance

1
Does a breakpoint inside UploadBlogPhoto get hit?sachin
Yes it does and file.InputStream gets the image succesfully.crystyxn
The error says that the exception is at service.UploadBlogPhoto but if a breakpoint inside that method is being hit, you should be able to pinpoint the exact line by using F10 a few times once you're debugging inside UploadBlogPhoto.sachin
Doesn't seem to do anything else. After that line it goes straight to the error?crystyxn
hmm, BTW, you could create just one method instead of 2 in StorageService and pass a third string parameter for the Container name.sachin

1 Answers

0
votes

The problem was the service was not initialized in the BlogController.

Simply adding a

public BlogController()
        {
            service = new StorageService();
        }

fixed my problem