Question

OK, here goes.

I have a page that has a form on, name, address etc... I also have a fileupload control to attach an image to the form.

Now I would like to pass the form data and the image file into an ashx file, trying to use $.ajax...

And then in the ashx file insert the record using entity framework.

Now I know I can send the formdata, but how can i get the ashx file to understand that the file is not a string. which it currently thinks it is, and insert it into an SQL image data type.

$("#submit").click(function () {
    var file = $("#file")[0].Files[0];
    var fd = new FormData();
    fd.append("address", $("#street").val() + "; " + $("#city").val() + "; " + $("#postcode").val());
    fd.append("firstname", $("#firstname").val());
    fd.append("lastname", $("#lastname").val());
    fd.append("avatar", file);
    $.ajax({
      type: "POST",
      contentType: false,
      processData: false,
      url: "<%= Page.ResolveUrl("~/GlobalUpdate.ashx?update=info")%>",
      data: fd
    }).success(function (data) {

    });
  });
Was it helpful?

Solution

This looks to me like a normal behavior. It looks to me that The string you're seeing is actually the file contents (bytes) formatted to a base 64 string. If it is, then it's just a matter of getting the bytes out of that base 64 string and store thw bytes in the database. Give it a try and see how it goes

OTHER TIPS

you can never upload file using $.ajax thats a protocol

if you want to upload file along with your form data then you will have to use a plug-in like uploadify

http://www.uploadify.com/

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top