2017-04-08 88 views
2

我目前正在學習使用asp.net mvc5開發網絡,現在正在開展我的學校項目。 。想存儲和數據庫顯示圖像我遇到這個錯誤「索引超出範圍,必須爲非負值,小於集合的大小。 r n參數名稱:索引

here is the screenshot

這裏是我的代碼,這是控制器:

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

[HttpPost] 
public ActionResult AddItems(FormCollection form) 
{ 
    StoreItems i = new StoreItems(); 
    //i.ID = int.Parse(form["AlbumID"]); 
    i.AlbumName = form["AlbumName"]; 
    i.Artist = form["AlbumArtist"]; 
    i.Genre = form["AlbumGenre"]; 
    i.DateReleased = DateTime.Parse(form["AlbumDateReleased"]); 
    i.Price = int.Parse(form["AlbumPrice"]); 
    i.Downloads = int.Parse(form["AlbumDownloads"]); 
    i.Listens = int.Parse(form["AlbumListens"]); 
    i.RecordLabel = form["RecordLabel"]; 
    HttpPostedFileBase file = Request.Files[0]; 
    i.PicturePath = file.FileName.ToString(); 

    DAL.AddItems(i); 
    return RedirectToAction("ItemLists"); 
} 

這裏是模型:

public static void AddItems(StoreItems i) 
{ 

    byte[] bytes; 
    if (string.IsNullOrEmpty(i.PicturePath)) 
    { 
     string filename = System.Web.HttpContext.Current.Server.MapPath("~/Content/Images/default-artwork.png"); 
     bytes = System.IO.File.ReadAllBytes(filename); 
    } 

    else 
    { 
     string filename = i.PicturePath; 
     bytes = System.IO.File.ReadAllBytes(filename); 


    } 


    SqlConnection con = new SqlConnection(DAL.cs); 
    con.Open(); 
    SqlCommand com = new SqlCommand(
     "INSERT INTO AlbumsTb (AlbumName, Artist, Genre, DateReleased, Price, Downloads, Listens, RecordLabel, DateAdded, AlbumArt) VALUES(@AlbumName, @Artist, @Genre, @DateReleased, @Price, @Downloads, @Listens, @RecordLabel, @DateAdded, @AlbumArt)", con); 
    //com.Parameters.AddWithValue("@ID", i.ID); 
    com.Parameters.AddWithValue("@AlbumName", SqlDbType.VarChar).Value = i.AlbumName; 
    com.Parameters.AddWithValue("@Artist", SqlDbType.VarChar).Value = i.Artist; 
    com.Parameters.AddWithValue("@Genre", SqlDbType.VarChar).Value = i.Genre; 
    com.Parameters.AddWithValue("@DateReleased", SqlDbType.Date).Value = i.DateReleased; 
    com.Parameters.AddWithValue("@Price",i.Price); 
    com.Parameters.AddWithValue("@Downloads", i.Downloads); 
    com.Parameters.AddWithValue("@Listens", i.Listens); 
    com.Parameters.AddWithValue("@RecordLabel", SqlDbType.VarChar).Value = i.RecordLabel; 
    com.Parameters.AddWithValue(@"DateAdded", DateTime.Now.ToString()); 

    com.Parameters.AddWithValue("@AlbumArt", SqlDbType.VarBinary).Value = bytes; 

    com.ExecuteNonQuery(); 
    con.Close(); 

} 
+0

你是如何發佈文件從客戶端到服務器? –

+2

此錯誤背後的問題是請求沒有任何文件。 –

+0

@ChetanRanpariya我的想法確切 - 應該可以添加爲答案。 – EJoshuaS

回答

2

沒有與請求一起發佈的文件。這意味着陣列是空的。因此索引超出範圍錯誤。您還應該練習防禦性編碼並檢查以確保數組已填充。如果文件是強制性的,那麼你可以優雅地錯誤並返回相關的錯誤信息(BadRequest..etc)

[HttpPost] 
public ActionResult AddItems(FormCollection form) 
{ 
    StoreItems i = new StoreItems(); 
    //i.ID = int.Parse(form["AlbumID"]); 
    i.AlbumName = form["AlbumName"]; 
    i.Artist = form["AlbumArtist"]; 
    i.Genre = form["AlbumGenre"]; 
    i.DateReleased = DateTime.Parse(form["AlbumDateReleased"]); 
    i.Price = int.Parse(form["AlbumPrice"]); 
    i.Downloads = int.Parse(form["AlbumDownloads"]); 
    i.Listens = int.Parse(form["AlbumListens"]); 
    i.RecordLabel = form["RecordLabel"]; 
    var files = Request.Files; 
    if(files.Count > 0) { 
     var file = files[0]; 
     i.PicturePath = file.FileName.ToString(); 
    } else { 
     //...return some error code or validation message. 
    } 

    DAL.AddItems(i); 
    return RedirectToAction("ItemLists"); 

} 
相關問題