This is a migrated thread and some comments may be shown as answers.

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

2 Answers 714 Views
Upload
This is a migrated thread and some comments may be shown as answers.
King Wilder
Top achievements
Rank 2
King Wilder asked on 27 Jun 2012, 10:30 PM
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 Answers, 1 is accepted

Sort by
0
Atanas Korchev
Telerik team
answered on 28 Jun 2012, 06:59 AM
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!
0
King Wilder
Top achievements
Rank 2
answered on 28 Jun 2012, 03:56 PM
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.  :^)
Tags
Upload
Asked by
King Wilder
Top achievements
Rank 2
Answers by
Atanas Korchev
Telerik team
King Wilder
Top achievements
Rank 2
Share this question
or