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

Uploading files is sending no files to controller

1 Answer 112 Views
Upload
This is a migrated thread and some comments may be shown as answers.
Michael
Top achievements
Rank 1
Michael asked on 26 Nov 2020, 12:34 AM

Hello,

When you select a file to be uploaded, it is not being sent to the controller to be saved to a directory or SQL Server. When the controller Async_MedicalCertificateSave is called the parameter files always has 0 count. 

 

 

Thanks in advance for your help.

 

My code is as follows:

HTML

@(Html.Kendo().Upload()
    .Name("Popupfiles")
    .Multiple(false)
    .Async(a => a
        .Save("Async_MedicalCertificateSave", "SAC")
        .Remove("Async_MedicalCertificateRemove", "SAC")
        .AutoUpload(true)
 
    )
    .Events(events => events
        .Upload("OnUpload")
        .Select("onSelect")
    )
)

 

JAVSCRIPT

function OnUpload(e) {
    var cboStudent = $("#StudentId").data("kendoMultiColumnComboBox");
    e.data = { StudentId: cboStudent.value() };
}
 
function onSelect(e) {
    document.getElementById("OriginalFilename").value = getFileInfo(e);
}

 

CONTROLLER

public async Task<ActionResult> Async_MedicalCertificateSave(IEnumerable<IFormFile> files, int StudentId)
{
    ccwFormsModel db = new ccwFormsModel();
 
    // The Name of the Upload component is "files"
    if (files != null)
    {
        foreach (var file in files)
        {
            var filetype = file.ContentType;
            var fileContent = ContentDispositionHeaderValue.Parse(file.ContentDisposition);
            //var filetype = fileContent.DispositionType;
 
            string dt = DateTime.Now.ToString("yyyyMMdd_HHmm");
            string extension = Path.GetExtension(fileContent.FileName.ToString().Trim('"'));
 
            var fileName = StudentId.ToString() + "_MedicalCert_" + dt + extension;
 
            var physicalPath = Path.Combine(HostingEnvironment.WebRootPath, "App_Data", fileName);
 
            using (var fileStream = new FileStream(physicalPath, FileMode.Create))
            {
 
                var objfiles = new TmpFileUpload()
                {
                    TmpFileId = 0,
                    StudentId = StudentId,
                    Filename = fileName,
                    OriginalFilename = file.FileName,
                    FileType = filetype
                };
 
                using (var target = new MemoryStream())
                {
                    file.CopyTo(target);
                    objfiles.UploadedFile = target.ToArray();
                }
 
                db.TmpFileUploads.Add(objfiles);
                db.SaveChanges();
            }
        }
    }
 
    // Return an empty string to signify success
    return Content("");
}
 
public ActionResult Async_MedicalCertificateRemove(string[] fileNames)
{
    // The parameter of the Remove action must be called "fileNames"
    ccwFormsModel db = new ccwFormsModel();
    if (fileNames != null)
    {
        foreach (var fullName in fileNames)
        {
            var fileName = Path.GetFileName(fullName);
            var physicalPath = Path.Combine(HostingEnvironment.WebRootPath, "App_Data", fileName);
 
            TmpFileUpload lst = db.TmpFileUploads.FirstOrDefault(a => a.OriginalFilename.Equals(fileName));
            db.TmpFileUploads.Remove(lst);
            db.SaveChanges();
        }
    }
 
    // Return an empty string to signify success
    return Content("");
}

1 Answer, 1 is accepted

Sort by
0
Michael
Top achievements
Rank 1
answered on 26 Nov 2020, 02:43 AM

I figured it out.  The name of the parameter is "files", but the name of the control is "Popupfiles".  Change the name of the parameter to "Popupfiles" and all is working.

This means that I need to duplicate my code as I have 2 upload controls on one page.  

Thanks

 

 

Tags
Upload
Asked by
Michael
Top achievements
Rank 1
Answers by
Michael
Top achievements
Rank 1
Share this question
or