繁体   English   中英

如何在服务器端Page方法中获取Ajax发送的发布文件

[英]how to get the posted file sent by Ajax in the server-side Page method

我整天都在工作。 我不想使用任何精美的插件。 我的问题基本上是这样的:在asp.net c#中,用户可以创建一个类的实例(假设是学生),每个实例都有一个文件(图像)。 我正在使用AJAX,并且代码工作正常。 当用户按下创建按钮时,我还希望将发布的文件发送到Page方法,该方法可以保存文件,同时将记录添加到db。 但是,我无法在Page方法中以FileInfo的形式获取发布的文件,而是以字符串的形式获取。 有没有办法使这项工作?

function addBadge() {
            var badgeName = $('#txtBadgeName').val();
            var badgeDesc = $('#txtBadgeDesc').val();
            var badgeImage = $('#file_BadgeImage').get().files[0];
            $.ajax({
                type: "POST",
                url: "/Instructor/ManageBadges.aspx/CreateBadge",
                data: "{badgeName:'" + badgeName + "', \
                        badgeImage:'" + badgeImage + "',\
                        badgeDescription:'" + badgeDesc + "'}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (data) {
                    .....
                    }
                }
            });

        }


[WebMethod]
    public static string CreateBadge(string badgeImage, string badgeName, string badgeDescription)
    {
        //HERE HOW CAN USE badgeImage ??
        //HOW CAN I CONVERT IT TO FileInfo and save ?

        Guid badgeId = Guid.NewGuid();
        BadgeInfo newbadge = new BadgeInfo();
        newbadge.BadgeId = badgeId;
        newbadge.BadgeName = badgeName;
        newbadge.BadgeDescription = badgeDescription;

    }

首先,我会尝试将内容类型设置为:

 contentType:'multipart/form-data'

然后,我将阅读有关AJAX和文件上传支持的文章。

jQuery Ajax文件上传

似乎只有最近它才开始受支持。 我知道一个事实,当您发布不带ajax的常规表单时,它必须具有enctype'multipart / form-data才能发送文件。

老实说,我对ASP C#方面的文件操作不熟悉,但是,我知道在进入该方面之前您会遇到一些问题。

对于那些需要ajax asp.net解决方案(使用文件提交表单)而不使用精美文件上传插件的用户:

我在这里强烈参考最近的解决方案: 在ASP.NET Web API中使用jQuery AJAX上传文件

指示的解决方案使用处理文件上传的控制器,并从ajax调用控制器的fileupload功能。 假设您有一种将表格输入保存到数据库的ajax方法。 成功完成此ajax之后,在成功事件中,您需要编写另一段ajax代码以执行在控制器类中定义的文件上传,如下所示:

 [HttpPost]
        public KeyValuePair<bool, string> UploadFile()
        {
            try
            {
                if (HttpContext.Current.Request.Files.AllKeys.Any())
                {
                    // Get the uploaded image from the Files collection
                    var httpPostedFile = HttpContext.Current.Request.Files["UploadedImage"];

                    if (httpPostedFile != null)
                    {
                        // Validate the uploaded image(optional)

                        // Get the complete file path
                        var fileSavePath = Path.Combine(HttpContext.Current.Server.MapPath("~/Images/UploadedFiles"), httpPostedFile.FileName);

                        // Save the uploaded file to "UploadedFiles" folder
                        httpPostedFile.SaveAs(fileSavePath);

                        return new KeyValuePair<bool, string>(true, "File uploaded successfully.");
                    }

                    return new KeyValuePair<bool, string>(true, "Could not get the uploaded file.");
                }

                return new KeyValuePair<bool, string>(true, "No file found to upload.");
            }
            catch (Exception ex)
            {
                return new KeyValuePair<bool, string>(false, "An error occurred while uploading the file. Error Message: " + ex.Message);
            }
        }

有关详细信息,请参阅该帖子。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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