C# 如何在服务器端页面方法中获取Ajax发送的已发布文件

C# 如何在服务器端页面方法中获取Ajax发送的已发布文件,c#,jquery,asp.net,ajax,file-upload,C#,Jquery,Asp.net,Ajax,File Upload,这一整天我都在工作。我不想使用任何花哨的插件。我的问题基本上是这样的:在asp.net c#中,用户可以创建一个类的实例(比如student),每个实例都有一个文件(图像)。我使用的是AJAX,代码运行良好。当用户按下create按钮时,我还想将发布的文件发送到Page方法,该方法可以保存文件,同时将记录添加到db中。但是,我无法将发布的文件作为页面方法中的FileInfo获取,而是作为字符串获取。有没有办法让这一切顺利进行 function addBadge() { v

这一整天我都在工作。我不想使用任何花哨的插件。我的问题基本上是这样的:在asp.net c#中,用户可以创建一个类的实例(比如student),每个实例都有一个文件(图像)。我使用的是AJAX,代码运行良好。当用户按下create按钮时,我还想将发布的文件发送到Page方法,该方法可以保存文件,同时将记录添加到db中。但是,我无法将发布的文件作为页面方法中的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和文件上传的文章

似乎直到最近才开始支持它。我知道一个事实,当你发布一个没有ajax的常规表单时,为了发送文件,它必须有enctype的multipart/form数据


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

对于那些需要ajax ASP.net解决方案(提交带有文件的表单)而不使用奇特的文件上载插件的用户:

我在这里特别提到最近的一个解决方案:

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

 [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);
            }
        }
[HttpPost]
公钥值对上载文件()
{
尝试
{
if(HttpContext.Current.Request.Files.AllKeys.Any())
{
//从文件集合中获取上载的图像
var httpPostedFile=HttpContext.Current.Request.Files[“uploadeImage”];
if(httpPostedFile!=null)
{
//验证上载的图像(可选)
//获取完整的文件路径
var fileSavePath=Path.Combine(HttpContext.Current.Server.MapPath(“~/Images/UploadedFiles”),httpPostedFile.FileName);
//将上传的文件保存到“UploadedFiles”文件夹
httpPostedFile.SaveAs(fileSavePath);
返回新的KeyValuePair(true,“文件上载成功”);
}
返回新的KeyValuePair(true,“无法获取上载的文件”);
}
返回新的KeyValuePair(true,“未找到要上载的文件”);
}
捕获(例外情况除外)
{
返回新的KeyValuePair(false,“上载文件时出错。错误消息:”+ex.Message);
}
}
详情请参阅帖子