Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

System.Web.UI.WebControls.FileUpload trying to upload multiple files

Posted on 2011-03-18
8
Medium Priority
?
956 Views
Last Modified: 2013-12-17
I am trying to upload mulitple files, The idea is that the user selects one file from teh browse button. I then use the information from this file, like its type e.g. ".txt" and load all the other ".txt" files and also grab all the ".txt" files from its subdirectorites.

I can make this work only for a single file "EditSingleFile_Click"

but I am having real trouble trying to make the "EditAllFilesIncludeSubDir_Click" work and really need some help.

in the end my webpage would have a button that you could either click for that opption, or change the definition by having the button be a sort of drop down choice. so the button is initailezed to only edit a single file, but you could change that button to edit all files of a particular type is the current directory and all of its sub directories.  That is why i need help with trying to get the multiple version working.

I have added the methods below that I am using.

The display method is just being used temporarily, this in the end will load the files into a grid that you could edit

So I am just trying to get the "EditAllFilesIncludeSubDir_Click" to work.

here is the code below
================================================================

    public Dictionary<string, string> FileDetials(System.Web.UI.WebControls.FileUpload FileUploadObj)
    {
        string RootDirectory;
        string dpath;
        string fname;
        string SubFileName;
        string FileType;
        string FileExtension;
        string FileEditable;
        string ResXFileExtension;
        string MasterFileName;
        string MasterFile;
        int MasterFileCheck; //0 will be master

        Dictionary<string, string> SETColsDictionary = new Dictionary<string, string>();

        RootDirectory = System.IO.Path.GetDirectoryName(FileUploadObj.PostedFile.FileName);
        dpath = RootDirectory;

        FileType = FileUploadObj.PostedFile.ContentType.ToString();

        fname = System.IO.Path.GetFileName(FileUploadObj.PostedFile.FileName); // filename
        FileExtension = System.IO.Path.GetExtension(fname);
       
        //The Name of the file you are currently looking at in the directory
        SubFileName = System.IO.Path.GetFileNameWithoutExtension(fname);

        //Get the Name of the Master File
        MasterFileName = System.IO.Path.GetFileNameWithoutExtension(SubFileName);

        //If the Name of the Master File is the Same LENGTH
        //as the Sub File then we assume you are looking at the
        //Master File
        MasterFileCheck = SubFileName.Length - MasterFileName.Length;

        if (MasterFileCheck == 0)
        {
            ResXFileExtension = "US";
            MasterFile = "TRUE";
        }
        else
        {
            //The Name of the Extension and placed in UPPER Text
            ResXFileExtension = System.IO.Path.GetExtension(SubFileName);
            ResXFileExtension = ResXFileExtension.Replace(".", "");
            ResXFileExtension = ResXFileExtension.ToUpper();
            MasterFile = "FALSE";
        }

        FileEditable = FileUpload1.HasFile.ToString().ToUpper();

        SETColsDictionary.Add("RootDirectory", RootDirectory);
        SETColsDictionary.Add("dpath", dpath);
        SETColsDictionary.Add("fname", fname);
        SETColsDictionary.Add("SubFileName", SubFileName);
        SETColsDictionary.Add("FileType", FileType);
        SETColsDictionary.Add("FileExtension", FileExtension);
        SETColsDictionary.Add("FileEditable", FileEditable);
        SETColsDictionary.Add("ResXFileExtension", ResXFileExtension);
        SETColsDictionary.Add("MasterFileName", MasterFileName);
        SETColsDictionary.Add("MasterFile", MasterFile);

        return SETColsDictionary;
    } //public Dictionary<string, string> FileDetials


    public void DisplayFiles(Dictionary<string, string> FileDetailDictionary)
    {
                if (FileDetailDictionary["FileEditable"].ToString().Trim() == "TRUE")    
            {
                Label1.Text =   "File "
                                + FileDetailDictionary["fname"].ToString()
                                + " ready for edit </br>"
                                + "Directory: RootDirectory";

            }  
        else    
            {   // warning message        
                Label1.Text = "Some problem occurred while preparing the file ("
                                + FileDetailDictionary["fname"].ToString()
                                + ") for Edit. Please try after some time.";
            }
    }
   
    protected void EditSingleFile_Click(object sender, EventArgs e)
    {
        Dictionary<string, string> FileDetailDictionary = new Dictionary<string,string>();

        FileDetailDictionary = FileDetials(FileUpload1);

        DisplayFiles(FileDetailDictionary);

    }//EditSingleFile_Click

    protected void EditAllFilesIncludeSubDir_Click(object sender, EventArgs e)
    {

        Dictionary<string, string> FileDetailDictionary = new Dictionary<string, string>();

        FileDetailDictionary = FileDetials(FileUpload1);

        string[] fileNames;

        fileNames = Directory.GetFiles
                                    ( FileDetailDictionary["RootDirectory"].ToString()
                                    , FileDetailDictionary["FileExtension"].ToString()
                                    , SearchOption.AllDirectories);




        int i = 0;
        //get the full path of each file
        foreach (string fUploadName in fileNames)
        {
            i++;
            Dictionary<string, string> FileDetailSubDictionary = new Dictionary<string, string>();
            System.Web.UI.WebControls.FileUpload TempfileUpload = new FileUpload();

            //TempfileUpload.FileName = fUploadName;
            //TempfileUpload.Directory = Need to assing the subdirectory;

            //FileDetailDictionary = FileDetials(TempfileUpload);
        }

    }//EditAllFilesIncludeSubDir_Click
