@{
var message= " upload your file";
var file=Request.Files["fileUpload"];
var fileName=Path.GetFileName(file.FileName);
message=" you uploded"+fileName;
var filePath=Server.MapPath("~/Exercises/Chapter7/Images");
file.SaveAs(filePath+fileName);
message+="It was saved to"+filePath;
Object reference not set to an instance of an object.
Description:
An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Source Error:
Line 2: var message= " upload your file";
Line 3: var file=Request.Files["fileUpload"];
Line 4: var fileName=Path.GetFileName(file.FileName); Line 5: message=" you uploded"+fileName;
Line 6:
That suggests that there is no file. In the book, the code that gets the file is wrapped in an if block:
@{
var message= " upload your file";
if(IsPost){
var file=Request.Files["fileUpload"];
var fileName=Path.GetFileName(file.FileName);
message=" you uploaded"+fileName;
...etc
That means the code will only execute if the form is submitted. Of course, you will get the same error if you submit the form without uploading a file, but solutions to that problem are detailed later in the book.
Maran
Member
37 Points
24 Posts
upload file with FileUpload helper
Jan 11, 2012 05:57 PM|LINK
Hello Friends,
I am going through Mike's webmatrix Book(page191)
I am trying to upload an image. I am receiving following error? I have check the asp.net site.
couldn't figure it out.
-------------------code --------------------------------
@{
var message= " upload your file";
var file=Request.Files["fileUpload"];
var fileName=Path.GetFileName(file.FileName);
message=" you uploded"+fileName;
var filePath=Server.MapPath("~/Exercises/Chapter7/Images");
file.SaveAs(filePath+fileName);
message+="It was saved to"+filePath;
}
------------------ error -----------------------------------
Server Error in '/' Application.
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Source Error:
Line 2: var message= " upload your file"; Line 3: var file=Request.Files["fileUpload"]; Line 4: var fileName=Path.GetFileName(file.FileName); Line 5: message=" you uploded"+fileName; Line 6:Mikesdotnett...
All-Star
155597 Points
19981 Posts
Moderator
MVP
Re: upload file with FileUpload helper
Jan 11, 2012 07:16 PM|LINK
That suggests that there is no file. In the book, the code that gets the file is wrapped in an if block:
@{ var message= " upload your file"; if(IsPost){ var file=Request.Files["fileUpload"]; var fileName=Path.GetFileName(file.FileName); message=" you uploaded"+fileName; ...etcThat means the code will only execute if the form is submitted. Of course, you will get the same error if you submit the form without uploading a file, but solutions to that problem are detailed later in the book.
Web Pages CMS | My Site | Twitter
Maran
Member
37 Points
24 Posts
Re: upload file with FileUpload helper
Jan 11, 2012 07:34 PM|LINK
Thanks for the Clarification Mike.