Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 523
osmanraifgunes Avatar asked Aug 14 '12 23:08

osmanraifgunes


3 Answers

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="" %>
like image 188
Casper Leon Nielsen Avatar answered Nov 03 '22 22:11

Casper Leon Nielsen


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.

like image 28
osmanraifgunes Avatar answered Nov 04 '22 00:11

osmanraifgunes


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("{}");
like image 24
Nicolas Fatoux Avatar answered Nov 03 '22 23:11

Nicolas Fatoux