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

Kendo Upload file to C# Web Method

1 Answer 245 Views
Upload
This is a migrated thread and some comments may be shown as answers.
Canton
Top achievements
Rank 1
Canton asked on 18 Jun 2015, 07:48 PM

I am using a Kendo upload to save files to my server. However, I would like to send the files to a C# web method in a web service where I then perform the code that saves the files to the server.

I am having difficulty when trying to call my web method.

Here is what I have:

  [WebMethod]
        //Saves attachment files to server
        public string UploadAttachments(IEnumerable<HttpPostedFileBase> files)
        {
            string returnMessage = "Copying Attachments...";
            try
            {
                  // The Name of the Upload component is "files"
            if (files != null)
            {
                foreach (var file in files)
                {
                    // Some browsers send file names with full path.
                    // We are only interested in the file name.
                    var fileName = Path.GetFileName(file.FileName);
                    var physicalPath = Path.Combine(Server.MapPath("~/App_Data/Attachments"), fileName);
                    file.SaveAs(physicalPath);
                }
            }
        }
          
            catch (Exception e)
            {
                returnMessage = "Fail";
            }

            return returnMessage;

        } //End

 

Here is the upload input on my webpage and the javascript I am using:

   <h2>Attachments:</h2>
<input id="files" type="file" name="files" />

 <script>
        //when adding attachments
        $(document).ready(function () {
            $("#files").kendoUpload({
                async: {
                    saveUrl: "../WebService.asmx/UploadAttachments",
                    removeUrl: "remove",
                    multiple: true,
                    autoUpload: true
                }
            });
        });
    </script>

 

I cannot get it to call my method when trying to pass the "IEnumerable<HttpPostedFileBase> files" , but it will hit the method if I take that part out, but then I cannot perform the rest of the code within the method.

Currently, when I try to upload a file, it automatically says it has failed and never calls my web method.

Am I missing a step somewhere?

 

1 Answer, 1 is accepted

Sort by
0
Boyan Dimitrov
Telerik team
answered on 22 Jun 2015, 03:47 PM

Hello Canton,

 

Please refer to the answer in the following thread and the provided demo. 

 

Regards,
Boyan Dimitrov
Telerik
 
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
 
Tags
Upload
Asked by
Canton
Top achievements
Rank 1
Answers by
Boyan Dimitrov
Telerik team
Share this question
or