I'm new to programming and just jumped from 1.9 to 2.40. After already trying my best to learn 1.9, I was abruptly slapped with 2.40 and the MVC 3 type of platform it is now using. Not even sure if that is the appropriate term to use. I understand the MVC model, but still have issues with models. Should I just be getting and setting values?
Nonetheless, I am attempting to create a new tab that is found on product edits. I think these are ProductAttributes. I am attempting to create a controller in which an admin can upload files and list the files if they already exist. I've been looking on youtube and so on for tuts on file upload classes. I was just attempting a real simple one at this time to see if it feasible for me. Below is my code:
CSHTML File:
@{
ViewBag.Title = "ProductDocuments";
}
<h2>Product Documents</h2>
@using (Html.BeginForm("Index", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<input type="file" name="file" />
<input type="submit" value="Submit" />
}
Controller File:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using Nop.Admin.Models.Catalog;
using Nop.Core;
using Nop.Core.Domain.Catalog;
using Nop.Core.Domain.Common;
using Nop.Services.Catalog;
using Nop.Services.Common;
using Nop.Services.ExportImport;
using Nop.Services.Localization;
using Nop.Services.Logging;
using Nop.Services.Media;
using Nop.Services.Security;
using Nop.Services.Tax;
using Nop.Web.Framework;
using Nop.Web.Framework.Controllers;
using Nop.Web.Framework.Mvc;
using Telerik.Web.Mvc;
namespace Nop.Admin.Controllers
{
public class ProductDocumentsController : BaseNopController
{
private readonly IImportManager _importManager;
private readonly ICustomerActivityService _customerActivityService;
private readonly IPermissionService _permissionService;
private readonly ILocalizationService _localizationService;
//
// GET: /ProductDocuments/
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult ImportFile(FormCollection form)
{
if (!_permissionService.Authorize(StandardPermissionProvider.ManageCatalog))
return AccessDeniedView();
try
{
var file = Request.Files["file"];
if (file != null && file.ContentLength > 0)
{
var fileBytes = new byte[file.ContentLength];
file.InputStream.Read(fileBytes, 0, file.ContentLength);
//do stuff with the bytes
string fileName = string.Format("{0}.doc", file);
string filePath = string.Format("{0}content\\files\\docs\\{1}", Request.PhysicalApplicationPath, fileName);
System.IO.File.WriteAllBytes(filePath, fileBytes);
_importManager.ImportProductsFromXlsx(filePath);
}
else
{
ErrorNotification("Please upload a file");
return RedirectToAction("List");
}
SuccessNotification(_localizationService.GetResource("Admin.Catalog.Products.Imported"));
return RedirectToAction("List");
}
catch (Exception exc)
{
ErrorNotification(exc);
return RedirectToAction("List");
}
}
public ActionResult List()
{
return View("Index");
}
}
}
Now, most of the controller class I used from the ProductController already from the ImportExcel Method. I thought this would work. However, since this is part of the admin and not supposed to be live, how do I at least view it first, just to see if it works?
From there, I would want to add it to the ProductEdit page where there are several tabs. How would I go about doing that too? I do apologize for the MANY questions. If there is a tut out there or any advice on this, I would greatly appreciate it. nopCommerce is awesome, I would just love to learn how to program for it.
Thanks a bunch!