views:

164

answers:

2

Hi,

I have a form where users are able to upload images to the website. The images are stored, 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?

A: 

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: http://stackoverflow.com/questions/766048/store-image-in-database-or-in-a-system-file

Jeff Dalley
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
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
A: 

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

Erik Philips
I went for SWFUpload, works great!
Mikael