Solved

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

Posted on 2011-03-18
8
938 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
  • 4
  • 2
8 Comments
 
LVL 33

Accepted Solution

by:
Todd Gerbert earned 500 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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 500 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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

744 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now