0
Comment
Question by:jhacharya
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
8 Comments
 
LVL 33

Accepted Solution

by:
Todd Gerbert earned 2000 total points
ID: 35170805
The C# code you're writing runs on the server, but the part of the equation here that lets the user pick a file runs on the user's computer: the web browser.  You really don't have any way to change the way Firefox, Internet Explorer, Chrome, etc, select files for upload.

The FileUpload controls only allow one file at a time - if you want to upload more than one file you can either use multiple FileUpload controls, or create your own ActiveX control for Internet Explorer.

You can probably also find some already-made components, like a Java applet which does run on the user's computer and would allow them to select multiple files, or an entire folder - though I don't know of any off the top of my head.
0
 
LVL 2

Author Comment

by:jhacharya
ID: 35183677
created the following DLL to get around the issue
using System;
using System.Collections;
using System.Collections.Specialized;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Windows.Forms;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Web;
using System.Web.Caching;
using System.Web.SessionState;
using System.Web.Security;
using System.Web.Profile;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;
using System.Xml.Linq;
using System.Xml.XPath;
using System.Net;
using System.IO;
using MAC.FileOperations;

namespace MAC.FileOperations
{
    public class IO_Operations
    {
        public bool isFileInUse(string filePath)
        {
            try
            {
                using (FileStream fileStream = new FileStream(filePath, FileMode.OpenOrCreate))
                {
                }
                return false;
            }
            catch (IOException)
            {
                return true;
            }
            catch (UnauthorizedAccessException)
            {
                return true;
            }
        }//public bool isFileInUse(string filePath)

        public bool isFileEditable(string filePath)
        {
            try
            {
                using (FileStream fileStream = new FileStream(filePath, FileMode.OpenOrCreate))
                {
                }
                return true;
            }
            catch (IOException)
            {
                return false;
            }
            catch (UnauthorizedAccessException)
            {
                return false;
            }
        }//public bool isFileEditable(string filePath)
    }//IO_Operations

    public class IO_ResXOperations
    {
        IO_Operations macIO = new IO_Operations();

