Sign In| Join
Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Last post May 03, 2012 11:02 AM by ramiramilu
Member
243 Points
484 Posts
May 03, 2012 10:33 AM|LINK
hi,
I am using System.IO to upload multiple files. I am still trying to get the code working
but having no luck.
Objective
On my File Server I have a Directory Called "Users".
On my aspx page, I have a FORM that gets info from a new user.
I now want to save the images in the "Users" Directory and within that create
another Directory Called The "Name" of the user - taken from the "Name" Textbox
on the form.
Inconplete Code
string name= Name.Text; // (variable from form) HttpFileCollection uploadedFiles = Request.Files; for (int i = 0; i < uploadedFiles.Count; i++) { HttpPostedFile file = uploadedFiles[i]; string fileExt = Path.GetExtension(file.FileName).ToLower(); string fileName = Path.GetFileName(file.FileName); if (fileName != string.Empty) { try { if (!Directory.Exists(Server.MapPath("/Users/"))) { { Directory.CreateDirectory(Server.MapPath("/Users/")); } } else { } } catch (Exception ex) { throw ex; } } }
All-Star
34421 Points
5534 Posts
May 03, 2012 10:53 AM|LINK
try this
string name= Name.Text; // (variable from form)
HttpFileCollection uploadedFiles = Request.Files; for (int i = 0; i < uploadedFiles.Count; i++) { HttpPostedFile file = uploadedFiles[i]; string fileExt = Path.GetExtension(file.FileName).ToLower(); string fileName = Path.GetFileName(file.FileName); if (fileName != string.Empty) { try { if (!Directory.Exists(Server.MapPath("/Users/"))) { Directory.CreateDirectory(Server.MapPath("/Users/")); } if (!Directory.Exists(Server.MapPath("/Users/name"))) { Directory.CreateDirectory(Server.MapPath("/Users/name")); } file.PostedFile.SaveAs(Server.MapPath("/Users/name") + "/" + file.FileName);
} catch (Exception ex) { throw ex; } }
}
hope this helps...
95463 Points
14106 Posts
May 03, 2012 11:02 AM|LINK
if (!Directory.Exists(Server.MapPath("/Users/"))) { Directory.CreateDirectory(Server.MapPath("/Users/"));
// Save File here in the new directory...uploadedFiles[i].SaveAs(your file name);
} else { // As already directory exists, save file in the directory...uploadedFiles[i].SaveAs(your file name); }
prontonet
Member
243 Points
484 Posts
File Upload query to create new directory within another ?
May 03, 2012 10:33 AM|LINK
hi,
I am using System.IO to upload multiple files. I am still trying to get the code working
but having no luck.
Objective
On my File Server I have a Directory Called "Users".
On my aspx page, I have a FORM that gets info from a new user.
I now want to save the images in the "Users" Directory and within that create
another Directory Called The "Name" of the user - taken from the "Name" Textbox
on the form.
Inconplete Code
string name= Name.Text; // (variable from form)
HttpFileCollection uploadedFiles = Request.Files;
for (int i = 0; i < uploadedFiles.Count; i++)
{
HttpPostedFile file = uploadedFiles[i];
string fileExt = Path.GetExtension(file.FileName).ToLower();
string fileName = Path.GetFileName(file.FileName);
if (fileName != string.Empty)
{
try
{
if (!Directory.Exists(Server.MapPath("/Users/")))
{
{
Directory.CreateDirectory(Server.MapPath("/Users/"));
}
}
else
{
}
}
catch (Exception ex)
{
throw ex;
}
}
}
kedarrkulkar...
All-Star
34421 Points
5534 Posts
Re: File Upload query to create new directory within another ?
May 03, 2012 10:53 AM|LINK
try this
string name= Name.Text; // (variable from form)
HttpFileCollection uploadedFiles = Request.Files;
for (int i = 0; i < uploadedFiles.Count; i++)
{
HttpPostedFile file = uploadedFiles[i];
string fileExt = Path.GetExtension(file.FileName).ToLower();
string fileName = Path.GetFileName(file.FileName);
if (fileName != string.Empty)
{
try
{
if (!Directory.Exists(Server.MapPath("/Users/")))
{
Directory.CreateDirectory(Server.MapPath("/Users/"));
}
if (!Directory.Exists(Server.MapPath("/Users/name")))
{
Directory.CreateDirectory(Server.MapPath("/Users/name"));
}
file.PostedFile.SaveAs(Server.MapPath("/Users/name") + "/" + file.FileName);
}
catch (Exception ex)
{
throw ex;
}
}
}
hope this helps...
KK
Please mark as Answer if post helps in resolving your issue
My Site
ramiramilu
All-Star
95463 Points
14106 Posts
Re: File Upload query to create new directory within another ?
May 03, 2012 11:02 AM|LINK
if (!Directory.Exists(Server.MapPath("/Users/")))
{
Directory.CreateDirectory(Server.MapPath("/Users/"));
// Save File here in the new directory...uploadedFiles[i].SaveAs(your file name);
}
else
{
// As already directory exists, save file in the directory...uploadedFiles[i].SaveAs(your file name);
}
JumpStart