我正在处理 azure 上的 FileUpload,这段代码真的很好用。
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);
}
它完美地工作。但这仅适用于我的虚拟程序。当我将它转移到我的应用程序时,我总是收到Object Reference Not Set to an Instance of an Object
. 这是我的应用程序代码:
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);
}
错误开启using (dtiFileUpload.PostedFile.InputStream)
。我一直都知道这个错误,而且我知道我的 FileUpload 控件 dtiFileUpload 包含一个空值,这就是它引发异常的原因。但我的错误是什么?代码与我的虚拟程序相同,我不知道我错在哪里。我<form id="newForm" method="post" runat="server" enctype="multipart/form-data">
在我的母版页上有。我哪里可能错了?