Home > OS >  How to upload file format .tar/.rar/.zip in .net core mvc using .net 6 c#
How to upload file format .tar/.rar/.zip in .net core mvc using .net 6 c#

Time:12-15

I have below code:

<div >
<div >
    <form method="post" enctype="multipart/form-data">
        <input type="file" name="file" asp-for="MyFile" />

        <input type="submit"asp-controller="Home" asp-action="AddData" />
    </form>
</div>

but it not work when upload file with format .tar/.rar/.zip

CodePudding user response:

You need to update your controller code completely. I tested it here and it can be uploaded:

View:

@model  WebApplication192.Models.FileModel
<form enctype="multipart/form-data" method="post">
    <dl>
        <dt>
            <label asp-for="myfile"></label>
        </dt>
        <dd>
            <input asp-for="myfile" type="file">
            <span asp-validation-for="myfile"></span>
        </dd>
    </dl>
    <input asp-page-handler="Upload"  type="submit" value="Upload" />
</form>

Controller(Simple test use):

public class TestController : Controller
    {
        [HttpPost]
        public async Task<IActionResult> Upload(FileModel file)
        {

            //Do something with the files here

            return View();
        }

        [HttpGet]
        public IActionResult Upload()
        {
            return View();
        }
    }

Model:

public class FileModel
    {
        public IFormFile myfile { set; get; } = null;
    }

Result:

enter image description here

  • Related