html实现图片上传

html-css022

html实现图片上传,第1张

html:

<section class="upload-section">

  <article class="upload-piclist">

    <div class="upload-file">

        <input type="file" id="file" accept="image/*" multiple onchange="imgChange()"/>

    </div>

  </article>

</section>

css:

/* body {

  margin: 0

  padding: 0

  max-width: 414px

  margin: 0 auto

  } */

  .upload-fh {

  background-image: url('data:image/pngbase64,iVBORw0KGgoAAAANSUhEUgAAADwAAAA8CAYAAAA6/NlyAAAD1klEQVRoQ+XbTYhWVRgH8P8fwZUguFKi2pShbvqg2lhhG0PJlc40zQg62WhDg+jklIo1wViUNgwVTGSWWMngyvCjVdLHLrHaJaOBIIK2DHL7lweegcvL48D7vveee7xztu+d972/Ofec8zznPJe4T5qkRQC2AVgP4A8AMySvt3v7bPcP6rhe0lIAJwFsKvz+CZLb272f7MGSHnDsiy24WZKPNQosyUDWs88EsEGS3zQGLOkpx64OUG+T/LhdrF2f5SMt6QXHPhSghklOd4LNEixpo2OXBaitJL/rFJsdWFKvYxe3oO4A6Cd5phtsVmBJrwH4KgD9C+BVkj91i80GLGk3gKkA9I9jfy8DmwVY0kEAEwHoL8f+XRa2drCkDwG8E4B+A9BH8maZ2FrBkj4D8GYA+hHAKyT/KxtbG1iSRUiWCLS2045VFdjkYEm23HwPYHMAOk5yR1XQue9NFmlJskDCsC8FqCmSe6rGJuthSRYiGnZtgJogeSgFNglYkgX/hn08QI2RPJIKWzlYkqV1hn0kQL1B8ouU2ErBkixhN+zyADVA0j5L3iqZtCTZVoyBlrSILAmwNfZscqn/YOlgSf0AohTOkoBekj/XhS39kZa0E0A0Li0JMOzlOrGlgiWNAjgagCwJ6CF5tW5saWBJ7wEYD0CWBBj2Vg7YUsCSbB19KwBdcOz/uWC7BkuyzbRdAchOBfpygs7dS8eztKRvAQwEqGMkh3LEdtzDkj4CMBagJkna5JVt66iHJUX56jjJ97OVdhN4SLJtGdueaW3N7GFTLqgxPNetC2qWLqDtUGtf8Hif93CyOetwAf0ugGjC+tXRzYm0Cui9AD4JevpPRzcnli6gXwfwZYC+5mGm4WttHa3D892xJAspTwXX3Pae/qVOcelgX7JednS042F58bm60JWAHb3Ot3lWBDg7N5qpA10Z2NFPO/rRADdE8lhqdKVgR69y9BMBbpTkZEp05WBHP+jo5wJc0qQjCdjRdrZkOfSGAJ0s6UgGdrSdHhq6J0An2ThICi4EKMcBDAZom7mtgKUZ58NFoCQrYrFiltZmSYehm1MBUOjpwwAOBGhLOgzdnBqPAno/gA8CtMXdhr5S5rJVyxhuBUgaAfBpALOkw9CXykJnAfYZ3Iq9vw5glnQY+mIZ6GzAjt7iJ49RraWhf+gWnRXY0RaY2Frd/GrawkT2vFfVPhz0aLPqpQvoJx29JkA3qyK+gF7p6GcD9AjJz9sd09mN4WDJutdbLTdIRq8IzPs/yB7sE9nCeW+p8Hjbm2nDAKwcahbAdGPfTGt3nM53/V00ckNMKWY8LAAAAABJRU5ErkJggg==')

  background-repeat: no-repeat

  background-size: 100% 100%

  height: 30px

  width: 30px

  }

  .upload-hedaer {

  height: 55px

  display: grid

  grid-template-columns: repeat(3, 1fr)

  padding: 0 10px

  box-sizing: border-box

  align-items: center

  text-align: center

  background: #287cff

  color: #fff

  border-bottom: 1px solid #efefef

  font-size: 19px

  }

  .upload-hedaer div:last-child {

  text-align: right

  }

  .upload-textarea {

  width: 100%

  height: 60px

  font-size: 28px

  border: 1px solid #efefee

  max-height: 300px

  }

  .upload-article-text {

  width: 100%

  padding: 10px

  box-sizing: border-box

  }

  .upload-file {

  position: relative

  background: url('../images/z_add.png') no-repeat 50%/100% 100%

  /* width: 100px*/

  height: 120px

  order: 9

  }

  .upload-piclist {

  padding: 0 10px

  box-sizing: border-box

  display: grid

  grid-template-columns: repeat(3, 120px)

  justify-content: space-between

  grid-gap: 14px

  }

  #file {

  width: 100%

  height: 100%

  opacity: 0

  }

  .upload-Picitem {

  width: 100%

  height: 120px

  }

  .upload-Picitem>img {

  width: 100%

  height: 100%

  object-fit: cover

  }

  .submit {

  padding: 15px 0

  background-color: #287cff

  color: #fff

  text-align: center

  margin: 10px

  font-size: 20px

  border-radius: 10px

  }

  .upload-sm {

  padding: 10px

  box-sizing: border-box

  color: gray

  }

  .upload-sm ol>li {

  margin-bottom: 10px

  }

