locked
how to save & upload file in asp.net mvc ? RRS feed

  • Question

  • User1928394251 posted

    hello. i want save my file  that choose in my database and upload it .

    my controller:

    public ActionResult Upload(FormCollection form, HttpPostedFileBase file)
    {

    UploadsViewModel uploadsViewModel = Session["Uploads"] != null ? Session["Uploads"] as UploadsViewModel : new UploadsViewModel();
    uploadsViewModel.ID = long.Parse(form["id"]);

    File upload = new File();
    upload.FileID = uploadsViewModel.Uploads.Count + 1;
    upload.FileName = file.FileName;
    upload.FilePath = "~/Uploads/" + file.FileName;
    //if (file.ContentLength < 4048576)
    //{
    if (file != null)
    {
    file.SaveAs(Server.MapPath(upload.FilePath));
    uploadsViewModel.Uploads.Add(upload);
    Session["Uploads"] = uploadsViewModel;
    }
    //}
    return PartialView("~/Views/Shared/_UploadsPartial.cshtml", uploadsViewModel.Uploads);
    }

    public ActionResult DeleteFile(long id)
    {
    UploadsViewModel viewModel = Session["Uploads"] as UploadsViewModel;
    File file = viewModel.Uploads.Single(x => x.FileID == id);
    try
    {

    System.IO.File.Delete(Server.MapPath(file.FilePath));
    viewModel.Uploads.Remove(file);

    }
    catch (Exception)
    {
    return PartialView("~/Views/Shared/_UploadsPartial.cshtml", viewModel.Uploads);
    }
    return PartialView("~/Views/Shared/_UploadsPartial.cshtml", viewModel.Uploads);
    }


    public ActionResult GetFiles(long Id)
    {
    UploadsViewModel viewModel = Session["Uploads"] as UploadsViewModel;

    return PartialView("~/Views/Shared/_UploadsPartial.cshtml", (viewModel == null ? new UploadsViewModel().Uploads : viewModel.Uploads));
    }

    sorry . i dont know how change my controller code for save file in db .

    please help me . thanks .

    <div class="DARKY_BlueFilter DARKY_Filter"></div> <div class="DARKY_YellowFilter DARKY_Filter"></div> <div class="DARKY_DimFilter DARKY_Filter"></div>

    Sunday, February 10, 2019 10:37 AM

Answers