C# ASP.NET Web服务通过会话传递表单数据

C# ASP.NET Web服务通过会话传递表单数据,c#,web-services,session,webforms,C#,Web Services,Session,Webforms,我正在尝试通过ASP.NETWebForm中的AJAX上传图像。这很好,当我传递表单数据时,我似乎无法访问我的会话值 这就是我试过的 ASPX(代码片段) ImageUploaderHandler.ashx(片段) UserWS(代码片段) 我在其他web服务中使用过[WebMethod(EnableSession=true)],它工作得很好。我猜这是因为我在这里从AJAX传递表单数据造成的。这可能是一种反向方法,但服务中可能需要cookies?(接近尾声)只是一个想法。您可以通过将withCr

我正在尝试通过ASP.NETWebForm中的AJAX上传图像。这很好,当我传递表单数据时,我似乎无法访问我的会话值

这就是我试过的

ASPX(代码片段)

ImageUploaderHandler.ashx(片段)

UserWS(代码片段)


我在其他web服务中使用过[WebMethod(EnableSession=true)],它工作得很好。我猜这是因为我在这里从AJAX传递表单数据造成的。

这可能是一种反向方法,但服务中可能需要cookies?(接近尾声)只是一个想法。

您可以通过将
withCredtials
设置为true来检查使用是否有效吗?我尝试添加xhrFields:{withCredentials:true},但仍然存在相同的问题。会话为空。我直到最后才读到这篇文章。今晚回家后我会尝试一下,然后告诉你。谢谢!
<asp:FileUpload runat="server" ID="profileImageFU" CssClass="profile-image-fu"/>
<input type="button" id="profileImageSaveBtn" class="profile-image-save-btn" />
<progress></progress>
$(document).ready(function () {
        var file, name, size, type;
        $('.profile-image-fu').change(function () {
            file = this.files[0];
            name = file.name;
            size = file.size;
            type = file.type;
        });
        $('.profile-image-save-btn').click(function (e) {
            e.preventDefault();
            var fileInput = $('.profile-image-fu')[0];
            var fileData = $(fileInput).prop("files")[0];   // Getting the properties of file from file field
            var formData = new window.FormData();                  // Creating object of FormData class
            formData.append("file", fileData); // Appending parameter named file with properties of file_field to form_data

            $.ajax({
                url: 'ImageUploaderHandler.ashx',
                data: formData,
                processData: false,
                contentType: false,
                type: 'POST',
                success: function (data) {
                    alert('success!');
                },
                error: function (errorData) {
                    alert('error!');
                }
            });
        });
    });
public void ProcessRequest (HttpContext context) {
    string result = (new UserWS()).setProfileImage(context);
}
[WebMethod(EnableSession = true)]
public string setProfileImage(object context)
{
    ....
    // Get the uploaded image from the Files collection (WORKS GREAT, I put a break point here and I see postedFile contains all the details I need)
    HttpPostedFile postedFile = HttpContext.Current.Request.Files[0];
    if (HttpContext.Current.Session["User"] != null) {
        // PROBLEM: I need to do my code here, but my Session["User"] is null, even though it's not.
        // EDIT: My HttpContext.Current.Session = null

    }
}