2011-05-16 126 views
0

我想在我的MVC項目中創建一個文件上傳頁面。首先,我想在本地進行管理。 我的問題是: 1-這是我的控制器和視圖。有什麼我必須做的,使這個代碼的工作?我的意思是定義一個模型或使用jquery等等。文件上傳過程是什麼?在Asp.Net文件上傳MVC

[HttpPost] 
public ActionResult FileUpload(HttpPostedFileBase uploadFile) 
{ 
if (uploadFile.ContentLength > 0) 
      { 
       string filePath = Path.Combine(HttpContext.Server.MapPath("C:/Users/marti/../PhotoGallery/myimages"), 
       Path.GetFileName(uploadFile.FileName)); 
       uploadFile.SaveAs(filePath); 
      } 
      return View(); 
} 

這裏查看:

<input name="uploadFile" type="file" /> 
<input type="submit" value="Upload File" /> 

2 - 當我調試此,它從來就沒有到控制器。

+0

不確定的問題。什麼不行?或者你只是想要一個文件上傳進度? – Craig 2011-05-16 06:19:25

+0

您需要使用Html表格'

'標籤通過發佈請求提交,並接受控制器中的「FormCollection集合」。 – 2011-05-16 06:21:02

+0

是的,我需要文件上傳進度。 – Mgnfcnt 2011-05-16 06:36:03

回答

0

您可能需要enctype='multipart/form-data'視圖形式:

@model ImageModel 
    @{ 
     ViewBag.Title = "New Image"; 
    } 
    <div class="content-form-container width-half"> 
     <form id='PropertiesForm' action='@Url.Action(ImageController.Actions.Add, ImageController.Name)' method='post' enctype='multipart/form-data' class='content-form'> 
     @Html.Partial("ImageName") 
     <fieldset class='content-form-1field'> 
      <div class='legend'> 
       file to upload 
      </div> 
      @Html.LabelledFileInput(ImageView.FileName, string.Empty) 
     </fieldset> 
     <div class='buttons'> 
      @Html.Button("button-submit", "submit") 
     </div> 
     </form> 
    </div> 
    @section script{ 
     @Html.JavascriptInclude("~/js/image/new.min.js") 
    } 

這裏是我的控制器代碼:

[HttpPost] 
    [MemberAccess] 
    public ActionResult Add() 
    { 
     var name = ImageView.ImageName.MapFrom(Request.Form); 

     if (Request.Files.Count == 0) 
     { 
      RegisterFailureMessage("No file has been selected for upload."); 

      return ValidationFailureAdd(name); 
     } 

     var file = Request.Files[0]; 

     if (file == null || file.ContentLength == 0) 
     { 
      RegisterFailureMessage("No file has been selected for upload or the file is empty."); 

      return ValidationFailureAdd(name); 
     } 

     var format = ImageService.ImageFormat(file.InputStream); 

     if (format != ImageFormat.Gif && format != ImageFormat.Jpeg && format != ImageFormat.Png) 
     { 
      RegisterFailureMessage("Only gif, jpg and png files are supported."); 

      return ValidationFailureAdd(name); 
     } 

     if (query.HasName(name)) 
     { 
      RegisterFailureMessage(string.Format("Image with name '{0}' already exists.", name)); 

      return ValidationFailureAdd(name); 
     } 

     using (var scope = new TransactionScope()) 
     { 
      var id = Guid.NewGuid(); 

      var fileExtension = ImageService.FileExtension(format); 

      Bus.Send(new AddWikiImageCommand 
        { 
         Id = id, 
         Name = name, 
         FileExtension = fileExtension 
        }); 

      var path = Path.Combine(ApplicationConfiguration.MediaFolder, 
            string.Format("{0}.{1}", id.ToString("n"), fileExtension)); 

      if (System.IO.File.Exists(path)) 
      { 
       System.IO.File.Delete(path); 
      } 

      file.SaveAs(path); 

      scope.Complete(); 
     } 

     return RedirectToAction(Actions.Manage); 
    } 

裏面還有一些定製位,所以你可以忽略這些。你需要的東西應該在那裏。

HTH

+0

@using(Html.BeginForm(「上傳」,「家」,FormMethod.Post,新{@ enctype =「multipart/form-data」}))我sude這個,它的作品。 – Mgnfcnt 2011-05-16 06:28:11

+0

@MgnFcnt:是的,你可以:) ---雖然不會買太多。我*做*有我自己的Html.ContentForm實現,當我有典型的數據輸入表單時爲我添加一些東西。但Html.BeginForm絕對是一種選擇。 – 2011-05-16 06:33:35

+0

感謝您的幫助。祝你好運。 – Mgnfcnt 2011-05-16 07:16:49