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("{}");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With