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

Sample .NET Webservice for Async

9 Answers 393 Views
Upload
This is a migrated thread and some comments may be shown as answers.
Jeremy
Top achievements
Rank 1
Jeremy asked on 28 Jun 2012, 05:37 PM
Does anyone have a sample for writing the Async handlers for the upload control using webservices in .net?

I'll be your best friend.



9 Answers, 1 is accepted

Sort by
0
Majid
Top achievements
Rank 2
answered on 26 Sep 2012, 04:32 AM
looking for the same. 
plz help
0
Ty Kang
Top achievements
Rank 1
answered on 26 Sep 2012, 02:38 PM
I have a pretty full example. I can supply a little later today.
0
Majid
Top achievements
Rank 2
answered on 26 Sep 2012, 04:15 PM
Waiting for you :)
0
Jeremy
Top achievements
Rank 1
answered on 26 Sep 2012, 04:54 PM
Here you go.  (I work with Ty Kang) 

Attached are the core of the files I use. It does the following:

Renders a Kendo grid of attachments, and wires up an upload control.
The Upload control communicates with a .NET handler.
On upload data is passed to the handler from javascript
The handler received the data, parses it, saves the files.
Error and Success events are then handled.

You'll have to modify the code, as it contains some of my specific actions that you will need, but I left some extra stuff in, in case it helps.

0
Majid
Top achievements
Rank 2
answered on 27 Sep 2012, 03:37 AM
Thanks Jeremy for helping me out.
I actually was trying to upload files via a RESTful WCF service.
But now i have created a handler in my project to save the uploaded files. 
And now although files are being uploaded sucessfully to the server, but a problem is there, Kendo upload control is showing that file is not uploaded and a retry button is showing on the control.
0
Majid
Top achievements
Rank 2
answered on 27 Sep 2012, 04:31 AM
I have made it.   :)
I had to send httpStatusCode "OK" in response.

Once again thanks for helping.
0
Jeremy
Top achievements
Rank 1
answered on 27 Sep 2012, 03:15 PM
I am actually having the same issue of the control not properly knowing if it is fail or success.  

What do you mean by  httpStatusCode "OK" ?

How about for my benefit and other who will find this thread later, can you post your sample handle as well?


0
Majid
Top achievements
Rank 2
answered on 27 Sep 2012, 04:33 PM
Sure! Why not
So here it goes...

My handlerFile looks like this :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Net;


namespace WebApplicationCE.Admin
{
    /// <summary>
    /// Summary description for FileHandler
    /// </summary>
    public class FileHandler : IHttpHandler
    {


        public void ProcessRequest(HttpContext context)
        {
            try
            {
                if (!string.IsNullOrEmpty(context.Request.QueryString["upload"]))
                {
                    HttpPostedFile postedFile = context.Request.Files["files"];
                    string filepath = context.Server.MapPath("AdminFiles");
                    string filename = postedFile.FileName;
                    if (!Directory.Exists(filepath))
                        Directory.CreateDirectory(filepath);
                    postedFile.SaveAs(Path.Combine(filepath, filename));
                    context.Response.StatusCode = (int)HttpStatusCode.OK;
                }
                else if (!string.IsNullOrEmpty(context.Request.QueryString["remove"]))
                {
                    string filepath = context.Server.MapPath("AdminFiles");
                    string filename = context.Request.Form["fileNames"];
                    FileInfo file = new FileInfo(Path.Combine(filepath, filename));
                    file.Delete();
                    context.Response.StatusCode = (int)HttpStatusCode.OK;
                }
               
            }
            catch (Exception ex)
            {
                 context.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
            }
        }


        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}

And here is the calling code :

<input name="files" id="files" type="file"/>
<script type="text/javascript">
    $('files').kendoUpload({
                            async: {
                                saveUrl: "FileHandler.ashx?upload=true",
                                removeUrl: "FileHandler.ashx?remove=true",
                                autoUpload: true
                            },
                            success: fileUploadSuccess,
                            error: fileUploadError
                        });
                    }
                    function fileUploadSuccess(result) {
                       alert(result.XMLHttpRequest.statusText);
                    }
                    function fileUploadError(result) {
                      alert(result.XMLHttpRequest.statusText);
                    }

</script>
0
Gerry
Top achievements
Rank 1
answered on 23 Dec 2014, 06:41 PM
I guess this question is not worth their time to answer.  I hope that I have understood the problem.

*** JS code
$("#upload").kendoUpload({
        multiple: false,
        async: {
            saveUrl: "/DataService/Upload.ashx",
            autoUpload: false
        },
        template: kendo.template($('#fileTemplate').html()),
    }).closest(".k-upload").find("span").text("Select One Bulk Load Excel Spread Sheet...");  

**** c# => NOTE that I store this handler in a DataService folder
public class Upload : IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
            context.Response.Expires = -1;
            try
            {
                HttpPostedFile postedFile = context.Request.Files["upload"];
                //do something with your postedfile
                context.Response.ContentType = "application/json";
                context.Response.Write("{}");
            }
            catch (Exception ex)
            {
                context.Response.Write(ex.ToString());

            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }



Tags
Upload
Asked by
Jeremy
Top achievements
Rank 1
Answers by
Majid
Top achievements
Rank 2
Ty Kang
Top achievements
Rank 1
Jeremy
Top achievements
Rank 1
Gerry
Top achievements
Rank 1
Share this question
or