2011-06-22 4 views
0

jqueryformプラグインを使用してファイルをアップロードしようとしています。私はアップロードコントロールをファイルしなければならないが、2番目のものはアップロードできない?jqueryformプラグインでアップロード

<div> 
    <h2> 
     Upload test</h2> 
    <script src="../../Scripts/jquery-1.5.1.min.js" type="text/javascript"></script> 
    <script src="../../Scripts/jquery-ui-1.8.12.custom.min.js" type="text/javascript"></script> 
    <script src="../../Scripts/jqueryform.js" type="text/javascript"></script> 
    <script src="../../Scripts/jblock.js" type="text/javascript"></script> 
    <script type="text/javascript"> 

     $(document).ready(function (event) { 

      $(function() { 
       $("#ajaxUploadForm").ajaxForm({ 
        iframe: true, 
        dataType: "json", 
        beforeSubmit: function() { 
         $("#ajaxUploadForm").block({ message: ' Uploading Image' }); 
        }, 
        success: function (result) { 
         $("#ajaxUploadForm").unblock(); 
         $("#ajaxUploadForm").resetForm(); 
         //$.growlUI(null, result.message); 
         if (result.message != 'Success') { 
          alert(result.message); 
         } 
         else { 

         } 
        }, 
        error: function (xhr, textStatus, errorThrown) { 
         $("#ajaxUploadForm").unblock(); 
         $("#ajaxUploadForm").resetForm(); 

        } 
       }); 
      }); 
     }); 
    </script> 
    <form id="ajaxUploadForm" action="<%= Url.Action("Upload", "Home")%>" method="post" 
    enctype="multipart/form-data"> 
    <input type="file" name="file" /> 

    <input type="file" name="file2" /> 

    <input id="ajaxUploadButton" type="submit" value="upload file" /> 
    </form> 
</div> 


    public ActionResult Index() 
    { 
     return View(); 
    } 

    public FileUploadJsonResult Upload(HttpPostedFileBase file) 
    { 
     if (file == null) 
     { 
      return new FileUploadJsonResult { Data = new { message = "error" } }; 
     } 

     if (file.ContentLength > 0) 
     { 
     //save file or something 
     } 

     return new FileUploadJsonResult { Data = new { message = string.Format("success") } }; 
    } 


public class FileUploadJsonResult : JsonResult 
{ 
    public override void ExecuteResult(ControllerContext context) 
    { 
     this.ContentType = "text/html"; 
     context.HttpContext.Response.Write("<textarea>"); 
     base.ExecuteResult(context); 
     context.HttpContext.Response.Write("</textarea>"); 
    } 
} 

答えて

2

あなたは、それぞれfilefile1という名前のフォーム上の2つのファイルの入力を備えています。アップロードを処理するコントローラーアクションには、HttpPostedFileBaseという単一の引数(file)しかありません。

public FileUploadJsonResult Upload(
    HttpPostedFileBase file, 
    HttpPostedFileBase file1 
) 
{ 
    if (file == null || file1 == null) 
    { 
     return new FileUploadJsonResult { Data = new { message = "error" } }; 
    } 

    if (file.ContentLength > 0) 
    { 
     //save file or something 
    } 

    if (file1.ContentLength > 0) 
    { 
     //save the second file or something 
    } 

    return new FileUploadJsonResult { Data = new { message = string.Format("success") } }; 
} 

するか、あなたはあなたのフォームに同じ名前を与えることができ、複数のファイルを処理したい場合:だから、第二1追加することができ

<input type="file" name="files" /> 
<input type="file" name="files" /> 
<input type="file" name="files" /> 
<input type="file" name="files" /> 
<input type="file" name="files" /> 
... 

とあなたのコントローラのアクションは、リストを取ることができますファイルの:

public FileUploadJsonResult Upload(IEnumerable<HttpPostedFileBase> files) 
{ 
    if (files) 
    { 
     return new FileUploadJsonResult { Data = new { message = "error" } }; 
    } 

    foreach (var file in files) 
    { 

     if (file.ContentLength > 0) 
     { 
      //save file or something 
     } 
    } 

    return new FileUploadJsonResult { Data = new { message = string.Format("success") } }; 
} 

あなたは、ASP.NET MVCでファイルのアップロードについてfollowing blog postをチェックアウトすることがあります。

関連する問題