2011-08-04 82 views
0

我認爲這些標籤幾乎說明我在問什麼。MVC 3文件上傳對話

我一直在努力上傳文件。我需要實現的是打開文件上傳對話框並將其保存到數據庫,所以沒有太多花哨。 基本的文件上傳並不容易。只需使用正確的加密和輸入類型文件即可。但是,當我將表單插入對話框時,出現錯誤,Post中沒有任何內容。我試圖添加像文件名的測試參數,它工作正常。但實際文件在文章中丟失。

這裏的一些代碼:

形式:

@using (Html.BeginForm("Edit", "Home", FormMethod.Post, 
     new { enctype = "multipart/form-data" })){ 
<label for="Name">Filename: </label> 
<input type="text" name="name" id="name"/> 
<input type="file" name="file" id="file" /> 

<input type="submit"/> 
} 

控制器:

public ActionResult Edit(Attachment model) 
    { 
     var strLen = Convert.ToInt32(model.file.InputStream.Length); 
     var strArr = new byte[strLen]; 
     model.file.InputStream.Read(strArr, 0, strLen); 

     return View(); 
    } 

編輯:

型號:

public class Attachment 
{ 
    public string Name { get; set; } 
    public HttpPostedFileBase file{ get; set; } 

} 

此表單位於對話框內。

回答

1

試試這個,

public ActionResult Edit(HttpPostedFileBase file) 
    { 
     //// 
     return View(); 
    } 
+0

哦,這個模型包括這個文件了。問題是POST中沒有文件內容(使用螢火蟲進行檢查) – Roggo