0

I'm trying to upload image via AJAX in ASP.NET Webform. 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)

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

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)

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

UserWS (snippet)

[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. I guess the error is caused because I am passing form data from AJAX here.

kent-id
  • 717
  • 10
  • 25
  • Can you check if using [xhrFields](http://stackoverflow.com/a/7190487/578411) works by setting `withCredtials` to true? – rene Sep 21 '14 at 09:07
  • I tried to add that xhrFields: { withCredentials: true } but still the same problem. Session is null. – kent-id Sep 21 '14 at 09:12

1 Answers1

0

this might be a backward approach but maybe cookies are required in the service? just a thought from this article (near the end).

wazz
  • 4,953
  • 5
  • 20
  • 34
  • I didn't read that article way til the end. I'll try it out tonight when I'm home and will let u know. Thanks! – kent-id Sep 23 '14 at 11:17