Click here to Skip to main content
15,935,728 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
I currently am running an application in which i need users to be able to upload a file to a directory on the server.

please help
Posted

Hii, I think it helps to u. It is in C# i hopes u can understand..
C#
if (UploadImage.PostedFile != null)
            {
                string fileName = UploadText.FileName.ToString();
                string[] dots = fileName.Split('.');
                string fileType = "txt";
                string type = dots[dots.Length - 1];
                string type1 = dots[dots.Length - 1];
                if (fileType.IndexOf(type) == -1)
                {
                    UploadText.Focus();
                    return;
                }
                else
                {
                    string UploadDescription = Description.Content;
                    string strUploadPath = "", strFilePath = "";
                    strFilePath = @"C:\\UploadTextFiles\";
                    string path = DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Year.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Millisecond.ToString();
                    strUploadPath = Server.MapPath(strFilePath) + path + UploadText.FileName;
                    UploadText.PostedFile.SaveAs(strUploadPath);
                    // UploadText.SaveAs(strUploadPath);


                    byte[] imageSize = new byte[UploadImage.PostedFile.ContentLength];
                    HttpPostedFile uploadedImage = UploadImage.PostedFile;
                    uploadedImage.InputStream.Read(imageSize, 0, (int)UploadImage.PostedFile.ContentLength);

                    SqlCommand cmd = new SqlCommand();
                    cmd.CommandText = @"INSERT INTO table(Cols1,Cols2) VALUES (@UploadImage,@UploadText)";
                    cmd.CommandType = CommandType.Text;
                    cmd.Connection = con;
                                      

                    SqlParameter UploadedImage = new SqlParameter("@UploadImage", SqlDbType.Image, imageSize.Length);
                    UploadedImage.Value = imageSize;
                    cmd.Parameters.Add(UploadedImage);

                    SqlParameter UploadedText = new SqlParameter("@UploadText", SqlDbType.VarChar, 200);
                    UploadedText.Value = strUploadPath;
                    cmd.Parameters.Add(UploadedText);

                   

                    con.Open();

                    int result = cmd.ExecuteNonQuery();
                    con.Close();
                    if (result > 0)
                        ScriptManager.RegisterStartupScript(this, this.GetType(), "alertmessage", "javascript:alert('" + strImageName + " image inserted successfully')", true);
                    SqlDataAdapter da = new SqlDataAdapter("select * from UploadProjectDetails", con);
                    DataSet ds = new DataSet();
                    da.Fill(ds);
                    GridView1.DataSource = ds.Tables[0].DefaultView;
                    GridView1.DataBind();
                }

            }
 
Share this answer
 
v2
Add a FileUpload control[^] to your page. The link includes an example that does what you want.
 
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