Quantcast
Channel: Telerik Forums RSS
Viewing all articles
Browse latest Browse all 94857

Kendo Upload file to C# Web Method

$
0
0

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?

 


Viewing all articles
Browse latest Browse all 94857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>