Javascript 能够在其他功能中访问本地存储映像
我正在尝试创建一个应用程序,在其中我有本地存储条目,其中包含一些用于创建图像的信息。我有一个函数来处理图像的读取,还有一个函数在提交表单时创建条目。现在,我需要将图像传递给在提交表单时创建条目的函数。我该怎么做 用于文件处理的函数:Javascript 能够在其他功能中访问本地存储映像,javascript,html,local-storage,Javascript,Html,Local Storage,我正在尝试创建一个应用程序,在其中我有本地存储条目,其中包含一些用于创建图像的信息。我有一个函数来处理图像的读取,还有一个函数在提交表单时创建条目。现在,我需要将图像传递给在提交表单时创建条目的函数。我该怎么做 用于文件处理的函数: $('#image').on('change', handleFileSelect); function handleFileSelect(e) { var photos = e.target.files; for (var i = 0; i &l
$('#image').on('change', handleFileSelect);
function handleFileSelect(e) {
var photos = e.target.files;
for (var i = 0; i < photos.length; i++) {
var photo = photos[i];
if (!photo.type.match('image.*')) {
continue;
}
var reader = new FileReader();
reader.onload = function(e) {
var img = e.target.result;
// I want my image to be in the json_entry I create in submitForm();
}
reader.readAsDataURL(photo);
}
}
$('#image')。在('change',handleFileSelect)上;
功能手柄文件选择(e){
var photos=e.target.files;
对于(var i=0;i
提交表单和创建条目时的函数:
function submitForm(event) {
event.preventDefault();
// Create new JSON entry
var json_entry = {'title': $("#title").val(),
'image': // I want my image to be in here,
'content': $("#story").val(),
'location': $("#locatie").val(),
'location_lat': $("#locatie_lat").val(),
'location_lon': $("#locatie_lon").val(),
'date': getCurrentDate(),
'key': Math.round(new Date().getTime() / 1000)};
//Put the entry in localstorage
localStorage.setItem(Math.round(new Date().getTime() / 1000), JSON.stringify(json_entry));
//Add the entry to the overview
$('#overview').append('<article class="diary_item" rel="'+json_entry.key+'"><h2>'+json_entry.title+'</h2><div style="position:relative;"><img class="entry_img" src="'+json_entry.image+'"><div class="location"><img src="gfx/location.svg"> '+json_entry.location+'</div><img class="delete" src="gfx/close.svg" /></div><p>'+json_entry.content+'<span class="time">Added on: '+json_entry.date+'</span></p></article>');
}
函数提交表单(事件){
event.preventDefault();
//创建新的JSON条目
var json_entry={'title':$(“#title”).val(),
'image'://我希望我的图像在这里,
'内容':$(“#故事”).val(),
'location':$(“#locatie”).val(),
'location_lat':$(“#locatie_lat”).val(),
'location_lon':$(“#locatie_lon”).val(),
“日期”:getCurrentDate(),
“key”:Math.round(new Date().getTime()/1000)};
//将条目放入本地存储
setItem(Math.round(new Date().getTime()/1000)、JSON.stringify(JSON_条目));
//将条目添加到概述中
$(“#概述”).append(“+json#entry.title+”+json#entry.location+”“+json#entry.content+”添加于:“+json#entry.date+””);
}
我希望我的问题是明确的,有人可以帮助我。我似乎真的无法让它工作:(您已经决定将图像作为数据uri字符串进行传递和存储 据我所知,问题是如何将该字符串传递给另一个函数。有一种非常简单的方法可以做到这一点。只需将其存储在全局变量中