Home > Web Front-end > JS Tutorial > body text

JavaScript method to determine file upload type_javascript skills

WBOY
Release: 2016-05-16 16:38:00
Original
1383 people have browsed it

The example in this article shows how JavaScript determines the file upload type, which is a very common technique. The specific implementation method is as follows:

A function is used when uploading files, which is implemented using the input tag of the html element:

<input id="imageFile" name="imageFile1" accept="image/jpg,image/jpeg,image/png,image/bmp,image/gif" type="file"   title="点击选择文件" onchange="imageSubmit(this,0);"/> 

Copy after login

After selecting the image, the onchange event is triggered immediately to upload the image. However, repeatedly selecting the same image will not trigger the onchang event. The solution is as follows:

function imageSubmit(obj, imageType) { 
  if (imageType == "0") { 
  //相关处理代码... 

  //解决上传相同图片不触发onchange事件
  var nf = obj.cloneNode(true);
  nf.value=''; 
  obj.parentNode.replaceChild(nf, obj);
  }
}

Copy after login
The

cloneNode() method is used to create an identical copy of the calling node. The parameter true means performing a deep copy, that is, copying the node and the entire child node tree. When the parameter is false, a shallow copy is performed, that is, Only the node itself is copied. The copy of the node returned after copying is owned by the document, but does not have a parent node assigned to it. Therefore, the node copy becomes an "orphan" unless it is added to the document via appendChild(), insertBefore(), or replaceChild().

I hope this article will help you use JavaScript to design web programs.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template