mysql索引超出范围 index_上传图片路径至数据库,索引超出范围。必须为非负值并小于集合大小。参数名: index...

本文探讨了ASP.NET MVC应用程序中的图片上传问题,详细分析了一段用于添加和保存产品的控制器代码。特别是针对图片文件上传过程中的错误进行了讨论,并提供了修正建议。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Controller的代码:publicActionResultAddProduct(){returnView();}publicActionResultSaveProduct(){Productproduct=newProduct();product.Name=Request.Form["Name"];product.Descr...

Controller的代码:

public ActionResult AddProduct()

{

return View();

}

public ActionResult SaveProduct()

{

Product product = new Product();

product.Name = Request.Form["Name"];

product.Description = Request.Form["Description"];

product.Price =Convert.ToDecimal(Request.Form["Price"]);

string savaPath = HttpContext.Request.MapPath(@"/content/images/");

product.ImageUrl = SaveFile(HttpContext.Request.Files[10], savaPath);

eshop.AddToProduct(product);

eshop.SaveChanges();

return RedirectToAction("ProductList");

}

View里面的代码:

修改商品信息

商品名称:

商品描述:

商品价格:

选择图片:

SaveFiles方法:

public static string SaveFile(HttpPostedFileBase file, string savepath)

{

string filename = "";

if (file.FileName != string.Empty && file.FileName != null)

{

if (file.FileName.LastIndexOf("\\") == -1)

{

filename = file.FileName;

}

else

{

filename = file.FileName.Substring(file.FileName.LastIndexOf("\\") + 1);

}

try

{

file.SaveAs(savepath + filename);

}

catch (HttpException)

{

return "";

}

return filename;

}

else

{

return "";

}

}

问题的提示就在 product.ImageUrl = SaveFile(HttpContext.Request.Files[10], savaPath);这一句,请问到底代表什么意思啊?哪里出错了??求解,必有重谢!

问题中有两处是HttpContext.Request.Files[10],是LZ写错了应该是HttpContext.Request.Files[0]

展开

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值