Reputation: 471
I am working on a FileUpload on azure, and this code really works fine.
string filePath = fileASP.FileName;
string fileType = fileASP.PostedFile.ContentType;
string[] cut = filePath.Split('.');
Array.Reverse(cut);
CloudStorageAccount storageAccount = new CloudStorageAccount(store, blobUri, queueUri, tableUri);
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
CloudBlobContainer container = blobClient.GetContainerReference("mycontainer");
container.CreateIfNotExists();
container.SetPermissions(new BlobContainerPermissions { PublicAccess = BlobContainerPublicAccessType.Blob });
Random rand = new Random();
int randomNum = rand.Next(1, 1000);
CloudBlockBlob blockBlob = container.GetBlockBlobReference(randomNum.ToString() + "." + cut[0]);
txtID.Text = randomNum.ToString();
using (fileASP.PostedFile.InputStream) //System.IO.File.OpenRead(filePath)
{
blockBlob.UploadFromStream(fileASP.PostedFile.InputStream);
}
It works perfectly. But this is only for my dummy program. When I transfer it to my application, I always get the error of Object Reference Not Set to an Instance of an Object
. This is my code for my application:
string filePath = dtiFileUpload.FileName;
string[] cut = filePath.Split('.');
Array.Reverse(cut);
CloudStorageAccount storageAccount = new CloudStorageAccount(wac.Credentials, wac.BlobUri, wac.QueueUri, wac.TableUri);
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
CloudBlobContainer container = blobClient.GetContainerReference("mycontainer");
container.CreateIfNotExists();
Random rand = new Random();
int randomNum1 = rand.Next(1000, 99999);
int randomNum2 = rand.Next(1000, 99999);
string path = "http://cspdemo.blob.core.windows.net/mycontainer/ClearanceDTI-" + randomNum1 + randomNum2 + "." + cut[0];
container.SetPermissions(new BlobContainerPermissions { PublicAccess = BlobContainerPublicAccessType.Blob });
CloudBlockBlob blockBlob = container.GetBlockBlobReference(path);
using (dtiFileUpload.PostedFile.InputStream)
{
blockBlob.UploadFromStream(dtiFileUpload.PostedFile.InputStream);
}
The error is on using (dtiFileUpload.PostedFile.InputStream)
. I've always known this error, and I know that my FileUpload control dtiFileUpload contains a null value that's why it raises an exception. But what is my error? The code is the same with my dummy program, and I don't know on where I am wrong. I have <form id="newForm" method="post" runat="server" enctype="multipart/form-data">
on my master page. Where can i possibly be wrong?
Upvotes: 1
Views: 2760
Reputation: 7005
You have to check the following locations:
dtiFileUpload
cannot be nullfilePath
cannot be nullI recommend you debug the method stepping in line by line and you will get the exact location of your problem, otherwise please post the stack trace of the exception and you should have the line number specified in it (make sure the .pdb files are present in the directory from where you are executing your application)
Upvotes: 1