Click here to Skip to main content
16,004,686 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi guys,

I'm trying to import in my web site a button where the user will be able to upload the whole folder in the web site

I found the following code which works but only with a single file
C#
if (IsPostBack)
        {
            Boolean fileOK = false;
            String path = Server.MapPath("~/UploadedImages/");
            if (FileUpload1.HasFile)
            {
                String fileExtension =
                    System.IO.Path.GetExtension(FileUpload1.FileName).ToLower();
                String[] allowedExtensions = { ".gif", ".png", ".jpeg", ".jpg" };
                for (int i = 0; i < allowedExtensions.Length; i++)
                {
                    if (fileExtension == allowedExtensions[i])
                    {
                        fileOK = true;
                    }
                }
            }

            if (fileOK)
            {
                try
                {
                    FileUpload1.PostedFile.SaveAs(path
                        + FileUpload1.FileName);
                    Label1.Text = "File uploaded!";
                }
                catch (Exception ex)
                {
                    Label1.Text = "File could not be uploaded.";
                }
            }
            else
            {
                Label1.Text = "Cannot accept files of this type.";
            }
        }



but I want to upload the whole folder with the contents inside which is the best way to do that?

Thnx in advance for your help and your time!!!
Posted
Comments
Sergey Alexandrovich Kryukov 5-Nov-13 11:24am    
Just a note, for now: isn't that obvious: you cannot create such upload using server-side along. Something at client side should eventually send the set of files in HTTP request. Remember that server side does not have access to the client-side system.
—SA
ZurdoDev 5-Nov-13 11:35am    
There may be some 3rd party controls that can do that but I am not sure. Gmail allows you to select multiple files so maybe look into how they do it but I don't know of a way to do a whole folder without selecting the files.
Sergey Alexandrovich Kryukov 5-Nov-13 11:43am    
Not as easy. Please see my answer where I explain why.
—SA
JasonTsoum77 5-Nov-13 12:16pm    
Hi Sergey first of all thnx for your time and your response!

I know that my code is for a simple file and not for the whole folder i think i mention it at the beginning of my question.
I need somenthing like that in order to upload pictures for specific items in a website where i implement but I see from your post that is very difficult, i sarch in the internet but i didn't find anything to help me, so i decided to change strategy I will try to upload multiple files and create a folder(where the pictures will be upload it) at the same time on my site, I will let you now if this will be work.

Thanks again for your help

Please see my comment to the question. Not only I explained why your code does not work for an entire folder, I explained why it cannot work like that in principle. All you can do is to create some active code which would assemble the set of files on the client side and put them in HTTP request. Your idea itself is very good, I must say, such thing would be great to achieve, but this is not as simple as you probably think.

The problems is: the solutions I saw so far are all based on the use of the ActiveX "Scripting.FileSystemObject". This is a dirty approach, to say the least. First of all, not all browsers and systems will support it. This is (almost) exclusively for Microsoft system and IE; even though some other browser can do it via a plug-in, it would still be only for clients on Microsoft OS. Worse, this is considered unsafe, because ActiveX would open wide access to the client's system for a Web application. Many computer-savvy users will never use your site when they learn that you use ActiveX. Anyway, you will be able to find such solutions easily: http://bit.ly/HEvPd8[^].

Other solutions may involve Silverlight or Flash (should be installed on client side, but such solutions are more multiplatform; for example, there is a Mono versions of Silverligh for Mono version called Moonlight), or Java applet (more exactly, an applet based on the installed JVM used in the browser, which most clients should have, but the source language could be different from Java).

Please see:
http://www.michielpost.nl/Silverlight/MultiFileUploader/[^],
http://www.vectorlight.net/silverlight/demos/file_explorer.aspx[^],
http://jupload.sourceforge.net/applet-basic.html[^].

And so on… Nothing you can do with ASP.NET along. You can try to find some more, using the search pattern shown above, adding keywords on the technology you could consider to use.

—SA
 
Share this answer
 
v5
See This, upload-folders[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900