Click here to Skip to main content
15,885,998 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
The Code is working perfectly only for multiple images/multiple videos. But what if I want to Upload multiple images with multiple videos in single file upload control so that only images and video can be uploaded from that control and nothing else.
Please Note that I am saving image path in SQl server database and image in asp.net folder.

Thanx in advance.

What I have tried:

XML
 <div class="form-group">
                                    <div class="col-sm-2">
                                        <asp:Label ID="lblUpload" runat="server" Text="Upload Photo/Video" CssClass="form-control"></asp:Label>
                                    </div>
                                    <div class="col-sm-6">
                                        <asp:FileUpload ID="FileUploadBlog" runat="server" CssClass="fileinput fileinput-new" AllowMultiple="true" />
                                    </div>
<pre><div class="col-sm-12">
                                        <asp:Button ID="btnBlogPost" runat="server" Text="Post" CssClass="btn btn-facebook" OnClick="btnBlogPost_Click" />
                                    </div>
                                    <div class="col-sm-4">
                                        <asp:Label ID="lblErrorUplaod" runat="server" Visible="false" CssClass="label-danger">
                                    </div>



Code behind:
C#
 string ext = System.IO.Path.GetExtension(this.FileUpload.PostedFile.FileName);
foreach (var file in FileUpload.PostedFiles)
{
   if (ext == ".jpeg" || ext == ".png" || ext == ".bmp" || ext == ".jpg")
   {
      if (FileUpload.PostedFile.ContentLength > 0 && FileUpload.PostedFile.ContentLength <= 625000)
      {.............................}
      else if (ext == ".mpeg4" || ext == ".mp4" || ext == ".3gp" || ext == ".flv")
      {
          if (FileUpload.PostedFile.ContentLength > 0 && FileUpload.PostedFile.ContentLength <= 625000)
         {.............................}
      }
Posted
Updated 16-Oct-17 2:36am
v3

1 solution

Well, you can use the "accept" attribute of the file upload control where you can specify all the valid file extensions you want. After this, the browser window will only show files matching the extensions available to upload.
But this is not foolproof, as the user is free to select all file types in the drop-down and select any file he wants.

If you wish to restrict the user from selecting invalid file types, you may want to write your own component.
 
Share this answer
 
v2
Comments
Umair Nafis 16-Oct-17 16:05pm    
Well I tried this validation also but the string ext = System.IO.Path.GetExtension(this.FileUpload.PostedFile.FileName); takes only one extension whis is selected first. ie If I select a image file of jpg format first then i select a video file of mp4 format than the ext takes only .jpg and execute only jpg part of the code, and in database the image inserted but video column having null value and vice versa.
GKP1992 16-Oct-17 23:33pm    
Yeah, I missed that you wanted to upload multiple files. In that case, the regex is not useful to you.
Also, you need to ask yourself a few questions like if you're selecting multiple files and some of them are not valid, what should happen? Should the validator accept the only valid files or should it discard all of them?
I will update the anwser.
Umair Nafis 17-Oct-17 10:57am    
Problem is neigther about multiple files nor about valid or invalid files, The problem is when i upload 2 files 1 *.jpg file and other one is *.mp4 file then whatever i select first file in fileupload control it insert that file only which was selected first, and i want to insert both the files in different coloumn of the database table, i.e image path in image coloumn and video path in video coloumn.

I also tried "string ext = System.IO.Path.GetExtension(this.FileUpload.PostedFile.FileName)" under foreach loop then first it succesfully insert the image file than again for second file loop start with taking .mp4 value and executing the If statement of the .jpg body. and insert the video path in image coloumn.

I dont know why this if statement of .jpg is executing even after checking that the file is .mp4 This is the problem, I am facing.

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