document.querySelector('.bg-ipt').addEventListener('change', e => {
console.log(e.target.files);
const file = e.target.files[0];
const formData = new FormData();
formData.append('img', file);
axios({
url: `https://hmajax.itheima.net/api/uploadimg`,
method: 'POST',
data: formData
}).then(res => {
console.log(res.data.data.url);
const url = res.data.data.url
localStorage.setItem('bg-url', res.data.data.url);
})
})
const bgurl = localStorage.getItem('bg-url');
bgurl && (document.body.style.backgroundImage = `url(${bgurl})`);
- 代码中展示了本地存储和获取的示例 localStorage
- formData上传的使用
- 内联样式使用 document.body.style.backgroundImage