Właśnie jeszcze ni jak bo ale kombinuje co zrobić :P
Mam tak w widoku inputa do uploadowania plików:
<div class="form-group">
@Html.LabelFor(model => model.ProductImageUrl, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.TextBoxFor(m => m.ProductImageUrl, new { type = "file", enctype = "multipart/form-data" })
@Html.ValidationMessageFor(model => model.ProductImageUrl, "", new { @class = "text-danger" })
</div>
</div>
Wczytuje sobie obrazek, niby ok. Ale jak potem go przekazać do root directory np do ~/Content/Images/Products a w bazie przechować ścieżkę do tego obrazka dla nowego produktu?
W akcji kombinuje coś takiego:
[HttpPost]
public ActionResult AddNewProduct(NewProductViewModel newProd, HttpPostedFileBase file)
{
using (var db = new ApplicationDbContext())
{
var product = new Product
{
ProductName = newProd.ProductName,
ProductImageUrl = newProd.ProductImageUrl,
Category = _categoryService.GetAllCategories().ToList(),
ProductDescription = newProd.ProductDescription,
PricePerUnit = newProd.ProductPrice,
ProductQuantity = newProd.ProductQuantity,
PremiumPoints = newProd.PremiumPoints,
NutritionalValues = new NutritionalValue
{
Calorific = newProd.ProductCalorific,
Carbohydrate = newProd.ProductCarbohydrates,
Protein = newProd.ProductProteins,
Fat = newProd.ProductFat
},
Suplier = new Suplier
{
CompanyName = newProd.ProductSuplierName,
Address = new Collection<Addresses>
{
new Addresses()
{
Country = newProd.ProductSuplierCountry,
City = newProd.ProductSuplierCity,
StreetName = newProd.ProductSuplierStreetName,
PostalCode = newProd.ProductSuplierPostalCode,
FlatNumber = newProd.ProductSuplierFlatNumber
}
}
}
};
if (file != null)
{
var fileName = Path.GetFileName(file.FileName);
if (fileName != null)
{
try
{
var path = Path.Combine(Server.MapPath("~/Content/Images/Products/"), fileName);
file.SaveAs(path);
_productService.InsertProduct(product);
return RedirectToAction("Index", "Home");
}
catch (Exception ex)
{
ViewBag.Message = "Bład!" + ex.Message;
}
}
else
{
ViewBag.Message = "Nie udało się załadować pliku!";
}
}
else
{
ViewBag.Message = "Wybierz odpowiedni plik";
}
}
return View();
}
Czyli w akcji przekazuje sobie jako drugi parametr ten plik, tylko coś mi to nie działa :P