简体   繁体   中英

ASP.NET Kendo UI Upload

I want to use Kendo UI to develop a web site. I am able to use other qualifications of kendo-ui . However, I cannot use file upload with ASP.NET. Is there any sample code or documentation to overcome this problem?

Here is what made it work for me:

js:

$(document).ready(function () {
    $(".attachmentUpload").kendoUpload({
        async: {
            saveUrl: "SaveAttachment.aspx",
            autoUpload: true
        }
    });
});

page:

<input name="attachmentUpload" id="attachmentUpload" type="file" class="attachmentUpload" />

SaveAttachment.aspx.cs

public partial class SaveAttachment : Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Expires = -1;
        try
        {
            HttpPostedFile postedFile = Request.Files["attachmentUpload"];
            //do something with your postedfile
            Response.ContentType = "application/json";
            Response.Write("{}");
        }
        catch (Exception ex)
        {
            Response.Write(ex.ToString());

        }
    }
}

SaveAttachment.aspx:

<%@ Page Language="C#" CodeBehind="SaveAttachment.aspx.cs" Inherits="Nstar.WebUI.Pages.SaveAttachment" EnableTheming="false" StyleSheetTheme="" Theme="" %>

It has worked by using a method similar to your method. I have created a upload.aspx webform and called it by :

 $("#userImage").kendoUpload({
                async: {
                    saveUrl: "upload.aspx"
                }
            });

I have this code at aspx.cs file of the upload.aspx:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;

public partial class upload : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        ProcessRequest();
    }
    public void ProcessRequest()
    {
        Response.Expires = -1;
        try
        {
            var db = new mypicksDBDataContext();
            HttpPostedFile postedFile = Request.Files["photos"];
            string savepath = Server.MapPath("userFolders/images/");
            string filename = postedFile.FileName;
            if (!Directory.Exists(savepath))
                Directory.CreateDirectory(savepath);
            postedFile.SaveAs(savepath + filename);
            Response.Write("upload");
        }
        catch (Exception ex)
        {
            Response.Write (ex.ToString());

        }
    }
}

It woks fine. It uploads the file but there is a new problem. How can I return the result to kendoui. It uploads but is always shows an error and retry button. In the documentation of Kendo-ui it says retun empty string for successfull upload. I tried Response.Write(""); but it did not work.

The answer from @sanalism is fine, but the upload control will display an error and a retry button. To avoid this, you need to send a json reply :

Response.ContentType = "application/json";
Response.Write("{}");

Response.Write("{}"); will send whole tags in upload.aspx. It's results sent to the Kendo UI upload fails to parse into json format.

Therefore you must remove all tags beside <% Page...%> in upload.aspx

Here is sample with HTTP handler:

<form id="form1" runat="server">
    <input type="file" name="files" id="photos" />
    <script type="text/javascript">
        $("#photos").kendoUpload({
            async: {
               saveUrl: "UploadHandler.ashx"
            }
        });
    </script>
</form>

using System; using System.Web;

public class UploadHandler : IHttpHandler {

public void ProcessRequest(HttpContext context)
{
    try
    {
        HttpFileCollection files = context.Request.Files;
        HttpPostedFile file = files[0];
        int filelength = file.ContentLength;
        byte[] input = new byte[filelength ];
        file.InputStream.Read(input, 0, filelength );

        file.SaveAs(string.Format("C:\\Uploads\\{0}", file.FileName));
    }
    catch (Exception e)
    {
        context.Response.Write("{'error':'" + e.Message + "'}");
    }

    context.Response.Write("");
}

public bool IsReusable
{
    get
    {
        return false;
    }
}

}

Configure async.saveUrl property to set the handler that accepts POST requests. Then use a multipart form data parser (such as this one from codeplex ) to parse the data sent by kendo upload. Also, configure your service to accept the form data: Check this post

Let me know how it goes!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM