當我想嘗試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);
}
要正確設置代碼格式有多困難?如果您需要幫助,請讓我們輕鬆閱讀您的代碼。 – Oded 2011-05-18 21:05:22
有什麼建議嗎? – Arbelac 2011-05-23 08:09:28
只有你閱讀[markdown編輯幫助](http://stackoverflow.com/editing-help)部分(或點擊編輯框右上方的小'?'。 – Oded 2011-05-23 08:32:58