0

I want to insert destination files to database like php do..

but I get error, in database insert System.Web.HttpPostedFileWrapper not my desination files, but files success upload to my folder..

and how do I download it in Razor View

this for my controller

public class HomeController : Controller
{
    RahmanEntities db = new RahmanEntities();

    public ActionResult Index()
    {
        return View();
    }

    [HttpPost]
    public ActionResult Index(updown viewModel)
    {

        // extract only the filename
        var fileName = Path.GetFileName(Request.Files[0].FileName);

        // store the file inside ~/App_Data/uploads folder
        var path = Path.Combine(Server.MapPath("~/App_Data/uploads"), fileName);

        try
        {
            if (System.IO.File.Exists(path))
            {
                ModelState.AddModelError("uploadError", "Duplicate Data");
            }
            else
            {
                Request.Files[0].SaveAs(path);
            }

        }
        catch (Exception)
        {
            ModelState.AddModelError("uploadError", "Can't save file to disk");
        }

        if (ModelState.IsValid)
        {
            // put your logic here

            updown item = new updown();
            item.upload = viewModel.upload;
            db.updowns.Add(item);
            db.SaveChanges();

            return RedirectToAction("Index", "Home");

        }

        return View(viewModel);
    }

    public ActionResult List()
    {
        var tes = db.updowns.ToList();

        return View(tes);
    }
}

and then this for my Razor View Upload

@model updownload.Models.updown
@using (Html.BeginForm(null, null, FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    <div>
        @Html.ValidationMessage("uploadError")<br />
        <input type="file" name="upload" id="upload" />
    </div>

    <button type="submit">Upload</button>
}

I dont know create Razor View for Download

can someone help fix me code..

Sorry for my bad english

this for my model

public partial class updown
{
    public int id { get; set; }
    public string upload { get; set; }
}
  • You need to create a link that calls a method which returns a `FileResult` –  Nov 07 '17 at 03:03
  • You can create an `ActionLink` in Razor page referencing action name & controller name to download the file, ensure that target action returns `FileResult`. – Tetsuya Yamamoto Nov 07 '17 at 03:03
  • can you write in code please, @StephenMuecke – Rahman Kryptonz Nov 07 '17 at 03:03
  • Refer [this answer](https://stackoverflow.com/questions/25711271/how-to-download-a-file-to-client-from-server) for an example –  Nov 07 '17 at 03:04
  • An example to get started: `@Html.ActionLink("Download", "DownloadFile", controller_name")` & controller action method: `public FileResult DownloadFile() { ... return File(...); }`. – Tetsuya Yamamoto Nov 07 '17 at 03:04
  • As for your other issue, you need to shown your data model and the view model (the data model appears to contain a property which is `HttpPostedFileBase`) –  Nov 07 '17 at 03:06
  • in my model or controller @StephenMuecke – Rahman Kryptonz Nov 07 '17 at 03:31
  • Sorry, I do not understand your comment - I asked you to edit you question to include you model –  Nov 07 '17 at 03:33
  • that my model @StephenMuecke – Rahman Kryptonz Nov 07 '17 at 03:36
  • You have given you file input `name="upload"` which is the same name as the `string` property in your model. Change it to something else. And then when saving you need to set `item.upload = path;` (not `item.upload = viewModel.upload;` which will always be `null`) –  Nov 07 '17 at 03:41
  • But all this is awful practice and you should have a view model with a property `HttpPostedFileBase File` and in the view `@Html.TextBoxFor(m => m.File)` and then the parameter in you POST method is the view model –  Nov 07 '17 at 03:42
  • can you write full code for FileResult, I stuck in `return File` @TetsuyaYamamoto – Rahman Kryptonz Nov 07 '17 at 07:21

0 Answers0