I managed to do file upload and create folder but I don't wan use my ID as folder name and due to DB relationship i only Can use ID and the name is from other DB any1 have any idea how to do it? Here is my codes:
[HttpPost]
public ActionResult Create(FileUpload fileupload, HttpPostedFileBase file)
{
if (ModelState.IsValid)
{
if (file != null && file.ContentLength > 0)
{
var fileName = System.IO.Path.GetFileName(file.FileName);
var folder = Server.MapPath("~/UploadArea/" + fileupload.JobID);
Directory.CreateDirectory(folder);
var path = System.IO.Path.Combine(Server.MapPath("~/UploadArea/" + fileupload.JobID), file.FileName);
file.SaveAs(path);
}
At the moment it only use the number from the JobID but I want it use my JobName as Folder name which do not exist on FileDB but FileDB have relationship to my JobDB propely that why it's display the JobID but not the JobName.
it do not give me JobName from the list due to that table do not have JobName on it. it only hold JobID i have to do some inner join to other table but i not sure how. here is the code, any idea?
public ActionResult Create()
{
ViewBag.JobID = new SelectList(db.Jobs, "JobID", "JobRef");
var FileUpload = new FileUpload();
FileUpload.Date = DateTime.Today;
return View(FileUpload);
}
[HttpPost]
public ActionResult Create(FileUpload fileupload, HttpPostedFileBase file)
{
if (ModelState.IsValid)
{
if (file != null && file.ContentLength > 0)
{
var fileName = System.IO.Path.GetFileName(file.FileName);
var folder = Server.MapPath("~/UploadArea/" + fileupload.Job);
Directory.CreateDirectory(folder);
Directory.Exists(folder);
var path = System.IO.Path.Combine(Server.MapPath("~/UploadArea/" + fileupload.Job), file.FileName);
file.SaveAs(path);
}fileupload.File = file.FileName;
BlackShadows
Member
4 Points
28 Posts
MVC3 File upload folder creation
Apr 24, 2012 10:55 AM|LINK
I managed to do file upload and create folder but I don't wan use my ID as folder name and due to DB relationship i only Can use ID and the name is from other DB any1 have any idea how to do it? Here is my codes:
[HttpPost]
public ActionResult Create(FileUpload fileupload, HttpPostedFileBase file)
{
if (ModelState.IsValid)
{
if (file != null && file.ContentLength > 0)
{
var fileName = System.IO.Path.GetFileName(file.FileName);
var folder = Server.MapPath("~/UploadArea/" + fileupload.JobID);
Directory.CreateDirectory(folder);
var path = System.IO.Path.Combine(Server.MapPath("~/UploadArea/" + fileupload.JobID), file.FileName);
file.SaveAs(path);
}
fileupload.File = file.FileName;
db.FileUploads.Add(fileupload);
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.JobID = new SelectList(db.Jobs, "JobID", "JobRef", fileupload.JobID);
return View(fileupload);
}
ignatandrei
All-Star
135047 Points
21654 Posts
Moderator
MVP
Re: MVC3 File upload folder creation
Apr 24, 2012 11:34 AM|LINK
Where is the line with your ID ? And what's your ID ?
BlackShadows
Member
4 Points
28 Posts
Re: MVC3 File upload folder creation
Apr 24, 2012 11:47 AM|LINK
my ID is JobID which is random numbers.
the lines is here:
var folder = Server.MapPath("~/UploadArea/" + fileupload.JobID);
...
var path = System.IO.Path.Combine(Server.MapPath("~/UploadArea/" + fileupload.JobID), file.FileName);
I tried JobName but it don't work. :S
ignatandrei
All-Star
135047 Points
21654 Posts
Moderator
MVP
Re: MVC3 File upload folder creation
Apr 24, 2012 12:04 PM|LINK
So it's not your id, is a random id generated for each user. And what;s the problem?
What do you mean by that?
BlackShadows
Member
4 Points
28 Posts
Re: MVC3 File upload folder creation
Apr 24, 2012 12:10 PM|LINK
At the moment it only use the number from the JobID but I want it use my JobName as Folder name which do not exist on FileDB but FileDB have relationship to my JobDB propely that why it's display the JobID but not the JobName.
many thank.
ignatandrei
All-Star
135047 Points
21654 Posts
Moderator
MVP
Re: MVC3 File upload folder creation
Apr 24, 2012 01:13 PM|LINK
So use it. Directory.Exists and Directory.Create
You can display anything. JobName, JObID, current date time combined with jobid....
BlackShadows
Member
4 Points
28 Posts
Re: MVC3 File upload folder creation
Apr 24, 2012 03:32 PM|LINK
um.. How can I combined items under Controller?
Young Yang -...
All-Star
21343 Points
1818 Posts
Microsoft
Re: MVC3 File upload folder creation
Apr 30, 2012 08:21 AM|LINK
Hi
Please try this:
var path = Path.Combine(Server.MapPath("~/UploadArea/"), fileupload.JobName, file.FileName);Hope this helpful
Regards
Young Yang
Feedback to us
Develop and promote your apps in Windows Store
BlackShadows
Member
4 Points
28 Posts
Re: MVC3 File upload folder creation
May 01, 2012 10:47 AM|LINK
@Young Yang
it do not give me JobName from the list due to that table do not have JobName on it. it only hold JobID i have to do some inner join to other table but i not sure how. here is the code, any idea?
public ActionResult Create()
{
ViewBag.JobID = new SelectList(db.Jobs, "JobID", "JobRef");
var FileUpload = new FileUpload();
FileUpload.Date = DateTime.Today;
return View(FileUpload);
}
[HttpPost]
public ActionResult Create(FileUpload fileupload, HttpPostedFileBase file)
{
if (ModelState.IsValid)
{
if (file != null && file.ContentLength > 0)
{
var fileName = System.IO.Path.GetFileName(file.FileName);
var folder = Server.MapPath("~/UploadArea/" + fileupload.Job);
Directory.CreateDirectory(folder);
Directory.Exists(folder);
var path = System.IO.Path.Combine(Server.MapPath("~/UploadArea/" + fileupload.Job), file.FileName);
file.SaveAs(path);
}fileupload.File = file.FileName;
db.FileUploads.Add(fileupload);
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.JobID = new SelectList(db.Jobs, "JobID", "JobRef", fileupload.JobID);
return View(fileupload);
}
Thank.