2017-02-20 9 views
0

私はこのコードを1つのファイルのアップロードに使用します。asp mvcでjquery ajaxを使って複数のファイルをアップロード

のjQueryのAjax:

$(document).ready(function() { 
    $('input[type=file]').change(function() { 
     $(this).simpleUpload("/Admin/News/GetFile", { 
      start: function (file) { 
       //upload started 
       console.log("upload started"); 
      }, 
      progress: function (progress) { 
       //received progress 
       console.log("upload progress: " + Math.round(progress) + "%"); 
      }, 
      success: function (data) { 
       //upload successful 
       console.log("upload successful!"); 
       console.log(data); 
      }, 
      error: function (error) { 
       //upload failed 
       console.log("upload error: " + error.name + ": " + error.message); 
      } 
     }); 
    }); 
}); 

簡単なアップロードのスクリプト。

コントローラ:

public ActionResult GetFile(HttpPostedFileBase NewsDefaultFile) 
{ 
    //Request.Files[0]; 
    if (NewsDefaultFile != null) 
    { 
     Session.Add("File", NewsDefaultFile); 
    } 
    return Content(""); 
} 

[HttpPost] 
    public ActionResult CreateNews(NewsModel model) 
    { 
     if (model.NewsDefaultFile == null) 
     { 
      var File = (HttpPostedFileBase)Session["File"]; 
      if (File.ContentLength > 0) 
      { 
       var ext = System.IO.Path.GetExtension(File.FileName); 
       if (ext == ".jpg" || ext == ".png" || ext == ".jpeg") 
       { 
        string filename = model.NewsTitle + _NewsClass.Rand(); 
        File.SaveAs(System.IO.Path.Combine(Server.MapPath(@"~/Upload/Image/" + filename))); 
        model.NewsDefaultFile = filename; 
        model.NewsDefaultFileExt = ext; 
       } 
       if (ext == ".webm" || ext == ".mkv" || ext == ".flv" || ext == ".avi" || ext == ".mov" || ext == ".3gp" || ext == ".mp4") 
       { 
        string filename = _NewsClass.Rand() + model.NewsTitle; 
        File.SaveAs(System.IO.Path.Combine(Server.MapPath(@"~/Upload/Video/" + filename))); 
        model.NewsDefaultFile = filename; 
        model.NewsDefaultFileExt = ext; 
       } 
       if (ext == ".mp3" || ext == ".ogg" || ext == "WAV" || ext == ".avi" || ext == ".mov" || ext == ".3gp" || ext == ".mp4") 
       { 
        string filename = _NewsClass.Rand() + model.NewsTitle; 
        File.SaveAs(System.IO.Path.Combine(Server.MapPath(@"~/Upload/Audio/" + filename))); 
        model.NewsDefaultFile = filename; 
        model.NewsDefaultFileExt = ext; 
       } 
      } 
     } 
    } 

は今、私はこれを行う方法を、このコードのuplaod複数のファイルが必要?

答えて

0

のタイプをHttpPostedFileBaseの代わりにList<HttpPostedFileBase>に変更する必要があると思います。

提供されたコードでこれを行うように求められました。

変更GetFileアクションのあなたのコード:

public ActionResult GetFile(HttpPostedFileBase NewsDefaultFile) 
{ 
    //Request.Files[0]; 
    if (NewsDefaultFile != null) 
    { 
     // Retrieve the list from session or create a new one 
     var files = Session["Files"] == null ? 
      new List<HttpPostedFileBase>() : 
      (List<HttpPostedFileBase>)Session["Files"]; 

     // Add the posted file 
     files.Add(NewsDefaultFile); 

     // Store the list in session 
     Session["Files"] = files; 
    } 
    return Content(""); 
} 

そしてCreateNewsアクションのあなたのコードを変更します。それが役に立つかもしれない以下のよう

