Wednesday, February 18, 2015

upload multiple files

 protected void Button2_Click(object sender, EventArgs e)
    {
        string filepath = Server.MapPath("Upload");
        HttpFileCollection uploadedFiles = Request.Files;
        label.Text = string.Empty;

        for (int i = 0; i < uploadedFiles.Count; i++)
        {
            HttpPostedFile userPostedFile = uploadedFiles[i];

            try
            {
                if (userPostedFile.ContentLength > 0)
                {
                    label.Text += "<u>File #" + (i + 1) + "</u><br>";
                    label.Text += "File Content Type: " + userPostedFile.ContentType + "<br>";
                    label.Text += "File Size: " + userPostedFile.ContentLength + "kb<br>";
                    label.Text += "File Name: " + userPostedFile.FileName + "<br>";

                    userPostedFile.SaveAs(filepath + "\\" + Path.GetFileName(userPostedFile.FileName));
                    label.Text += "Location where saved: " + filepath + "\\" + Path.GetFileName(userPostedFile.FileName) + "<p>";
                }
            }
            catch (Exception Ex)
            {
                label.Text += "Error: <br>" + Ex.Message;
            }
        }
    }


}

No comments:

Post a Comment