[HttpPostedFileBase].Filename returns name only, not full path

3 posts, 0 answers
  1. King Wilder
    King Wilder avatar
    240 posts
    Member since:
    Oct 2007

    Posted 27 Jun 2012 Link to this post

    I'm using the new KendoUI Extensions for MVC and while they work I'm having this weird issue where the Upload component seems to be just returning the filename of the file and not the full path.

    Does anyone know why?  This has never happened before, so I'm a little confused as to why it's happening now.

    [HttpPost]
    public virtual ActionResult CreateTable(IEnumerable<HttpPostedFileBase> files)
    {
        string tableName = Request["CreateTableViewModel.TableName"].ToString();
        bool blnOk = true;
     
        try
        {
            if (string.IsNullOrEmpty(tableName))
            {
                ModelState.AddModelError("CreateTableViewModel.TableName", "Table Name is required.");
                blnOk = false;
            }
     
            if (files == null)
            {
                ModelState.AddModelError("files", "CSV file is required");
                blnOk = false;
            }
     
            if (!blnOk)
            {
                return RedirectToAction(MVC.Data.CreateTable());
            }
     
            foreach (var hpf in files)
            {
                if (hpf.ContentLength == 0)
                {
                    ModelState.AddModelError("files", "You must select a file to upload.");
                    return RedirectToAction(MVC.Data.CreateTable());
                }
     
                _sqlService.CreateDatasource(hpf.FileName, tableName);
            }
     
            return RedirectToAction(MVC.Data.Success());
        }
        catch (Exception ex)
        {
            ModelState.AddModelError("", ex.Message);
            return RedirectToAction(MVC.Data.CreateTable());
        }
    }

    The "hpf.FileName" is the filename only and not the full path to the file.

    Thanks,

    King Wilder
  2. Atanas Korchev
    Admin
    Atanas Korchev avatar
    8462 posts

    Posted 28 Jun 2012 Link to this post

    Hello King,

    This issues is caused by a difference in the way browsers support file upload. For security reasons up-to-date browsers strip the full path of the uploaded file to conceal the end-user folder structure.

    You could check here for additional info: http://stackoverflow.com/questions/382464/httppostedfile-filename-different-from-ie 

    Regards,
    Atanas Korchev
    the Telerik team
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
  3. UI for ASP.NET MVC is VS 2017 Ready
  4. King Wilder
    King Wilder avatar
    240 posts
    Member since:
    Oct 2007

    Posted 28 Jun 2012 Link to this post

    I didn't know this.  So I have to change the way I handle uploads in working with the InputStream instead of just pointing to the file path.  Is this correct, or is there some other way of handling file uploads that I'm missing?

    Thanks,

    King Wilder

    EDIT: nevermind - I figured out a work around.  My service needs the full path to the file that it needs to process, so instead of rewriting my service, I simply save the file immediately to my server, and then reference that file, instead of the uploaded file.

    Hopefully this helps others who may have similar problems.

    This is the MVC controller action BEFORE the modifications:

    [HttpPost]
    public virtual ActionResult CreateTable(IEnumerable<HttpPostedFileBase> files)
    {
        string tableName = Request["CreateTableViewModel.TableName"].ToString();
        bool blnOk = true;
     
        try
        {
            if (string.IsNullOrEmpty(tableName))
            {
                ModelState.AddModelError("CreateTableViewModel.TableName", "Table Name is required.");
                blnOk = false;
            }
     
            if (files == null)
            {
                ModelState.AddModelError("files", "CSV file is required");
                blnOk = false;
            }
     
            if (!blnOk)
            {
                return RedirectToAction(MVC.Data.CreateTable());
            }
     
            foreach (var hpf in files)
            {
                if (hpf.ContentLength == 0)
                {
                    ModelState.AddModelError("files", "You must select a file to upload.");
                    return RedirectToAction(MVC.Data.CreateTable());
                }
     
                // The service expects the hpf.FileName to be the full path, not just the filename.
                // This will fail!
                _sqlService.CreateDatasource(hpf.FileName, tableName);
            }
     
            return RedirectToAction(MVC.Data.Success());
        }
        catch (Exception ex)
        {
            ModelState.AddModelError("", ex.Message);
            return RedirectToAction(MVC.Data.CreateTable());
        }
    }

    This is the solution AFTER a little workaround:

    [HttpPost]
    public virtual ActionResult CreateTable(IEnumerable<HttpPostedFileBase> files)
    {
        string tableName = Request["CreateTableViewModel.TableName"].ToString();
        bool blnOk = true;
     
        try
        {
            if (string.IsNullOrEmpty(tableName))
            {
                ModelState.AddModelError("CreateTableViewModel.TableName", "Table Name is required.");
                blnOk = false;
            }
     
            if (files == null)
            {
                ModelState.AddModelError("files", "CSV file is required");
                blnOk = false;
            }
     
            if (!blnOk)
            {
                return RedirectToAction(MVC.Data.CreateTable());
            }
     
            foreach (var hpf in files)
            {
                if (hpf.ContentLength == 0)
                {
                    ModelState.AddModelError("files", "You must select a file to upload.");
                    return RedirectToAction(MVC.Data.CreateTable());
                }
     
                // New changes - first save the file on the server
                hpf.SaveAs(Path.Combine(Server.MapPath("~/App_Data/Csv"),hpf.FileName));
     
                // Now create a path to the file to send to the service
                string csvFilePath = Path.Combine(Server.MapPath("~/App_Data/Csv"), hpf.FileName);
     
                // Now my service works without being modified as it expects a full path.
                _sqlService.CreateDatasource(csvFilePath, tableName);
            }
     
            return RedirectToAction(MVC.Data.Success());
        }
        catch (Exception ex)
        {
            ModelState.AddModelError("", ex.Message);
            return RedirectToAction(MVC.Data.CreateTable());
        }
    }

    Every once in a while I do have a moment of clarity.  :^)
Back to Top