0

I have list box that contains paths to specific files in the directory that the code reads and parses data and do ohter stuff with it. The error I get is Unable to cast object of type 'System.String' to type 'System.Web.UI.HtmlControls.HtmlInputFile' and I'm not sure how to overcome this.

I have a html INPUT control of type file whose only function is to get the directory path becuase the directory is not always the same. Then I split the PostedFile.Filename which looks like this C:\temp\2013\03-2013\Calib 100 for 29 Mar 13\211jd13100.txton '\' into an string array. The I reasseble path in a string by adding the array elements less the last index and use that as param for 'Directory.GetFiles(string) to get all my files in the directory. Again, I don't know of anohter way to get the directory information. Anyway I'll just post the code, it will be easier to understand.

static public ArrayList hif = new ArrayList();
static string[] filePaths;

 protected void btnAddFile_Click(object sender, System.EventArgs e)
 {                
    if (Page.IsPostBack == true)
    {            
        StringBuilder sb = new StringBuilder();

        string[] dirLocation = fileUpload.PostedFile.FileName.Split('\\');            
        for (int i = 0; i < dirLocation.Length - 1; i++)
        {
            sb.Append(dirLocation[i].ToString() + "\\");
        }

        // The assenbled directory path is being used to get the files.
        filePaths = Directory.GetFiles(sb.ToString());

        for (int i = 0; i < filePaths.Length; i++)
        {
            hif.Add(filePaths[i]);
            lbxSelectedFiles.Items.Add(filePaths[i]);
        }
    }
}

The method above loads the listbox and filePath array with file path and name. The method below takes care of parsing but before that takes place I need to get the files and the error happens the foreach statement between the parents ().

    foreach (System.Web.UI.HtmlControls.HtmlInputFile HIF in hif)
    {
        try
        {
            File = HIF.PostedFile;
            StreamReader data = new StreamReader(HIF.PostedFile.InputStream);
            PathFilename = File.FileName.ToString();

            DirectoryInfo directory = new DirectoryInfo(PathFilename);
            Directory = directory.Parent.ToString();
            .
            .
            .
         }
    }
8
  • what is the data type of hif Commented Apr 19, 2013 at 21:44
  • @DanHunex HtmlInputFile Commented Apr 19, 2013 at 21:45
  • 1
    Why do you think that you could convert a path/string to a HtmlInputFile control? Commented Apr 19, 2013 at 21:46
  • is that List or just HtmlInputFile (hif -- small case...bad naming btw) Commented Apr 19, 2013 at 21:47
  • What directory do you need? You should use the helpers under System.IO instead of attempting to figure out the directory yourself, but I'm confused as to what use the path on the client machine will be on the server? Commented Apr 19, 2013 at 21:47

1 Answer 1

1

It looks like "hif" is an ArrayList of strings because you are adding the file paths to it.
Because it contains strings the objects pulled out of it to use in the loop variable will also be strings.
This line:

 foreach (System.Web.UI.HtmlControls.HtmlInputFile HIF in hif)

should change to

 foreach (string HIF in hif)

You will then have the file path name and can then use the path to open the file and get the actual File object you want to work with.

Basically the cast you specify doesn't work because hif doesn't have that type of object in it.

Sign up to request clarification or add additional context in comments.

2 Comments

The reason I use System.Web.UI.HtmlControls.HtmlInputFile in so that I can use its `PostedFile' property.
In the code sample above it looks like you are getting a list of file paths then cycling through them. It sounds like you want to get a list of the HTML controls that are on the page, and cycle through those to get the posted file information from the control. Reading the directory for the file paths will never give you the HTML control. If the file you are trying to access is in fact at the location you read from the directory, I believe you should do a file.open on that path and you will have access to do whatever you need with it from there.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.