        public Dictionary<string, string> MasterFileDetials(System.Web.UI.WebControls.FileUpload FileUploadObj)
        {
            string RootDirectory;
            string dpath;
            string fname;
            string SubFileName;
            string FileType;
            string FileExtension;
            string FileEditable;
            string ResXFileExtension;
            string MasterFileName;
            string MasterFile;
            int MasterFileCheck; //0 will be master

            Dictionary<string, string> SETColsDictionary = new Dictionary<string, string>();

            RootDirectory = System.IO.Path.GetDirectoryName(FileUploadObj.PostedFile.FileName);
            dpath = RootDirectory;

            FileType = FileUploadObj.PostedFile.ContentType.ToString();

            fname = System.IO.Path.GetFileName(FileUploadObj.PostedFile.FileName); // filename
            FileExtension = System.IO.Path.GetExtension(fname);

            //The Name of the file you are currently looking at in the directory
            SubFileName = System.IO.Path.GetFileNameWithoutExtension(fname);

            //Get the Name of the Master File
            MasterFileName = System.IO.Path.GetFileNameWithoutExtension(SubFileName);

            //If the Name of the Master File is the Same LENGTH
            //as the Sub File then we assume you are looking at the
            //Master File
            MasterFileCheck = SubFileName.Length - MasterFileName.Length;

            if (MasterFileCheck == 0)
            {
                ResXFileExtension = "US";
                MasterFile = "TRUE";
            }
            else
            {
                //The Name of the Extension and placed in UPPER Text
                ResXFileExtension = System.IO.Path.GetExtension(SubFileName);
                ResXFileExtension = ResXFileExtension.Replace(".", "");
                ResXFileExtension = ResXFileExtension.ToUpper();
                MasterFile = "FALSE";
            }

            FileEditable = FileUploadObj.HasFile.ToString().ToUpper();

            SETColsDictionary.Add("RootDirectory", RootDirectory);
            SETColsDictionary.Add("dpath", dpath);
            SETColsDictionary.Add("fname", fname);
            SETColsDictionary.Add("SubFileName", SubFileName);
            SETColsDictionary.Add("FileType", FileType);
            SETColsDictionary.Add("FileExtension", "*" + FileExtension);
            SETColsDictionary.Add("FileEditable", FileEditable);
            SETColsDictionary.Add("ResXFileExtension", ResXFileExtension);
            SETColsDictionary.Add("MasterFileName", MasterFileName);
            SETColsDictionary.Add("MasterFile", MasterFile);

            return SETColsDictionary;
        } //public Dictionary<string, string> MasterFileDetials



        public Dictionary<string, string> SubFileDetials(string FileFullPathAndName)
        {
            string RootDirectory;
            string dpath;
            string fname;
            string SubFileName;
            string FileType;
            string FileExtension;
            string FileEditable;
            string ResXFileExtension;
            string MasterFileName;
            string MasterFile;
            int MasterFileCheck; //0 will be master

            Dictionary<string, string> SETColsDictionary = new Dictionary<string, string>();

            RootDirectory = System.IO.Path.GetDirectoryName(FileFullPathAndName);
            dpath = RootDirectory;

            FileType = System.IO.Path.GetExtension(FileFullPathAndName);

            fname = System.IO.Path.GetFileName(FileFullPathAndName); // filename
            FileExtension = System.IO.Path.GetExtension(fname);

            //The Name of the file you are currently looking at in the directory
            SubFileName = System.IO.Path.GetFileNameWithoutExtension(fname);

            //Get the Name of the Master File
            MasterFileName = System.IO.Path.GetFileNameWithoutExtension(SubFileName);

            //If the Name of the Master File is the Same LENGTH
            //as the Sub File then we assume you are looking at the
            //Master File
            MasterFileCheck = SubFileName.Length - MasterFileName.Length;

            if (MasterFileCheck == 0)
            {
                ResXFileExtension = "US";
                MasterFile = "TRUE";
            }
            else
            {
                //The Name of the Extension and placed in UPPER Text
                ResXFileExtension = System.IO.Path.GetExtension(SubFileName);
                ResXFileExtension = ResXFileExtension.Replace(".", "");
                ResXFileExtension = ResXFileExtension.ToUpper();
                MasterFile = "FALSE";
            }

            FileEditable = macIO.isFileEditable(FileFullPathAndName).ToString().ToUpper();

            SETColsDictionary.Add("RootDirectory", RootDirectory);
            SETColsDictionary.Add("dpath", dpath);
            SETColsDictionary.Add("fname", fname);
            SETColsDictionary.Add("SubFileName", SubFileName);
            SETColsDictionary.Add("FileType", FileType);
            SETColsDictionary.Add("FileExtension", "*" + FileExtension);
            SETColsDictionary.Add("FileEditable", FileEditable);
            SETColsDictionary.Add("ResXFileExtension", ResXFileExtension);
            SETColsDictionary.Add("MasterFileName", MasterFileName);
            SETColsDictionary.Add("MasterFile", MasterFile);

            return SETColsDictionary;
        } //public Dictionary<string, string> SubFileDetials

    }//IO_ResXOperations

} //MAC.FileOperations
0
 
