Creation of Directory and Uploading Images

Posted by Raj.Trivedi under ASP.NET on 3/9/2013 | Points: 10 | Views : 753 | Status : [Member] [MVP] | Replies : 4
Hello Team,

I want to create a directory in my asp.net web app and upload images into that newly created folder if the directory is not present.
If next time i try to upload images then it should recreate the directory,instead it should just upload the images in that directory.

Regard's
Raj.Trivedi
"Sharing is Caring"
Please mark as answer if your Query is resolved



Responses

Posted by: Santhi on: 3/10/2013 [Member] Starter | Points: 25

Up
0
Down
Hi

If you are uploading the images next time, then it should be saved in the different directory name right.
or Do you want to save all the images in the same directory

If you want to re-create the directory when you are saving the images next time to the directory.
You have to delete the directory which is existing. so, that you can re-create the directory with the same name.

I am explaining here to save the images in the same directory name.
if the directory is not exist, it creates the directory once.
Design Page
  <asp:FileUpload ID="FileUpload1" runat="server" />

<asp:Button ID="btnSave" runat="server" Text="Save"
onclick="btnSave_Click" />
</asp:Content>


CS page:
public partial class _Default : System.Web.UI.Page

{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void btnSave_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
if(!Directory.Exists(Server.MapPath("Images")))
{
Directory.CreateDirectory(Server.MapPath("Images"));

}

FileUpload1.SaveAs(Server.MapPath("Images\\"+FileUpload1.FileName));
}
}
}

Let me know, if you have any queries




Thanks & Regards,
Santhi .V

Raj.Trivedi, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Raj.Trivedi on: 3/10/2013 [Member] [MVP] Starter | Points: 25

Up
0
Down
Hello Santhi,

I want that if the user is uploading the images for the first time and if the directory does not exist then it should create it.
Once the directory is created it should not recreate again,it should upload in the same one

Regard's
Raj.Trivedi
"Sharing is Caring"
Please mark as answer if your Query is resolved

Raj.Trivedi, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Santhi on: 3/11/2013 [Member] Starter | Points: 25

Up
0
Down
Hi

I have given that code only.
Try to use that code. it works.


Thanks & Regards,
Santhi .V

Raj.Trivedi, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Jayakumars on: 3/11/2013 [Member] [MVP] Bronze | Points: 25

Up
0
Down
hi
raj
try this code working this

protected void Bt_Create_Click(object sender, EventArgs e)
{
string ImgPath = Server.MapPath("Images");
if (Fup1.HasFile)
{
if (Directory.Exists(Server.MapPath("Images")))
{
Fup1.SaveAs(ImgPath + "\\"+Fup1.FileName);
}
else
{
Directory.CreateDirectory(Server.MapPath("Images"));
Fup1.SaveAs(ImgPath + "\\"+Fup1.FileName);
}
}
}

Mark as Answer if its helpful to you

Raj.Trivedi, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response