问题
Hopefully I can get some headway here as nopCommerce forums has been silent to my post. My current situation is that for each product we have in our store, we(admins) need to upload a specific document and have that document show to the end user when they are browsing the product detail section, through means of a link and download.
So I figured I would chop this project up and first attempt to develop the upload function from the admin area.
In case anyone else can help but doesn't know nopCommerce, it is an ASP.NET MVC 3 project. For those who have nopCommerce already, please look below on how to navigate and add my code to the specific files.
1.How to add a tab to Product Edit:
a.Inside Nop.Admin
i.Navigate to Views -> _CreateOrUpdate.cshtml
b.Add TabPanel after line 24
x.Add().Text(T("Admin.Catalog.Products.ProductDocuments").Text).Content(TabProductDocuments().ToHtmlString());
c.Create ‘TabProductDocuments’ help method on line 772
@helper TabProductDocuments()
{
if (Model.Id > 0)
{
<h2>Product Documents</h2>
<form action="" method="post" enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file" />
<input type="submit" />
</form>
}
else
{
@T("Admin.Catalog.Products.ProductDocuments.SaveBeforeEdit")
}
}
d.Change ProductDocumentsController.cs to more simple code:
public class ProductDocumentsController : BaseNopController
{
[HttpPost]
public ActionResult Index(HttpPostedFileBase file)
{
if (file.ContentLength > 0)
{
var fileName = Path.GetFileName(file.FileName);
var path = Path.Combine(HttpContext.Server.MapPath("../Content/files/uploads"), fileName);
file.SaveAs(path);
}
return RedirectToAction("Index");
}
Now, the issue I am experiencing is: I can see the tab now in Product Edit, but I cannot upload the file. It submits the query but just refreshes the page and leads back to Product List. No file is uploaded. If you can, please assist me with trying to upload the file properly to the path I have designated. Thank you again for your assistance.
I've already tried from scratch an upload project and it does work properly, but for some reason, here, it just isn't working.
回答1:
You probably need an Action url in the form's action parameter.
<form action="/Admin/Product/Upload/555" method="post" enctype="multipart/form-data">
And rename your Action method to match
[HttpPost]
public ActionResult Upload(int productId, HttpPostedFileBase file)
{
if (file.ContentLength > 0)
{
var fileName = Path.GetFileName(file.FileName);
var path = Path.Combine(HttpContext.Server.MapPath("../Content/files/uploads"), fileName);
file.SaveAs(path);
//attach the file to the product record in the database
}
return RedirectToAction("Index");
}
回答2:
Probably a bit late but I did a plugin for this. Can be found on github
https://github.com/johanlasses/nopcommerce-product-files-plugin
来源:https://stackoverflow.com/questions/9791635/nopcommerce-2-40-admin-product-edit-addition