ASP.Net MVC文件上传问题

时间:2011-05-18 21:01:40

标签: asp.net-mvc

当我想在ASP.Net MVC中尝试上传文件时,我收到以下错误。

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 

这是我的代码行。

<%@ 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>

控制器:

[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();    
    }
}

哪里错了?

谢谢,


 [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);

        }

2 个答案:

答案 0 :(得分:3)

确保用户在处理文件之前检查uploadFile是否为空来选择文件:

[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();   
}

另外,请确保您已阅读following blog post

答案 1 :(得分:1)

我不将文件作为Action参数,我通常只是从请求中提取它。你推测,你是通过html表格发送文件的吗?

public ActionResult FileUpload()
{

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

    } 

    return
}