0

I have two FileUpload on a single Web Page. The problem is that when I uploaded the file using FileUpload3 on specific folder it also upload the FileUpload2 file also on that folder. I want that when i click on Submit Button FileUpload3 upload file on specfic folder of only FileUpload3 not FileUpload2. I am using ASP.Net and C#.

My ASPX Code :

<asp:FileUpload ID="FileUpload2" runat="server" /></br>
<asp:FileUpload ID="FileUpload3" runat="server" AllowMultiple="true" />

and My C# Code :

if (FileUpload2.HasFile)
            {
                string time1 = INDIAN_ZONE.ToString("MM-dd-yyyy_hhmmss");
                string directoryPath = Server.MapPath(string.Format("./Upload_Files/Day_Description/Destination_Image/" + DropDownList8.SelectedItem.Text + "/" + Label23.Text));
                if (!Directory.Exists(directoryPath))
                {
                    Directory.CreateDirectory(directoryPath);
                }
                else
                {
                }

                string fileName = Path.GetFileName(FileUpload2.PostedFile.FileName);
                fileName = time1 + fileName;
                string path = "./Upload_Files/Day_Description/Destination_Image/" + DropDownList8.SelectedItem.Text + "/" + Label23.Text + "/";
                FileUpload2.PostedFile.SaveAs(Server.MapPath(path) + fileName);
            }
            else
            {
                string time1 = INDIAN_ZONE.ToString("MM-dd-yyyy_hhmmss");
                string directoryPath = Server.MapPath(string.Format("./Upload_files/Day_Description/" + DropDownList8.SelectedItem.Text + "/" + Label23.Text));
                if (!Directory.Exists(directoryPath))
                {
                    Directory.CreateDirectory(directoryPath);
                }
                else
                {
                }
            }

            if (FileUpload3.HasFile)
            {
                HttpFileCollection hfc = Request.Files;
                for (int i = 0; i < hfc.Count; i++)
                {
                    HttpPostedFile hpf = hfc[i];
                    if (hpf.ContentLength > 0)
                    {
                        string FileExtention = System.IO.Path.GetExtension(FileUpload3.FileName);
                        string time1 = INDIAN_ZONE.ToString("MM-dd-yyyy_hhmmss");
                        string directoryPath = Server.MapPath(string.Format("./Upload_Files/Day_Description/Hotel_Image/" + DropDownList8.SelectedItem.Text + "/" + Label23.Text));
                        if (!Directory.Exists(directoryPath))
                        {
                            Directory.CreateDirectory(directoryPath);
                        }
                        else
                        {
                        }

                        string fileName = Path.GetFileName(hpf.FileName);
                        fileName = time1 + fileName;
                        string path = "./Upload_Files/Day_Description/Hotel_Image/" + DropDownList8.SelectedItem.Text + "/" + Label23.Text + "/";
                        hpf.SaveAs(Server.MapPath(path) + fileName);
                    }
                }
            }
            else
            {
                string time1 = INDIAN_ZONE.ToString("MM-dd-yyyy_hhmmss");
                string directoryPath = Server.MapPath(string.Format("./Upload_Files/Day_Description/Hotel_Image/" + DropDownList8.SelectedItem.Text));
                if (!Directory.Exists(directoryPath))
                {
                    Directory.CreateDirectory(directoryPath);
                }
                else
                {
                }
            }

The problem is that when I uploaded the file using FileUpload3 on specific folder it also upload the FileUpload2 file also on that folder. I want that when i click on Submit Button FileUpload3 upload file on specfic folder of only FileUpload3 not FileUpload2. I am using ASP.Net and C#.

2 Answers 2

1

That's because both the file upload controls generate file input controls on the same form and when you submit the page (regardless of which button triggers the submit) everything on the form is posted (including all the file upload controls).

The solution is to use two separate forms, or to use javascript to clear the file input for the file upload control you don't want to submit prior to submitting the page.

Unfortunately, it is a short-coming of the ASP.NET web-forms model that you cannot have multiple forms on the same page (with runat="server" enabled) which means that you wouldn't be able to use ASP.NET controls on the other forms. So really, the java-script solution is the only way to do it if you wish to keep using the ASP.NET file-upload controls.

EDIT

Another option is to re-organize the UI of your form and require the user to select the kind of upload they'd like to use before showing them the upload controls.

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

Comments

0

I got the Solution for this problem. I empty the file of FileUpload2 using

FileUpload2.PostedFile.InputStream.Dispose();

before upload file using FileUpload3.

Comments

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.