2011-05-18 116 views
1

當我想嘗試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); 

     } 
+1

要正確設置代碼格式有多困難?如果您需要幫助,請讓我們輕鬆閱讀您的代碼。 – Oded 2011-05-18 21:05:22

+0

有什麼建議嗎? – Arbelac 2011-05-23 08:09:28

+0

只有你閱讀[markdown編輯幫助](http://stackoverflow.com/editing-help)部分(或點擊編輯框右上方的小'?'。 – Oded 2011-05-23 08:32:58

回答

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

+0

@Darin Dimitrov-我不能安裝一個擴展名爲.flv的文件,但是我可以安裝一個擴展名爲.jpeg的文件。 – Arbelac 2011-05-19 19:00:58

+0

@Arbelac,你是什麼意思?*安裝.flv擴展文件*? – 2011-05-19 19:01:52

+0

@ Darin Dimitrov - 所以我不能上傳一個.flv擴展名的文件。如果我更改爲同一個文件的.jpeg擴展名,它工作。 – Arbelac 2011-05-19 19:17:04

1

我不把該文件作爲一個Action參數,我通常只是從請求中提取它。我想通過一個html表單發送文件,我想呢?

public ActionResult FileUpload() 
{ 

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

    } 

    return 
}