示例#1
0
        public void UploadImage(object sender, EventArgs e)
        {
            if (UploadContainer.HasFile && Session["username"] != null)
            {
                string username       = (string)Session["username"];
                string filename       = Path.GetFileName(UploadContainer.PostedFile.FileName);
                string serverFilePath = "~/Images/" + username + "/";

                if (!CreateFolderIfNeeded(Server.MapPath(serverFilePath)))
                {
                    PageMessage.Text = "Failed to upload image. Try again later or contact the administrator of the site!";
                    return;
                }

                string randomString = RandomUtils.RandomString(10);

                string fullPath = serverFilePath + randomString + filename;

                UploadContainer.PostedFile.SaveAs(Server.MapPath(fullPath));

                int imgId = SqlUtilities.InsertIntoImages(username, fullPath, ImageDescription.Text, ImageCity.Text, ImageCountry.Text);

                ConnectWithSelectedCategories(fullPath);
                ConnectWithSelectedAlbums(fullPath);

                Response.Redirect("Image.aspx?id=" + imgId);
            }
        }