MVC3文件上载文件夹创建

时间:2012-04-24 10:49:25

标签: asp.net-mvc-3 file-upload

我设法做文件上传和创建文件夹,但我不会使用我的ID作为文件夹名称,由于数据库关系我只能使用ID而名称是来自其他数据库any1有任何想法怎么做?这是我的代码:

[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);
}

0 个答案:

没有答案