Save Images into Sql Server Database Table using asp.net 4.0 ajax
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/storeDat开发者_如何学PythonaAndImg",
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?
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
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?
精彩评论