below is my codes.
This code can create directory and sub-directory. It can also upload image..The problem is I want to upload the image into sub directory, It cannot save into sub-directory.It stores only on the main directory.The main directory is the Albums directory. The concept of this, is that everytime I create a directory (that is a sub-directory), I upload images/photo that would automatically save in the sub-directory i created..
Anyone can help me regarding this matter.
Anyone can help me regarding this matter.
onclick="Button1_Click" />
TypeScript
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;
using System.IO;
using System.Data.SqlClient;
using System.Data.SqlTypes;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
lblFolderName.Visible = false;
txtFolderName.Visible = false;
btnSave.Visible = false;
}
private void CreateDirectoryIfNotExists(string NewDirectory)
{
try
{
// Checking the existance of directory
if (!Directory.Exists(NewDirectory))
{
//If No any such directory then creates the new one
Directory.CreateDirectory(NewDirectory);
Label1.Text = "Directory Created";
}
else
{
Label1.Text = "Directory Exist";
}
}
catch (IOException _err)
{
Response.Write(_err.Message);
}
}
protected void Button1_Click(object sender, EventArgs e)
{
lblFolderName.Visible = true;
txtFolderName.Visible = true;
btnSave.Visible=true;
Label1.Visible = false;
}
protected void btnSave_Click(object sender, EventArgs e)
{
string NewDirectory = Server.MapPath("~/Albums/" + txtFolderName.Text);
//New Directory Name in string variable
CreateDirectoryIfNotExists(NewDirectory);
//Calling the function to create new directory
Label1.Visible = true;6 Answers
Know the answer? Post it. Somebody with the same question will find it here.
Does it giving any error?
What's happening if you attach the debugger?
What's happening if you attach the debugger?
no error at all..my concern is just, I want to save the images uploaded in sub-directory I created..because the image uploaded stores or saves in main directory (which is Albums directory)...
Former memberPosted Sep 24, 2013, 9:29 PM EDT
Can you please upload the complete code to check?
mikescrupz5 pointsPosted Sep 25, 2013, 3:47 AM EDT
mikescrupz5 pointsPosted Sep 25, 2013, 4:17 AM EDT
mikescrupz5 pointsPosted Sep 25, 2013, 4:44 AM EDT
using System; using System.Collections.Generic; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data; using System.Configuration; using System.IO; //additional namespace is required for io operations using System.Data.SqlClient; using System.Data.SqlTypes; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { lblFolderName.Visible = false; txtFolderName.Visible = false; btnSave.Visible = false; } private void CreateDirectoryIfNotExists(string NewDirectory) { try { // Checking the existance of directory if (!Directory.Exists(NewDirectory)) { //If No any such directory then creates the new one Directory.CreateDirectory(NewDirectory); Label1.Text = "Directory Created"; } else { Label1.Text = "Directory Exist"; } } catch (IOException _err) { Response.Write(_err.Message); } } protected void Button1_Click(object sender, EventArgs e) { lblFolderName.Visible = true; txtFolderName.Visible = true; btnSave.Visible=true; Label1.Visible = false; } protected void btnSave_Click(object sender, EventArgs e) { string NewDirectory = Server.MapPath("~/Albums/" + txtFolderName.Text); //New Directory Name in string variable CreateDirectoryIfNotExists(NewDirectory); //Calling the function to create new directory Label1.Visible = true; FileUpload img = (FileUpload)fileUpload; Byte[] imgByte = null; if (img.HasFile && img.PostedFile != null) { //To create a PostedFile HttpPostedFile File = fileUpload.PostedFile; //Create byte Array with file len imgByte = new Byte[File.ContentLength]; //force the control to load data in array File.InputStream.Read(imgByte, 0, File.ContentLength); // Insert the profile records into db string conn = ConfigurationManager.ConnectionStrings["DatabaseConnectionString"].ConnectionString; //string conn = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Coldwind.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True"; SqlConnection connection = new SqlConnection(conn); // SqlDataReader dr = new SqlDataReader(); string fname = fileUpload.FileName.ToString(); string FolderName = txtFolderName.Text.Replace("'", "''"); connection.Open(); string sql = "INSERT INTO [ImageTable]([FolderName],[PhotoFileName]) Values('" + FolderName + "','" + fname.ToString() + "' )"; SqlCommand cmd = new SqlCommand(sql, connection); cmd.CommandType = CommandType.Text; cmd.ExecuteNonQuery(); connection.Close(); imgPhoto.Visible = true; StartUpLoad(); } else { Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "Alert", "alert('Please select a photo before creating a profile')", true); } } private void StartUpLoad() { //get the file name of the posted image string imgName = fileUpload.FileName; //sets the image path string subfolder = txtFolderName.Text; string imgPath = "~/Albums/" + subfolder + imgName; //get the size in bytes that int imgSize = fileUpload.PostedFile.ContentLength; //validates the posted file before saving if (fileUpload.PostedFile != null && fileUpload.PostedFile.FileName != "") { // 10240 KB means 10MB, You can change the value based on your requirement if (fileUpload.PostedFile.ContentLength > 10240000) { Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "Alert", "alert('File is too big.')", true); } else { //then save it to the Folder fileUpload.SaveAs(Server.MapPath(imgPath)); imgPhoto.ImageUrl = "~/" + imgPath; Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "Alert", "alert('Profile Photo has been uploaded!')", true); } } } }