0

I am looking for a solution on how to send a file using a form to an asp application. I am writing in the asp .net framework MVC Razor.

My form:

<div class="container">
    <div class="col-md-2">
        @using (Html.BeginForm("Data", "Admin", FormMethod.Post, new { encrypte = "multipart/form-data"}))
        {
            <div class="form login-form">
                <label for="username" class="text-info">Wczytaj plik:</label>
                <input type="file" name="file" id="file" />
            </div>
            <div id="register-link" class="text-right">
                <input type="submit" class="btn btn-success" value="Importuj" />
            </div>
            @ViewBag.Message
        }
    </div>
</div>

My controller:


        [HttpPost]
        public ViewResult Data(HttpPostedFile file = null)
        {
            if(file != null && file.ContentLength > 0)
            {
                string path = Path.Combine(Server.MapPath("~/Upload/Data"), Path.GetFileName(file.FileName));
                file.SaveAs(path);
                ViewBag.Message = "Succes";
            }

            return View("AdminDataView", students);
        }

Unfortunately, the above code does not work, am I doing something wrong with it, is there another option to upload the file to asp?

  • 1
    "The above code does not work" is there an error? Are you getting the success message? Did you try to debug? – Jerdine Sabio Mar 17 '20 at 18:52
  • I have no errors. So I debugged the code: I load the file and send the form, in the controller the object passed is empty and returns to the original view. –  Mar 17 '20 at 19:22

2 Answers2

0

I think there is a typo and i suggest you that use ActionResult instead of ViewResult.

Defference between ActionResult & ViewResult

the typo is: new { enctype="multipart/form-data"}) not new { encrypte = "multipart/form-data"})

Sample Code:

View

@using(Html.BeginForm("UploadFile","Upload", FormMethod.Post, new { 
enctype="multipart/form-data"}))  
{  
    <div>  
        @Html.TextBox("file", "", new {  type= "file"}) <br />  
        <input type="submit" value="Upload" />  
        @ViewBag.Message  
    </div>     
}

Controller

    [HttpPost]  
    publicActionResultUploadFile(HttpPostedFileBase file)  
    {  
        try  
        {  
            if (file.ContentLength > 0)  
            {  
                string _FileName = Path.GetFileName(file.FileName);  
                string _path = Path.Combine(Server.MapPath("~/UploadedFiles"), _FileName);  
                file.SaveAs(_path);  
            }  
            ViewBag.Message = "File Uploaded Successfully!!";  
            return View();  
        }  
        catch  
        {  
            ViewBag.Message = "File upload failed!!";  
            return View();  
        }  
    } 
Shadman
  • 131
  • 1
  • 10
0

Please refer below link this gives you better understanding.

https://stackoverflow.com/a/60519052/7761461

Hope this will surely help you.

Niraj
  • 89
  • 1
  • 5