locked
File upload Directory / file include in solution RRS feed

  • Question

  • User739135361 posted

    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. 

     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?

    Saturday, April 13, 2019 2:02 PM

All replies

  • User475983607 posted

    This sytanx is incorrect.  The tilde is an ASP.NET construct not System.IO.

                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);

    Use Server.MapPath to convert the ASP.NET path to a proper file path.

    string folder = @"~\Images\ProductImages\"+fc["BrandName"].ToString()+fc["ProductID"].ToString();  
    folder = Server.MapPath(folder);

    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

    Saturday, April 13, 2019 2:46 PM
  • User739135361 posted

    System.Web.HttpException --- is a physical path, but a virtual path was expected this is the exception I get

    Saturday, April 13, 2019 3:34 PM
  • User475983607 posted

    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");
       }
    		
    }

    Saturday, April 13, 2019 5:54 PM
  • User-474980206 posted

    you have your slashes in the wrong direction for virtual folder

    var folder = Server.MapPath(@"~/Images/ProductImages/"
    if(!Directory.Exists(folder))  
        Directory.CreateDirectory(folder); 
    
    folder += "\\" + fc["BrandName"].ToString();
    if(!Directory.Exists(folder))  
        Directory.CreateDirectory(folder); 
    
    folder += "\\" + fc["ProductID"].ToString();
    if(!Directory.Exists(folder))  
        Directory.CreateDirectory(folder); 
    
    var path = Path.Combine(folder, fc["ProductID"].ToString() + extension);          
    file.SaveAs(path); 
    

    Saturday, April 13, 2019 10:20 PM
  • User-1716253493 posted

    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

    string ProductImagesFolder = Server.MapPath(@"~\Images\ProductImages\");
    string brandfolder =  fc["BrandName"].ToString() + "\\" + fc["ProductID"].ToString();
    string folderpath = Path.Combine(ProductImagesFolder, brandfolder);
    string filepath = Path.Combine(folderpath, fc["ProductID"].ToString() + extension);
    if (!Directory.Exists(folderpath))
        Directory.CreateDirectory(folderpath); 
    file.SaveAs(filepath);

    Monday, April 15, 2019 1:06 AM