LVL 33

Expert Comment

by:Todd Gerbert
ID: 35183735
>> I am trying to upload mulitple files
I fail to see how that code you have above will allow for multiple file uploads.

You are aware that all of that code is going to run on the server, right? e.g. RootDirectory = System.IO.Path.GetDirectoryName(FileFullPathAndName); is going to get the name of the folder on the server, not the client that's uploading files.
0
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
LVL 2

Author Comment

by:jhacharya
ID: 35183938
i get  the master file using the System.Web.UI.WebControls.FileUpload object

and plug this intoinforamtion into

            fileNames = Directory.GetFiles
                                (FileDetailDictionary["RootDirectory"].ToString()
                                , FileDetailDictionary["FileExtension"].ToString()
                                , SearchOption.AllDirectories);

which I then plug into  RootDirectory = System.IO.Path.GetDirectoryName(FileFullPathAndName);


So I was thinking that since I get the data from client by using System.Web.UI.WebControls.FileUpload object initialy then everything else would still be pointing to the client information.

If this is wrong please let me know
0
 
LVL 33

Assisted Solution

by:Todd Gerbert
Todd Gerbert earned 2000 total points
ID: 35184019
Yes, I think you may be mistaken.  It's important to keep in mind that all that code you're writing only every runs on your server, never on the client machine.

Consider this:

On my computer I have a folder on my C: drive named FilesToUpload, and in it is a "Master001.file" and a "Child001.file".

I go to your website and upload the Master001.file.

Here's where your first glitch is: my web browser is set to not send complete paths when I upload files, so your call to Path.GetDirectoryName is going to return an empty string: ""

Assume however that I do send the complete path, C:\FilesToUpload\master001.file - so now your Directory.GetFiles (remember, this code is running on the server) is going try and list the files in C:\FilesToUpload, but you'll get an exception because your server doesn't have a C:\FilesToUpload folder.


Those issues might have easily slipped by, since it will work, but only in the case where the server and the client are the same machine (e.g. while debugging in Visual Studio).
0
 
LVL 33

Expert Comment

by:Todd Gerbert
ID: 35233017
The FileUpload control will not, under any circumstance, allow a user to upload more than one file per FileUpload control (http://msdn.microsoft.com/en-us/library/aa479405.aspx, No built-in capabilities in the Microsoft .NET Framework enable you to upload multiple files from a single ASP.NET page.).  Using the System.IO namespace in code-behind to build paths and enumerate files will result in a server-side enumeration of a given directory, not the client sending the files.

The currently accepted answer addresses neither requirement of the question, uploading multiple files nor allowing the user to select a folder/multiple files (I am trying to upload mulitple files and I then use the information from this file, like its type e.g. ".txt" and load all the other ".txt" files and also grab all the ".txt" files from its subdirectorites).

The correct answer is http:#a35170805, and is further explained in http:#a35184019; my recommendation is to split points between these two comments.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

609 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question