0

I have a form where users are able to upload images to the website. The images are stored in binary, in the table.

I use ajax and JQuery on the site and it is never reloaded so when a users enter the data and push submit, the page is not reloaded. Instead I use ajax to upload the the data to the server.

When I just passthrough text it looks like this:

$("#storeDataAndImgBtn").bind("click", function () {
    var msg = $("#emailMsg").val();
    var from = $("#fromEmail").val();
    $.ajax({
        type: "POST",
        url: "Default.aspx/storeDataAndImg",
        data: "{\"from\":\"" + from + "\" ,\"msg\":\"" + msg + "\" ,\"value\":\" 'image should be here' \" }",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (temp) {
            alert(temp.d);
            $("#mailForm").fadeOut(function () {
                $("#overlay").fadeOut();
            });
        }
    });
    return false;
});

My question is how i can passthrough the image so that I can store it in the database?

Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
Mikael
  • 5,429
  • 5
  • 30
  • 38

2 Answers2

1

I am assuming that the image is just a Input Type="File". If this is the case, you cannot access the Input field using Ajax due to security reasons. I believe the best solution is using an IFrame for the submit target. It's not pretty but it works.

SOF - How to make Asynchronous(AJAX) File Upload using iframe

Your other option is to use a Flash/Silverlight/etc plugin to. I am currently using SWFUpload.

SWFUpload

Community
  • 1
  • 1
Erik Philips
  • 53,428
  • 11
  • 128
  • 150
0

If possible, I would suggest not storing the image in the database. Instead, store images on the server's file-system.

Refer here for similar/related questions: store image in database or in a system file?

Community
  • 1
  • 1
Jeff Dalley
  • 1,311
  • 1
  • 18
  • 31
  • Okay, so say that I actually change so that I store the images onto the server's file-system instead. How do I (without reloading the page, making a post) store the image onto the file-system? – Mikael Aug 23 '10 at 16:44
  • Hmm, using Ajax this doesn't really happen out of the box; but you may find what you need here: http://geekswithblogs.net/rashid/archive/2007/08/01/Create-An-Ajax-Style-File-Upload.aspx and also a similar question/answer here: http://stackoverflow.com/questions/254831/asp-net-free-ajax-file-upload-control – Jeff Dalley Aug 23 '10 at 19:00