js:let picmax = 9//限制上传数量

function imgChange() {

let file = document.getElementById('file').files

let imglist = document.querySelectorAll('.upload-Picitem')

let piclist = document.getElementsByClassName('upload-piclist')[0]

let filelist = file.length + imglist.length >picmax ? 9 - imglist.length : file.length + imglist.length

if (file.length + imglist.length >= 9) {

let uploadfile = document.getElementsByClassName('upload-file')[0]

uploadfile.style.display = "none"

}

for (let i = 0i <filelisti++) {

readerfile(file[i]).then(e =>{

let html = document.createElement('div')

html.className = 'upload-Picitem'

html.innerHTML = '<img src=' + e + ' alt="pic">'

piclist.appendChild(html)

})

}

}

function readerfile(file) {

return new Promise((resolve, reject) =>{

let reader = new FileReader()

reader.addEventListener("load", function() {

resolve(reader.result)

}, false)

if (file) {

reader.readAsDataURL(file)

}

})

}

//提交

function submit() {

let imglist = []

let text = document.getElementsByClassName('upload-textarea')[0].value

let piclist = document.querySelectorAll('.upload-Picitem')

for (let i = 0i <piclist.lengthi++) {

imglist.push(piclist[i].lastChild.src)

}

console.log("发布内容:", text)

console.log("图片列表:", imglist)

}

//textarea高度自适应

var autoTextarea = function(elem, extra, maxHeight) {

extra = extra || 0

var isFirefox = !!document.getBoxObjectFor || 'mozInnerScreenX' in window,

isOpera = !!window.opera &&!!window.opera.toString().indexOf('Opera'),

addEvent = function(type, callback) {

elem.addEventListener ?

elem.addEventListener(type, callback, false) :

elem.attachEvent('on' + type, callback)

},

getStyle = elem.currentStyle ? function(name) {

var val = elem.currentStyle[name]

if (name === 'height' &&val.search(/px/i) !== 1) {

var rect = elem.getBoundingClientRect()

return rect.bottom - rect.top -

parseFloat(getStyle('paddingTop')) -

parseFloat(getStyle('paddingBottom')) + 'px'

}

return val

} : function(name) {

return getComputedStyle(elem, null)[name]

},

minHeight = parseFloat(getStyle('height'))

elem.style.resize = 'none'

var change = function() {

var scrollTop, height,

padding = 0,

style = elem.style

if (elem._length === elem.value.length) return

elem._length = elem.value.length

if (!isFirefox &&!isOpera) {

padding = parseInt(getStyle('paddingTop')) + parseInt(getStyle('paddingBottom'))

}

scrollTop = document.body.scrollTop || document.documentElement.scrollTop

elem.style.height = minHeight + 'px'

if (elem.scrollHeight >minHeight) {

if (maxHeight &&elem.scrollHeight >maxHeight) {

height = maxHeight - padding

style.overflowY = 'auto'

} else {

height = elem.scrollHeight - padding

style.overflowY = 'hidden'

}

style.height = height + extra + 'px'

scrollTop += parseInt(style.height) - elem.currHeight

document.body.scrollTop = scrollTop

document.documentElement.scrollTop = scrollTop

elem.currHeight = parseInt(style.height)

}

}

// addEvent('propertychange', change)

// addEvent('input', change)

// addEvent('focus', change)

change()

}

pic:z_add.png

要看你的网站是自己电脑作服务器还是租用空间的形式,如是自己电脑,则把文件放到网站目录下就行了,想必你是租用空间的,

这就要看那个空间提供商,允许你采用什么方式传,大部分的都支持ftp,web管理页传,

ftp传的话,下个ftp软件,输入服务器ip,你的帐号,密码(都由空间提供商给你),软件就能打开一个远程目录,把文件拖到该目录就行,

web管理页传的话,用你的帐号,密码登录空间提供商的管理系统(一个网页),之后就跟发邮件附件类似,把文件传到相应目录下就行。

【html】

超文本标记语言,标准通用标记语言下的一个应用。

“超文本”就是指页面内可以包含图片、链接,甚至音乐、程序等非文字元素。

超文本标记语言的结构包括“头”部分(英语:Head)、和“主体”部分(英语:Body),其中“头”部提供关于网页的信息,“主体”部分提供网页的具体内容。