2012-03-18 1 views
2

ファイルをアップロードする予定のasp.netでアプリケーションを開発していますが、enctype = "multipart/form-data"を使用するとフォームコレクションは空です私はenctypeを使用していませんが、フォームコレクションにはファイルをアップロードするのに、Request.Files.count = 0という名前が付いています。ファイルのアップロードとフォームコレクションでのアップロードファイルの名前を取得したい。どんな解決策ですか?フォームのコレクションが空です。ファイルのアップロードにenctypeを使用すると

答えて

0

次は私のために正常に動作します:ファイル入力ファイルをフェッチするために、後にコントローラのアクションで使用されるnameを持たなければならないこと

@using (Html.BeginForm("someaction", "somecontroller", FormMethod.Post, new { enctype = "multipart/form-data" })) 
{ 
    <input type="file" name="file" /> 
    <button type="submit">Upload</button> 
} 

は注意してください。 Request.File.Count = 0を取得しているという事実は、あなたが入力フィールドに名前を指定しなかったことを強く示しています。

とアクション:

[HttpPost] 
public ActionResult SomeAction(HttpPostedFileBase file) 
{ 
    if (file != null && file.ContentLength > 0) 
    { 
     var filename = Path.GetFileName(file.FileName); 
     filename = Path.Combine(Server.MapPath("~/uploads"), filename); 
     file.SaveAs(filename); 
    } 
    return View(); 
} 

、あなたは(私はお勧めしません)FormCollectionを使用したい場合:

[HttpPost] 
public ActionResult SomeAction() 
{ 
    var file = Request.Files["file"]; 
    if (file != null && file.ContentLength > 0) 
    { 
     var filename = Path.GetFileName(file.FileName); 
     filename = Path.Combine(Server.MapPath("~/uploads"), filename); 
     file.SaveAs(filename); 
    } 
    return View(); 
} 

またfollowing blog postをチェックアウトすることがあります。

1

次のコードをチェックアウト:

は、コードに従うことによって、ビューの形式でエンコードの種類を追加します。

@using (Html.BeginForm("Create", "Employees", FormMethod.Post,new{ enctype="multipart/form-data"})) 
{ 
    @Html.TextBoxFor(model => model.Name) 

    @Html.TextBoxFor(model => model.Resume, new { type = "file" }) 

    <p> 
    <input type="submit" value="Save" /> 
    </p> 
@Html.ValidationSummary() 
} 

は、お使いのコントローラのあなたのそれぞれのアクションで

[HttpPost] 
public ActionResult Create(EmployeeViewModel viewModel) 
{ 
     if (Request.Files.Count > 0) 
     { 
      foreach (string file in Request.Files) 
      { 
       string pathFile = string.Empty; 
       if (file != null) 
       { 
        string path = string.Empty; 
        string fileName = string.Empty; 
        string fullPath = string.Empty; 
        path = AppDomain.CurrentDomain.BaseDirectory + "directory where you want to upload file";//here give the directory where you want to save your file 
        if (!System.IO.Directory.Exists(path))//if path do not exit 
        { 
         System.IO.Directory.CreateDirectory(AppDomain.CurrentDomain.BaseDirectory + "directory_name/");//if given directory dont exist, it creates with give directory name 
        } 
        fileName = Request.Files[file].FileName; 

        fullPath = Path.Combine(path, fileName); 
        if (!System.IO.File.Exists(fullPath)) 
        { 

         if (fileName != null && fileName.Trim().Length > 0) 
         { 
          Request.Files[file].SaveAs(fullPath); 
         } 
        } 
       } 
      } 
     } 
} 
を次のコードを追加します。

私はアサーションされたパスがベースディレクトリのディレクトリの中にあります....ファイルを保存したい場所に自分のパスを与えることができます

関連する問題