I have a scenario where in user uploads different products ( of different brands etc). Whenever a new product is uploaded, I want images to be saved in ~Images/Brands/ProductID/ProductID.jpg.
Adding files to existing folder is hassel free. However, for a new Brand or ProductID folders need to be created. Image to be uploaded in the respective folder and both the folder and image to be included in the solution. All this through code. Here is
what I have.
When ever I save file, I get either "Could not find a part of the path C#" or "Directory.CreateDirectory(folder);" gets executed
but no folder is created.
public ActionResult Create(FormCollection fc, HttpPostedFileBase file)
{
string extension = Path.GetExtension(file.FileName);
string subdir = fc["ModelNumber"].ToString();
// Verification
if (ModelState.IsValid)
{
if (file != null && file.ContentLength > 0)
{
string folder = @"~\Images\ProductImages\"+fc["BrandName"].ToString()+fc["ProductID"].ToString();
string path = Path.Combine(folder, fc["ProductID"].ToString() + extension); if(!Directory.Exists(folder)) Directory.CreateDirectory(folder); // Creating Folder Structure like "Images/ProductImages/BrandName/ProductID"
file.SaveAs(Server.MapPath(path));// PROBLEM OCCURS HERE
if (AddProduct(fc))// This function save values to db. This is fine
{
return RedirectToAction("Index", "Products");
}
}
else
{
ViewBag.Message = "Input a file.";
return View();
}
}
return View();
}
Next, If I upload files to an existing folder, I want them to be included directly.. Can someone help?
The only way to get smarter is by playing a smarter opponent.
System.Web.HttpException --- is a physical path, but a virtual path was expected this is the exception I get
You should have only one Server.MapPath. Again, the issue is you are using System.IO which requires standard file paths not virtual.
string folder = @"~\Images\ProductImages\"+fc["BrandName"].ToString()+fc["ProductID"].ToString();
folder = Server.MapPath(folder);
string path = Path.Combine(folder, fc["ProductID"].ToString() + extension);
if(!Directory.Exists(folder))
Directory.CreateDirectory(folder); // Creating Folder Structure like "Images/ProductImages/BrandName/ProductID"
file.SaveAs(path);// PROBLEM OCCURS HERE
if (AddProduct(fc))// This function save values to db. This is fine
{
return RedirectToAction("Index", "Products");
}
}
Member
281 Points
549 Posts
File upload Directory / file include in solution
Apr 13, 2019 02:02 PM|N1ZAM|LINK
Hi,
I have a scenario where in user uploads different products ( of different brands etc). Whenever a new product is uploaded, I want images to be saved in ~Images/Brands/ProductID/ProductID.jpg.
Adding files to existing folder is hassel free. However, for a new Brand or ProductID folders need to be created. Image to be uploaded in the respective folder and both the folder and image to be included in the solution. All this through code. Here is what I have.
When ever I save file, I get either "Could not find a part of the path C#" or "Directory.CreateDirectory(folder);" gets executed but no folder is created.
Next, If I upload files to an existing folder, I want them to be included directly.. Can someone help?
NIZAM
All-Star
52231 Points
23301 Posts
Re: File upload Directory / file include in solution
Apr 13, 2019 02:46 PM|mgebhard|LINK
This sytanx is incorrect. The tilde is an ASP.NET construct not System.IO.
Use Server.MapPath to convert the ASP.NET path to a proper file path.
Or take a look at the docs.
https://docs.microsoft.com/en-us/dotnet/api/system.web.ui.webcontrols.fileupload?view=netframework-4.7.2
Member
281 Points
549 Posts
Re: File upload Directory / file include in solution
Apr 13, 2019 03:34 PM|N1ZAM|LINK
System.Web.HttpException --- is a physical path, but a virtual path was expected this is the exception I get
NIZAM
All-Star
52231 Points
23301 Posts
Re: File upload Directory / file include in solution
Apr 13, 2019 05:54 PM|mgebhard|LINK
You should have only one Server.MapPath. Again, the issue is you are using System.IO which requires standard file paths not virtual.
All-Star
57874 Points
15510 Posts
Re: File upload Directory / file include in solution
Apr 13, 2019 10:20 PM|bruce (sqlwork.com)|LINK
you have your slashes in the wrong direction for virtual folder
All-Star
52523 Points
15675 Posts
Re: File upload Directory / file include in solution
Apr 15, 2019 01:06 AM|oned_gk|LINK
AFAIK, you can use Server.MapPath for existing file/folder only, you can't use it for not exist file or folder.
Create ProductImages folder first in the website, get the path using server.MapPath
Suwandi - Non Graduate Programmer