admin管理员组

文章数量:1315990

Imagine a web page has an img tag which needs to be uploaded. How do I convert this to a file object, so that I can send it over?

And on the other end, I have the hypothetical img, converted to file and sent to me. How do I convert this to a HTML tag?

I have an initial starting point for the latter part:

imageUrl = URL.createObjectURL(file);
image = document.createElement("img");
image.src = imageUrl;

Imagine a web page has an img tag which needs to be uploaded. How do I convert this to a file object, so that I can send it over?

And on the other end, I have the hypothetical img, converted to file and sent to me. How do I convert this to a HTML tag?

I have an initial starting point for the latter part:

imageUrl = URL.createObjectURL(file);
image = document.createElement("img");
image.src = imageUrl;
Share Improve this question edited Jan 14, 2016 at 22:05 Vegetto asked Apr 30, 2014 at 16:08 VegettoVegetto 591 silver badge8 bronze badges 2
  • 1 Wouldn't it be a lot easier to just send the image URL to the server, and then use the serverside to fetch the actual image ? – adeneo Commented Apr 30, 2014 at 16:09
  • I'm trying something crooked. Imagine the server from which the image originated is now down or has crashed. And only my web page has the image. The URL would be practically useless in such a scenario, isn't it? – Vegetto Commented Apr 30, 2014 at 16:14
Add a ment  | 

1 Answer 1

Reset to default 7

If you alredy have your image loaded in html page, you can encode it to base64, then send it to your server by ajax call and save it

Javascript

function getBase64Image(img) {
  // Create an empty canvas element
  var canvas = document.createElement("canvas");
  canvas.width = img.width;
  canvas.height = img.height;

  // Copy the image contents to the canvas
  var ctx = canvas.getContext("2d");
  ctx.drawImage(img, 0, 0);

  // Get the data-URL formatted image
  // Firefox supports PNG and JPEG. You could check img.src to
  // guess the original format, but be aware the using "image/jpg"
  // will re-encode the image.
  var dataURL = canvas.toDataURL("image/png");

  return dataURL.replace(/^data:image\/(png|jpg);base64,/, "");
}

$.ajax({
  type: "POST",
  url: "/yourPage.aspx/YourWebMethod",
  data: "{yourParameterName:'" + JSON.stringify(getBase64Image(yourAlreadyLoadedImage)) + "'}",
  contentType: "application/json; charset=utf-8",
  dataType: "json",
      async: false,
  success: function (data) {
    var result = data.d;
  },
  error: function () { alert('/yourPage.aspx/YourWebMethod'); }
});

Server side you can decode base64 image and save it, for example, in JPEG

C#

public void Base64ToImage(string coded)
{
  System.Drawing.Image finalImage;
  MemoryStream ms = new MemoryStream();
  byte[] imageBytes = Convert.FromBase64String(coded);
  using(var ms = new MemoryStream(imageBytes)) {
    finalImage = System.Drawing.Image.FromStream(ms);

  }

   finalImage.Save(yourFilePath, ImageFormat.Jpeg);

}

本文标签: htmlConvert an img to file and back in JavascriptStack Overflow