[HttpPost] 
public ActionResult CreateNews(NewsModel model) 
{ 
    if (model.NewsDefaultFile == null) 
    { 
     if (Session["Files"] != null) 
     { 
      // Retrieve in Session 
      var files = (List<HttpPostedFileBase>)Session["Files"]; 
      var i = 1; // Just to change a bit the filename 

      foreach (var File in files) 
      { 
       if (File.ContentLength > 0) 
       { 
        var ext = System.IO.Path.GetExtension(File.FileName); 
        if (ext == ".jpg" || ext == ".png" || ext == ".jpeg") 
        { 
         // Just added $"_{i}" to avoid overriding saved file 
         string filename = model.NewsTitle + _NewsClass.Rand() + $"_{i}"; 
         File.SaveAs(System.IO.Path.Combine(Server.MapPath(@"~/Upload/Image/" + filename))); 
         model.NewsDefaultFile = filename; 
         model.NewsDefaultFileExt = ext; 
         i++; 
        } 
        if (ext == ".webm" || ext == ".mkv" || ext == ".flv" || ext == ".avi" || ext == ".mov" || ext == ".3gp" || ext == ".mp4") 
        { 
         // Just added $"_{i}" to avoid overriding saved file 
         string filename = _NewsClass.Rand() + model.NewsTitle + $"_{i}"; 
         File.SaveAs(System.IO.Path.Combine(Server.MapPath(@"~/Upload/Video/" + filename))); 
         model.NewsDefaultFile = filename; 
         model.NewsDefaultFileExt = ext; 
         i++; 
        } 
        if (ext == ".mp3" || ext == ".ogg" || ext == "WAV" || ext == ".avi" || ext == ".mov" || ext == ".3gp" || ext == ".mp4") 
        { 
         // Just added $"_{i}" to avoid overriding saved file 
         string filename = _NewsClass.Rand() + model.NewsTitle + $"_{i}"; 
         File.SaveAs(System.IO.Path.Combine(Server.MapPath(@"~/Upload/Audio/" + filename))); 
         model.NewsDefaultFile = filename; 
         model.NewsDefaultFileExt = ext; 
         i++; 
        } 
       } 
      } 
     } 
    } 
} 
+0

私はこのコードを使用し、3ファイル、セッション[「ファイル」]アップロード6ファイルを持っています。どうしたの ? – Kianoush

+0

'GetFile'アクションが同じファイルに対して2回呼び出されていないか確認してください – dbraillon

0

てみてください。

[HttpPost] 
public ActionResult GetFile(List<HttpPostedFileBase> NewsDefaultFile) 
{ 
    if (NewsDefaultFile != null) 
    { 
     Session["Files"] = NewsDefaultFile; 
    }  
    return Content(""); 
} 

[HttpPost] 
public ActionResult CreateNews(NewsModel model) 
{ 
    if (model.NewsDefaultFile == null && Session["Files"] != null) 
    { 
     // Retrieve in Session 
     var files = (List<HttpPostedFileBase>)Session["Files"]; 
     var i = 1; // Just to change a bit the filename 

     foreach (var File in files) 
     { 
      //write your own logic here 
      if (File.ContentLength > 0) 
      { 
       var ext = System.IO.Path.GetExtension(File.FileName); 
       if (ext == ".jpg" || ext == ".png" || ext == ".jpeg") 
       { 
        // Just added $"_{i}" to avoid overriding saved file 
        string filename = model.NewsTitle + _NewsClass.Rand() + $"_{i}"; 
        File.SaveAs(System.IO.Path.Combine(Server.MapPath(@"~/Upload/Image/" + filename))); 
        model.NewsDefaultFile = filename; 
        model.NewsDefaultFileExt = ext; 
        i++; 
       } 
       if (ext == ".webm" || ext == ".mkv" || ext == ".flv" || ext == ".avi" || ext == ".mov" || ext == ".3gp" || ext == ".mp4") 
       { 
        // Just added $"_{i}" to avoid overriding saved file 
        string filename = _NewsClass.Rand() + model.NewsTitle + $"_{i}"; 
        File.SaveAs(System.IO.Path.Combine(Server.MapPath(@"~/Upload/Video/" + filename))); 
        model.NewsDefaultFile = filename; 
        model.NewsDefaultFileExt = ext; 
        i++; 
       } 
       if (ext == ".mp3" || ext == ".ogg" || ext == "WAV" || ext == ".avi" || ext == ".mov" || ext == ".3gp" || ext == ".mp4") 
       { 
        // Just added $"_{i}" to avoid overriding saved file 
        string filename = _NewsClass.Rand() + model.NewsTitle + $"_{i}"; 
        File.SaveAs(System.IO.Path.Combine(Server.MapPath(@"~/Upload/Audio/" + filename))); 
        model.NewsDefaultFile = filename; 
        model.NewsDefaultFileExt = ext; 
        i++; 
       } 
      } 
     }  
    } 
} 
関連する問題