简体   繁体   English

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

[英]ASP.NET Web Service pass form data with session

I'm trying to upload image via AJAX in ASP.NET Webform. 我正在尝试通过ASP.NET Webform中的AJAX上传图像。 It's going good, accept that when I pass the form data, I can't seem to get access to my session values. 一切顺利,接受当我传递表单数据时,我似乎无法访问我的会话值。

This is what I tried. 这就是我尝试过的。

ASPX (snippet) ASPX(摘要)

<asp:FileUpload runat="server" ID="profileImageFU" CssClass="profile-image-fu"/>
<input type="button" id="profileImageSaveBtn" class="profile-image-save-btn" />
<progress></progress>

JS JS

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

ImageUploaderHandler.ashx (snippet) ImageUploaderHandler.ashx(代码段)

public void ProcessRequest (HttpContext context) {
    string result = (new UserWS()).setProfileImage(context);
}

UserWS (snippet) UserWS(代码段)

[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

    }
}

I have used [WebMethod(EnableSession = true)] in other web services and it works fine. 我在其他Web服务中使用了[WebMethod(EnableSession = true)],并且工作正常。 I guess the error is caused because I am passing form data from AJAX here. 我猜是由于我在这里从AJAX传递表单数据引起的。

this might be a backward approach but maybe cookies are required in the service? 这可能是一种落后的方法,但是服务中可能需要cookie? just a thought from this article (near the end). 只是本文的一个想法(即将结束)。

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

相关问题 ASP.NET Web API会话数据 - ASP.NET Web API Session Data 通过JSON对象将Session对象中的Byte Array传递给Web Service(Asp.Net 2.0 asmx) - Pass Byte Array that was in Session object to a Web Service (Asp.Net 2.0 asmx) through a JSON object ASP.Net Web App:如何将Session传递给新线程? - ASP.Net Web App: How to pass Session to new thread? ASP.NET会话值未传递到Web服务? - ASP.NET Session Values Not Being Carried to a Web Service? jQuery / Ajax和ASP.NET Web服务的会话状态 - Session state with jQuery/Ajax and ASP.NET web service Web服务上的ASP.NET不同的会话变量值 - ASP.NET Different Session Variable Value on the Web Service Web / WCF服务上的ASP.Net会话 - ASP.Net Session over web / WCF Service 如何通过asp.net Web服务传递对象 - how to pass object through asp.net web service 如何将数据从Windows窗体应用程序发送到Asp.Net Web服务应用程序 - How to send data from a Windows Form Application to a Asp.Net Web Service Application 如何从Javascript中的alert()分配一个值给asp.net会话,Cookie,应用程序或其他,以将该值传递给asp.net中的下一个Web表单? - How to assign a value from prompt() in Javascript to asp.net Session, Cookie, Application, or any to pass that value to the next web form in asp.net?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM