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

HttpPostedFileBase count = 0

3 Answers 1059 Views
Upload
This is a migrated thread and some comments may be shown as answers.
Ursus
Top achievements
Rank 1
Iron
Ursus asked on 14 Jul 2019, 12:15 PM

I have read all (most of) the entries in this forum regarding this matter and cannot seem to find my error. I want to upload a single file that I then pass onto another system. As multiple users could upload at the same time I use a GUID as a message ID and create a folder per user. Here I get the document (from a backend system where I would then like to upload the file to) and create the GUID:

       public IActionResult Open()

        {
            ViewBag.MessageId = Guid.NewGuid().ToString();
            return View("DocumentOpen", new List<string> { "in open event" });
        }

Here is part of my View - I have many fields but I have created a view with only the upload widget:

@using Kendo.Mvc.UI

<form method="post" enctype="multipart/form-data">
    <div class="k-content">
        @(Html.Kendo().Upload()
              .Name("attachments")
              .Multiple(false)
              .Async(async => async
                  .Save("UploadFileAsync", "Document", new { messageId = ViewBag.MessageId })
                  .Remove("RemoveFileAsync", "Document")
                  .AutoUpload(true)
              )
        )
    </div>
</form>

Here the processing of the upload - as you can see the name and the parameter coincide (attachments) and the messageId has the GUID

        [HttpPost]
        public ActionResult UploadFileAsync(IEnumerable<HttpPostedFileBase> attachments, string messageId)
        {
            if (attachments == null) return Content("");

            HttpPostedFileBase fileBase = attachments.FirstOrDefault();
            var fileName = Path.GetFileName(fileBase?.FileName);
            if (fileName != null)
            {
                var serverDirectory = System.Web.HttpContext.Current.Server.MapPath("~/" + messageId);
                var serverPath = Path.Combine(serverDirectory, fileName);
                fileBase.SaveAs(serverPath);
            }


            return Content("");
        }

When running the code I get a GUID in the messageId field, the attachments field has a count of 0 i.e. It is *not* null. I have tried multiple examples here but am always getting the same error. 

 

Any ideas?

 

PS: I cannot upload the project as it needs a backend document management system :(

3 Answers, 1 is accepted

Sort by
0
Ianko
Telerik team
answered on 16 Jul 2019, 12:00 PM
Hello Ursus,

I tested with the demo solution on my end and I was unable to reproduce the same issues. The files posted are correctly posted. You can check that screencast: https://www.screencast.com/t/1P1GQfy3O.

Typically, with the async option you do not need a form. The files are upload through ajax requests. 

I would suggest to isolate the case in a very simple .net project so that I can inspect it locally and see what the problem is. 

Regards,
Ianko
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
0
Ursus
Top achievements
Rank 1
Iron
answered on 16 Jul 2019, 12:07 PM

Hi Ianko,

I have found my error last night, haven't gotten round to informing you, sorry -> The problem I had was the project was created for ASP.NET 4.6.1, my colleague installed .NET Core in the code and was using that to create the Views. I therefore needed to use the .NET Core functions and everything works. Sorry that I did not get back to you this morning (it got late last night :)

 

Thank you again

ursus

0
Ianko
Telerik team
answered on 16 Jul 2019, 12:12 PM
Hi Ursus,

I am glad to see that at the end everything is working fine on your end. 

Regards,
Ianko
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Tags
Upload
Asked by
Ursus
Top achievements
Rank 1
Iron
Answers by
Ianko
Telerik team
Ursus
Top achievements
Rank 1
Iron
Share this question
or