0

I want to take an Excel file form the user and want to read it. I can do it on .net Core project with the first code block below as you see. But I have to run it on a .net Framework project now. I have to fix 2 parts. First one is IFormFile comes with AspNetCore.Http.Features package. How can I take the file in .net Framework in sameway? Second one is await file.CopyToAsync(stream); code block. How I can work it on .net Framework project? Rest of the code is OK I guess.

public void ExcelProducts(IFormFile file)
{
    using (var stream = new MemoryStream())
    {
        await file.CopyToAsync(stream);
        using (var package = new ExcelPackage(stream))
        {
            ExcelWorksheet worksheet = package.Workbook.Worksheets[0];
            var rowcount = worksheet.Dimension.Rows;
            for (int row = 2; row <= rowcount; row++)
            {

                try
                {

...
}

And this is my View.

<div class="text-center">
    <div class="container">
        <form method="post" action="Market/ExcelProducts" enctype="multipart/form-data">
            <input type="file" name="file" />
            <button type="submit">Submit</button>
        </form>
    </div>
</div>
burnsi
  • 6,194
  • 13
  • 17
  • 27
  • In .NET Framework, the equivalent method to `await file.CopyToAsync(stream)` would be `await Task.Factory.FromAsync(file.BeginCopyTo, file.EndCopyTo, stream, null)`. – Sebastian Siemens Feb 14 '23 at 12:04
  • @SebastianSiemens it didnt give error. How can I take file in .net framework instead IFormFile file input, do you have any idea about it? – Mert Çakar Feb 14 '23 at 12:11
  • Di dyou try `ExcelProducts(HttpPostedFileBase file)`? you can read this description to see how to use HttpPostedFileBase: https://www.compilemode.com/2016/04/uploading-files-in-Asp-Net-mvc-using-HttpPostedFileBase.html – Sebastian Siemens Feb 14 '23 at 13:58

0 Answers0