This is a migrated thread and some comments may be shown as answers.

upload error without detail

3 Answers 206 Views
Upload
This is a migrated thread and some comments may be shown as answers.
Dario
Top achievements
Rank 1
Veteran
Dario asked on 15 May 2020, 10:09 AM

I'm trying to implement an upload into a window.

this is a code

<!-- >> IMPORT EXCEL-->
 
@{Html.Kendo().Window()
          .Name("importExcelFileWindow")
          .Title("Importa file Excel")
          .Width(500)
          .Height(300)
          .Content(@<text>
        @using (Html.BeginForm("ImportExcelFile", "UploadContacts", FormMethod.Post))
        {
            @(Html.Kendo().Upload()
                                .Name("excelFiles")
                                .Validation(v=>v.AllowedExtensions(new string[] { ".xlsx", ".xls" }))
                                .Async(a => a
                                    .Save("Async_Save", "UploadContacts")
                                    .Remove("Async_Remove", "UploadContacts")
                                    .AutoUpload(true)
                                ).Events(e=>e.Error("uploadError"))
                                )
        }
        </text>)
.Visible(false)
.Render();
}
<script>

function uploadError(e) {
        alert(e.XMLHttpRequest.response);
    }

[...]

<!-- << IMPORT EXCEL-->

 

 

this is a controller

public class AsyncUploadContactsController : Controller
    {
        public IWebHostEnvironment HostingEnvironment { get; set; }
        public string incomingFolder { get; set; }
        private readonly BusinessDbContext _context;
 
        public AsyncUploadContactsController(IWebHostEnvironment hostingEnvironment, BusinessDbContext context)
        {
            HostingEnvironment = hostingEnvironment;
            _context = context;
 
            incomingFolder = "App_Data";
        }
 
        [HttpPost]
        public ActionResult ImportExcelFile()
        {
            return View();
        }
 
        public async Task<ActionResult> Async_Save(IEnumerable<IFormFile> files)
        {
            if (files != null)
            {
                foreach (var file in files)
                {
                    var fileContent = ContentDispositionHeaderValue.Parse(file.ContentDisposition);
 
                    var fileName = Path.GetFileName(fileContent.FileName.ToString().Trim('"'));
                    var physicalPath = Path.Combine(HostingEnvironment.WebRootPath, incomingFolder, fileName);
 
                    using (var fileStream = new FileStream(physicalPath, FileMode.Create))
                    {
                        await file.CopyToAsync(fileStream);
                    }
                }
            }
 
            // Return an empty string to signify success
            return Content("");
        }
 
        public ActionResult Async_Remove(string[] fileNames)
        {
            // The parameter of the Remove action must be called "fileNames"
 
            if (fileNames != null)
            {
                foreach (var fullName in fileNames)
                {
                    var fileName = Path.GetFileName(fullName);
                    var physicalPath = Path.Combine(HostingEnvironment.WebRootPath, incomingFolder, fileName);
 
                    if (System.IO.File.Exists(physicalPath))
                        System.IO.File.Delete(physicalPath);
                }
            }
 
            // Return an empty string to signify success
            return Content("");
        }
    }

 

when I open the window and I try to upload a file, I obtain this effect (attached screen-shots)

 

The strange thing is that if I set a breakpoint into actions, I notice that it doesn't go into an action AsynchSave.

I create App_Data folder into wwwroot node's project (is it corrects?

Where I wrong?

 

3 Answers, 1 is accepted

Sort by
0
Dario
Top achievements
Rank 1
Veteran
answered on 15 May 2020, 10:25 AM

Ok I'm a stupid dev, controller's name was wrong.

Now, it doesn't shows me any error, but it goes inside into Async_Save and it doesn't found any file, I have add one file instead.

Why?

 

0
Ivan Danchev
Telerik team
answered on 20 May 2020, 08:25 AM

Hello Dario,

The name of the Upload is "excelFiles":

.Name("excelFiles")

so the parameter of the action should use the same name:

public async Task<ActionResult> Async_Save(IEnumerable<IFormFile> excelFiles)
{
     //...
}

Regards,
Ivan Danchev
Progress Telerik

Progress is here for your business, like always. Read more about the measures we are taking to ensure business continuity and help fight the COVID-19 pandemic.
Our thoughts here at Progress are with those affected by the outbreak.
0
Dario
Top achievements
Rank 1
Veteran
answered on 19 Jun 2020, 06:52 AM
Great! Thank you!
Tags
Upload
Asked by
Dario
Top achievements
Rank 1
Veteran
Answers by
Dario
Top achievements
Rank 1
Veteran
Ivan Danchev
Telerik team
Share this question
or