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
UploadBlogPhoto
get hit? – sachinservice.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 insideUploadBlogPhoto
. – sachin