0

I am doing a Project of Uploading the files to SQL Server and then Download it I want to Select Multiple files to be Selected and Uploaded at a Single Click but Presently a Single File is selected and Uploaded both to FTP Server and SQL Server, could any one Suggest me a link or Sample Code to Select Multiple Files and Upload it to SQL Server

Thanks in Advance...

2 Answers 2

1

You need to use FileUpload.PostedFiles Property

Try with following code:

foreach (HttpPostedFile upFile in FileUpload1.PostedFiles)
{
    SaveFiles(upFile);
}

private void SaveFiles(HttpPostedFile fObj)
{
   using(SqlConnection con = new SqlConnection(ConnectionString))// set ConnectionString
   {
       using(SqlCommand cmd = new SqlCommand(DatabaseQuery,con)) // set appropriate query
       {
          cmd.Parameters.AddWithValue("@data", ReadFile(fObj));
          con.Open();
          cmd.ExecuteNonQuery();
       }
   }
}

private byte[] ReadFile(HttpPostedFile fObj2)
{
    byte[] data = new Byte[fObj2.ContentLength];
    fObj2.InputStream.Read(data, 0, file.ContentLength);
    return data;
}

MSDN:

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.fileupload.postedfiles.aspx

Hope Its Helpful.

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

2 Comments

@ Freelancer As My system OS is XP only i can't Install .Net Framework 4.5 to access Fileupload.postedfiles So, I found some Jquery to select multiple files and used your RReadFile(HttpPostedFile fObj2) to read the file and Upload it in SQL Server
@Rajesh If permited, you can use telerik controls for this purpose, inbuilt functionality for multiple file upload is there.
1

Be sure to use the AllowMultiple attribute (supported by .Net 4.5):

<asp:FileUpload ID="MyFileUpload" runat="server" AllowMultiple="true" />

You can now select multiple files with ALT/STRG to be uploaded. Then use Freelancer´s code.

1 Comment

@ AGuyCalledGerald i had only XP installed in my System so I can't use the AllowMultiple which was Supported by .Net 4.5 so I used the Jquery to select Multiple files and used Freelancers ReadFile(HttpPostedFile fObj2) to read and Upload files in SQL Server

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.