【问题标题】:ASP.NET Kendo UI UploadASP.NET 剑道 UI 上传
【发布时间】:2012-08-11 08:03:39
【问题描述】:

我想使用 Kendo UI 开发一个网站。我可以使用kendo-ui 的其他资格。但是,我不能通过 ASP.NET 使用文件上传。是否有任何示例代码或文档来解决这个问题?

【问题讨论】:

    标签: asp.net upload kendo-ui


    【解决方案1】:

    这就是它对我有用的原因:

    js:

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

    页面:

    <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="" %>
    

    【讨论】:

    • @GerryWhitworth 仅供参考:如果您对答案投赞成票会更好。最好的祝愿,卡斯帕'
    【解决方案2】:

    它通过使用与您的方法类似的方法起作用。我创建了一个 upload.aspx 网络表单并通过以下方式调用它:

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

    我在upload.aspx 的aspx.cs 文件中有这段代码:

    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());
    
            }
        }
    }
    

    没问题。它上传文件,但有一个新问题。如何将结果返回给 kendoui。它会上传,但总是显示错误并重试按钮。在 Kendo-ui 的文档中,它说重新调整空字符串以成功上传。我试过 Response.Write("");但它没有用。

    【讨论】:

    • 回答自己的问题添加更多问题是个坏主意!您应该更新处理新步骤的原始问题。
    【解决方案3】:

    @sanalism 的回答很好,但是上传控件会显示一个错误和一个重试按钮。为避免这种情况,您需要发送 json 回复:

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

    【讨论】:

      【解决方案4】:

      Response.Write("{}"); 将在upload.aspx 中发送整个标签。 发送到剑道UI上传的结果解析为json格式失败。

      因此您必须删除upload.aspx 中&lt;% Page...%&gt; 旁边的所有标签

      【讨论】:

        【解决方案5】:

        这是带有 HTTP 处理程序的示例:

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

        使用系统; 使用 System.Web;

        公共类 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;
            }
        }
        

        }

        【讨论】:

          【解决方案6】:

          配置 async.saveUrl 属性以设置接受 POST 请求的处理程序。 然后使用多部分表单数据解析器(如this one from codeplex)来解析剑道上传发送的数据。 另外,将您的服务配置为接受表单数据:Check this post

          告诉我进展如何!

          【讨论】:

            猜你喜欢
            • 2014-11-19
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2016-06-20
            • 2013-11-07
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多