使用一个图像文件,我得到了一个图像的url,需要发送到一个Web服务.从那里图像必须保存在我的系统本地.

我使用的代码是:

var imagepath = $("#imageid").val();// from this getting the path of the selected image

that var st = imagepath.replace(data:image/png or jpg; base64"/"");

How to convert the image url to BASE64?

推荐答案

HTML

<img id=imageid src=https://www.google.de/images/srpr/logo11w.png>

JavaScript

function getBase64Image(img) {
  var canvas = document.createElement("canvas");
  canvas.width = img.width;
  canvas.height = img.height;
  var ctx = canvas.getContext("2d");
  ctx.drawImage(img, 0, 0);
  var dataURL = canvas.toDataURL("image/png");
  return dataURL.replace(/^data:image\/(png|jpg);base64,/, "");
}

var base64 = getBase64Image(document.getElementById("imageid"));

This method requires the canvas element, which is perfectly supported.

Jquery相关问答推荐

退出 jquery.each 循环后

在 jQuery 事件中控制this的值

使用 JavaScript/jQuery 重定向到 ASP.NET MVC 中的另一个页面

Bootstrap 4 多选下拉菜单

扩展 jQuery 插件的最佳方式

jQuery从下拉列表中删除一个选项,给定选项的文本/值

JQuery 数据 Select 器不使用 .data 更新

jQuery ajax() 使用成功、错误和完成与 .done()、.fail() 和 always()

jQuery UI 自动完成宽度未正确设置

使用 jQuery 从 JavaScript 对象中添加/删除项目

Onchange 通过 Select 打开 URL - jQuery

如何使用jQuery动态设置宽度和高度

jQuery.extend 和 jQuery.fn.extend 的区别?

如何使用 jQuery 更改文本

将多个 JavaScript 文件合并为一个 JS 文件

设置 JQuery event.preventDefault() 时绕过 window.open 上的弹出窗口阻止程序

使用 jquery 禁用文本框?

bootstrap 日期和时间 Select 器

粘性侧边栏:向下滚动时固定在底部,向上滚动时固定在顶部

如何使用 jQuery 处理复选框的更改?