简体   繁体   English

asp.net-未设置FileUpload对象参考

[英]asp.net - FileUpload Object Reference Not Set

I am working on a FileUpload on azure, and this code really works fine. 我正在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);
            }

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 . 当我将其传输到应用程序时,总是会收到“ 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) . 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. 我一直都知道这个错误,而且我知道我的FileUpload控件dtiFileUpload包含一个空值,这就是它引发异常的原因。 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. 我的<form id="newForm" method="post" runat="server" enctype="multipart/form-data"><form id="newForm" method="post" runat="server" enctype="multipart/form-data"> Where can i possibly be wrong? 我在哪里可能错了?

You have to check the following locations: 您必须检查以下位置:

  • First line: dtiFileUpload cannot be null 第一行: dtiFileUpload不能为null
  • Second line: filePath cannot be null 第二行: filePath不能为null

I 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) 我建议您逐行调试该方法,您将获得问题的确切位置,否则请发布异常的堆栈跟踪,并且应该在其中指定行号(确保存在.pdb文件)在执行应用程序的目录中)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM