1

When I want to try the file upload in ASP.Net MVC,I am receive the following 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 21:         public ActionResult FileUpload(HttpPostedFileBase uploadFile)
Line 22:         {
Line 23:            if (uploadFile.ContentLength > 0)
Line 24:            {
Line 25:              string filePath =  Path.Combine(HttpContext.Server.MapPath
("../../Tarifler/Videolar/"),
Source File: D:\yemekizle\yemekizle\Controllers\FileUploadController.cs  Line: 23 

Here is my code line.

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %> 

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">     
    FileUpload
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">  
    <h2>FileUpload</h2>        
    <% using (Html.BeginForm("FileUpload", "FileUpload", FormMethod.Post, new { enctype = "multipart/form-data" })) { %>        
        <input name="uploadFile" type="file" />      
        <input type="submit" value="Upload File" />
    <% } %>
</asp:Content>

Controller:

[HandleError]
public class FileUploadController : Controller
{    
    public ActionResult FileUpload()    
    { 
         return View();    
    }   

    [AcceptVerbs(HttpVerbs.Post)]    
    public ActionResult FileUpload(HttpPostedFileBase uploadFile)    
    {  
        if (uploadFile.ContentLength > 0)      
        {
            string filePath = Path.Combine(
                HttpContext.Server.MapPath("../Uploads"), 
                Path.GetFileName(uploadFile.FileName)
            );          
            uploadFile.SaveAs(filePath);    
        }      
        return View();    
    }
}

Where I wrong?

Thanks,


 [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Upload(HttpPostedFileBase uploadFile)
    {
        if (uploadFile.ContentLength > 0)
        {

          string filePath = 
     Path.Combine(HttpContext.Server.MapPath("~/resim"),      Path.GetFileName
      (uploadFile.FileName));
            uploadFile.SaveAs(filePath);

        }
1
  • Only that you read the markdown editing help section (or click the little ? at top right of the edit box. Commented May 23, 2011 at 8:32

2 Answers 2

3

Ensure that the user has selected a file by checking if uploadFile is not null before processing it:

[HttpPost]    
public ActionResult FileUpload(HttpPostedFileBase uploadFile)    
{  
    if (uploadFile != null && uploadFile.ContentLength > 0)      
    {
        string filePath = Path.Combine(
            Server.MapPath("~/Uploads"), 
            Path.GetFileName(uploadFile.FileName)
        );          
        uploadFile.SaveAs(filePath);    
    }     
    return View();   
}

Also ensure you have read the following blog post.

Sign up to request clarification or add additional context in comments.

6 Comments

@Darin Dimitrov- I can't install a file that is .flv extension ,but I can install a file that is .jpeg extension.
@Arbelac, what do you mean by installing a .flv extension file?
@ Darin Dimitrov - So, I can't upload a .flv extension file.if I change as .jpeg extension of the the same file,it's working.
@Arbelac, what does can't mean? Error message? Something else?
@Darin Dimitrov - Here is my error message. ressim.net/s/upload/82db7d36.jpg ressim.net/s/upload/9bafcde2.jpg
|
1

I don't take the file as an Action parameter, I usually just pull it from the request. You're sending the file via a html form post, I presume?

public ActionResult FileUpload()
{

    foreach(var file in Request.Files)
    {
         //do something

    } 

    return
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.