web前端文件上传的几种方式

纯form表单提交

前端代码

<!doctype html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>上传文件</title>
</head>
<body>

<h1>纯form表单提交</h1>

<form action="http://192.168.8.175:8088/upload" enctype="multipart/form-data" method="post">
  <label>参数1:</label>
  <input type="text" name="firstParam" value="" size="12" maxlength="32" /><br />
  
  <label>参数2:</label>
  <input type="text" name="secondParam" value="" size="12" maxlength="32" /><br />
  
  <label>File to stash:</label>
  <input type="file" name="file" required />
  
  <input type="submit" value="提交" />
</form>


<!--<input type="file" οnchange="fileChange()" /> -->

<script type="text/javascript">

</script>
</body>
</html>

== 注意事项 :==
在这里插入图片描述

后台代码

使用springboot搭建工程,编写接收文件上传的接口:

package com.xl.test.pluploader.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

@Controller
public class UploaderController {

	@PostMapping(value = "upload")
	@ResponseBody
	public String upload(@RequestParam("file") MultipartFile file,String firstParam,String secondParam) throws Exception {
		
		// doSomething
		/*
		 * 	这里写业务逻辑 :如对文件file的处理,保存文件等。。。
		 */
		String fileName = file.getOriginalFilename();
		System.out.println(".....fileName= " + fileName);
		long fileSzie = file.getSize();
		System.out.println(".....fileSzie= " + fileSzie);
		byte[] fileBts = file.getBytes();
		System.out.println(".....fileBts= " + fileBts);
		
		System.out.println(".....firstParam= " + firstParam);
		System.out.println(".....secondParam= " + secondParam);
		return "测试完成";
	}
}

== 注意事项:==
在这里插入图片描述

测试

运行后台springboot项目,打开前端form表单html

在这里插入图片描述
在这里插入图片描述

填写参数,选择文件,提交

在这里插入图片描述
在这里插入图片描述

踩的坑

本来纯form表单上传的方式是最简单的,但是却花了最长的时间,原因是在写的过程中,在表单中开始忘记了添加enctype=“multipart/form-data”,然后报错:

org.springframework.web.multipart.MultipartException: Current request is not a multipart request

添加了enctype="multipart/form-data",还报这个错,奇怪了。。。耗时中。。。
很久之后才想起,清除浏览器缓存!,然后没问题了。

纯Form表单提交有个问题是:
上次文件完成之后无法接受后台服务器返回的数据,解决这个问题可以使用FormData对象封装数据,然后使用AJAX提交或者使用XMLHttpRequest对象提交

使用FormData对象封装表单中的数据 XMLHttpRequest对象提交请求

前端代码

编写html

<!doctype html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>上传文件</title>
</head>
<body>

<h1>FormData封装数据 XMLHttpRequest提交请求数据</h1>

<form enctype="multipart/form-data" method="post" name="fileinfo">
  <label>参数1:</label>
  <input type="text" name="firstParam" size="12" maxlength="32" /><br />
  <label>文件上传:</label>
  <input type="file" name="file" required />
  <input type="submit" value="提交" />
</form>
<div></div>


<script type="text/javascript">
var form = document.forms.namedItem("fileinfo");
form.addEventListener('submit', function(ev) {

  var oOutput = document.querySelector("div"),
      oData = new FormData(form);
	  
// 也可通过append方法添加数据,FormData的类似方法可参考https://developer.mozilla.org/en-US/docs/Web/API/FormData
  oData.append("secondParam", "这是第二个参数"); 

  var oReq = new XMLHttpRequest();
  oReq.open("POST", "http://localhost:8080/upload", true);
  oReq.onload = function(oEvent) {
    if (oReq.status == 200) {
		 // 其中类似responseText的属性可参考https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest 
		 alert(oReq.responseText); 
		 // doSomething...
    } else {
      // doSomething...
    }
  };

  oReq.send(oData);
  ev.preventDefault();
}, false);
</script>
</body>
</html>

后台代码

package com.xl.uploadfile.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

@Controller
public class UploadController {

	@PostMapping(value = "upload")
	@ResponseBody
	@CrossOrigin // 处理跨域
	public String upload(@RequestParam("file") MultipartFile file, String firstParam, String secondParam)
			throws Exception {
//	public String test () {

		// doSomething
		/*
		 * 这里写业务逻辑 :如对文件file的处理,保存文件等。。。
		 */
		String fileName = file.getOriginalFilename();
		System.out.println(".....fileName= " + fileName);
		long fileSzie = file.getSize();
		System.out.println(".....fileSzie= " + fileSzie);
		byte[] fileBts = file.getBytes();
		System.out.println(".....fileBts= " + fileBts);

		System.out.println(".....firstParam= " + firstParam);
		System.out.println(".....secondParam= " + secondParam);
		return "测试完成";
	}
}

注意事项

XMLHttpRequest对象提交请求时,涉及到跨域,所以后台使用@CrossOrigin做了跨域请求处理。

测试

使用google打开html页面

在这里插入图片描述

输入数据,提交请求

在这里插入图片描述

结果

在这里插入图片描述

“测试完成” 是后台return "测试完成";返回给页面的,html页面使用XMLHttpRequest对象的responseText属性接受。

后台运行结果

在这里插入图片描述

这种方式可行,就是注意需要处理跨域。参考:https://developer.mozilla.org/en-US/docs/Web/API/FormData/Using_FormData_Objects#Submitting_forms_and_uploading_files_via_AJAX_without_FormData_objects

使用FormData对象封装表单中的数据 使用ajax提交请求

前端代码

<!doctype html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<script src="jquery-3.5.1.min.js"></script>
<title>上传文件</title>
</head>
<body>

<h1>FormData封装数据 Ajax提交请求数据</h1>

<form id="formid" enctype="multipart/form-data" method="post" name="fileinfo">
  <label>参数1:</label>
  <input type="text" name="firstParam" size="12" maxlength="32" /><br />
  <label>文件上传:</label>
  <input type="file" name="file" required />
  <input type="submit" value="提交0" />
</form>
<div></div>


<script type="text/javascript">
//监听表单提交,变为异步提交表单
$("#formid").on("submit", function(event){
    var form = this;//this代表的就是当前提交表单的DOM对象
    //用H5的FormData对象对表单数据进行构造
    var formData = new FormData(form);//FormData构造器接收的是一个form的DOM对象

    $.ajax({
        url: "http://localhost:8080/upload",
        type: "POST",
        data: formData,
        dataType: "JSON",
        async: true,
        //要想用jquery的ajax来提交FormData数据,
        //则必须要把这两项设为false
        processData: false, 
        contentType: false,
        error: function(xhr,status,error){
            alert("请求出错!");
        },
        success: function(result){
            alert(result.msg);
        }
    });

    //阻止表单的提交事件
    return false;
});
</script>
</body>
</html>

后台代码

package com.xl.uploadfile.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

@Controller
public class UploadController {

	@PostMapping(value = "upload")
	@ResponseBody
	@CrossOrigin
	public String upload(@RequestParam("file") MultipartFile file, String firstParam, String secondParam)
			throws Exception {
//	public String test () {

		// doSomething
		/*
		 * 这里写业务逻辑 :如对文件file的处理,保存文件等。。。
		 */
		String fileName = file.getOriginalFilename();
		System.out.println(".....fileName= " + fileName);
		long fileSzie = file.getSize();
		System.out.println(".....fileSzie= " + fileSzie);
		byte[] fileBts = file.getBytes();
		System.out.println(".....fileBts= " + fileBts);

		System.out.println(".....firstParam= " + firstParam);
		System.out.println(".....secondParam= " + secondParam);
		return "{\"msg\": \"测试完成 \"}";
	}
}

注意

  1. 需要引入jquery,这里使用的是`
  2. 后台同样需要处理跨域,使用 @CrossOrigin

使用FormoData 上传二进制文件 blob类型文件

前端代码

编写一个html

<!doctype html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>使用FormoData 上传二进制文件 blob类型文件</title>

<h3>使用FormoData 上传二进制文件 blob类型文件</h3>

<form enctype="multipart/form-data" method="post" name="fileinfo">
  <label>参数1:</label>
  <input type="text" name="firstParam" value="" size="12" maxlength="32" /><br />
  <!--<label>File to stash:</label>
  <input type="file" name="file" required /> -->
  <input type="submit" value="提交" />
</form>
<div></div>

<input type="file" onchange="fileChange()" />

<script type="text/javascript">

var blob = null;

var form = document.forms.namedItem("fileinfo");
form.addEventListener('submit', function(ev) {

  var oOutput = document.querySelector("div"),
      oData = new FormData(form); // 将表单中的数据封装到FormData对象中

  oData.append("secondParam", "第二个参数内容"); //也可以通过FormData的append()方法添加参数。append()方法用法可参考https://developer.mozilla.org/en-US/docs/Web/API/FormData/append
  
  oData.append("file", blob, "springboot.pdf"); // 将blob类型的文件封装到FormData对象中

  var oReq = new XMLHttpRequest();
  oReq.open("POST", "http://localhost:8088/upload", true);
  oReq.onload = function(oEvent) {
    if (oReq.status == 200) {
	alert(oReq.responseText);
    /*oOutput.innerHTML = "Uploaded!"; */
	// doSomething...
    } else {
	// doSomething...
    }
  };

  oReq.send(oData); // 使用XMLHttpRequest提交数据
  ev.preventDefault();
}, false);

function fileChange() { // 将上传的文件转为 blob类型, 实际开发可根据具体业务 获取blob类型的文件数据
  /*let e = e || window.event; */
  var e = e || window.event;
  let file = e.target.files[0];
  let reader = new FileReader();
  let rs = reader.readAsArrayBuffer(file);
 /* let blob = null; */
  reader.onload = (e) => {
    if (typeof e.target.result === 'object') {
      blob = new Blob([e.target.result])
    } else {
      blob = e.target.result
    }
    console.log(Object.prototype.toString.call(blob));
  }
}


</script>
</body>
</html>

后台

package com.xl.test.pluploader.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

@Controller
public class UploaderController {

	@PostMapping(value = "upload")
	@ResponseBody
	@CrossOrigin
	public String upload(@RequestParam("file") MultipartFile file,String firstParam,String secondParam) throws Exception {
		
		// doSomething
		/*
		 * 	这里写业务逻辑 :如对文件file的处理,保存文件等。。。
		 */
		String fileName = file.getOriginalFilename();
		System.out.println(".....fileName= " + fileName);
		long fileSzie = file.getSize();
		System.out.println(".....fileSzie= " + fileSzie);
		byte[] fileBts = file.getBytes();
		System.out.println(".....fileBts= " + fileBts);
		
		System.out.println(".....firstParam= " + firstParam);
		System.out.println(".....secondParam= " + secondParam);
		return "测试完成";
	}
}

测试

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

注意

后台同样需要跨域处理

关于跨域的说明

MND官方说明:
在这里插入图片描述

Cross-site XMLHttpRequest
Modern browsers support cross-site requests by implementing the Cross-Origin Resource Sharing (CORS) standard. As long as the server is configured to allow requests from your web application's origin, XMLHttpRequest will work. Otherwise, an INVALID_ACCESS_ERR exception is thrown.

野鸡翻译:
由于现代浏览器都实现了Cross-Origin Resource Sharing (CORS) 标准,所以这些浏览器都是支持跨域的。只要你的服务器配置了允许跨域请求,XMLHttpRequest对象就能正常工作,否则,浏览器就会抛出INVALID_ACCESS_ERR异常。

使用纯ajax提交文件数据

前端代码

<!doctype html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Sending forms with pure AJAX &ndash; MDN</title>
<script type="text/javascript">

"use strict";

/*\
|*|
|*|  :: XMLHttpRequest.prototype.sendAsBinary() Polyfill ::
|*|
|*|  https://developer.mozilla.org/en-US/docs/DOM/XMLHttpRequest#sendAsBinary()
\*/

if (!XMLHttpRequest.prototype.sendAsBinary) {
  XMLHttpRequest.prototype.sendAsBinary = function(sData) {
    var nBytes = sData.length, ui8Data = new Uint8Array(nBytes);
    for (var nIdx = 0; nIdx < nBytes; nIdx++) {
      ui8Data[nIdx] = sData.charCodeAt(nIdx) & 0xff;
    }
    /* send as ArrayBufferView...: */
    this.send(ui8Data);
    /* ...or as ArrayBuffer (legacy)...: this.send(ui8Data.buffer); */
  };
}

/*\
|*|
|*|  :: AJAX Form Submit Framework ::
|*|
|*|  https://developer.mozilla.org/en-US/docs/DOM/XMLHttpRequest/Using_XMLHttpRequest
|*|
|*|  This framework is released under the GNU Public License, version 3 or later.
|*|  https://www.gnu.org/licenses/gpl-3.0-standalone.html
|*|
|*|  Syntax:
|*|
|*|   AJAXSubmit(HTMLFormElement);
\*/

var AJAXSubmit = (function () {

  function ajaxSuccess () {
    /* console.log("AJAXSubmit - Success!"); */
    console.log(this.responseText);
    /* you can get the serialized data through the "submittedData" custom property: */
    /* console.log(JSON.stringify(this.submittedData)); */
  }

  function submitData (oData) {
    /* the AJAX request... */
    var oAjaxReq = new XMLHttpRequest();
    oAjaxReq.submittedData = oData;
    oAjaxReq.onload = ajaxSuccess;
    if (oData.technique === 0) {
      /* method is GET */
      oAjaxReq.open("get", oData.receiver.replace(/(?:\?.*)?$/,
          oData.segments.length > 0 ? "?" + oData.segments.join("&") : ""), true);
      oAjaxReq.send(null);
    } else {
      /* method is POST */
      oAjaxReq.open("post", oData.receiver, true);
      if (oData.technique === 3) {
        /* enctype is multipart/form-data */
        var sBoundary = "---------------------------" + Date.now().toString(16);
        oAjaxReq.setRequestHeader("Content-Type", "multipart\/form-data; boundary=" + sBoundary);
        oAjaxReq.sendAsBinary("--" + sBoundary + "\r\n" +
            oData.segments.join("--" + sBoundary + "\r\n") + "--" + sBoundary + "--\r\n");
      } else {
        /* enctype is application/x-www-form-urlencoded or text/plain */
        oAjaxReq.setRequestHeader("Content-Type", oData.contentType);
        oAjaxReq.send(oData.segments.join(oData.technique === 2 ? "\r\n" : "&"));
      }
    }
  }

  function processStatus (oData) {
    if (oData.status > 0) { return; }
    /* the form is now totally serialized! do something before sending it to the server... */
    /* doSomething(oData); */
    /* console.log("AJAXSubmit - The form is now serialized. Submitting..."); */
    submitData (oData);
  }

  function pushSegment (oFREvt) {
    this.owner.segments[this.segmentIdx] += oFREvt.target.result + "\r\n";
    this.owner.status--;
    processStatus(this.owner);
  }

  function plainEscape (sText) {
    /* How should I treat a text/plain form encoding?
       What characters are not allowed? this is what I suppose...: */
    /* "4\3\7 - Einstein said E=mc2" ----> "4\\3\\7\ -\ Einstein\ said\ E\=mc2" */
    return sText.replace(/[\s\=\\]/g, "\\$&");
  }

  function SubmitRequest (oTarget) {
    var nFile, sFieldType, oField, oSegmReq, oFile, bIsPost = oTarget.method.toLowerCase() === "post";
    /* console.log("AJAXSubmit - Serializing form..."); */
    this.contentType = bIsPost && oTarget.enctype ? oTarget.enctype : "application\/x-www-form-urlencoded";
    this.technique = bIsPost ?
        this.contentType === "multipart\/form-data" ? 3 : this.contentType === "text\/plain" ? 2 : 1 : 0;
    this.receiver = oTarget.action;
    this.status = 0;
    this.segments = [];
    var fFilter = this.technique === 2 ? plainEscape : escape;
    for (var nItem = 0; nItem < oTarget.elements.length; nItem++) {
      oField = oTarget.elements[nItem];
      if (!oField.hasAttribute("name")) { continue; }
      sFieldType = oField.nodeName.toUpperCase() === "INPUT" ? oField.getAttribute("type").toUpperCase() : "TEXT";
      if (sFieldType === "FILE" && oField.files.length > 0) {
        if (this.technique === 3) {
          /* enctype is multipart/form-data */
          for (nFile = 0; nFile < oField.files.length; nFile++) {
            oFile = oField.files[nFile];
            oSegmReq = new FileReader();
            /* (custom properties:) */
            oSegmReq.segmentIdx = this.segments.length;
            oSegmReq.owner = this;
            /* (end of custom properties) */
            oSegmReq.onload = pushSegment;
            this.segments.push("Content-Disposition: form-data; name=\"" +
                oField.name + "\"; filename=\"" + oFile.name +
                "\"\r\nContent-Type: " + oFile.type + "\r\n\r\n");
            this.status++;
            oSegmReq.readAsBinaryString(oFile);
          }
        } else {
          /* enctype is application/x-www-form-urlencoded or text/plain or
             method is GET: files will not be sent! */
          for (nFile = 0; nFile < oField.files.length;
              this.segments.push(fFilter(oField.name) + "=" + fFilter(oField.files[nFile++].name)));
        }
      } else if ((sFieldType !== "RADIO" && sFieldType !== "CHECKBOX") || oField.checked) {
        /* NOTE: this will submit _all_ submit buttons. Detecting the correct one is non-trivial. */
        /* field type is not FILE or is FILE but is empty */
        this.segments.push(
          this.technique === 3 ? /* enctype is multipart/form-data */
            "Content-Disposition: form-data; name=\"" + oField.name + "\"\r\n\r\n" + oField.value + "\r\n"
          : /* enctype is application/x-www-form-urlencoded or text/plain or method is GET */
            fFilter(oField.name) + "=" + fFilter(oField.value)
        );
      }
    }
    processStatus(this);
  }

  return function (oFormElement) {
    if (!oFormElement.action) { return; }
    new SubmitRequest(oFormElement);
  };

})();

</script>
</head>
<body>

<h1>Sending forms with pure AJAX 分以下几种情况</h1>

<h2>Using the GET method</h2> // 1、 GET方式请求

<form action="register.php" method="get" onsubmit="AJAXSubmit(this); return false;">
  <fieldset>
    <legend>Registration example</legend>
    <p>
      First name: <input type="text" name="firstname" /><br />
      Last name: <input type="text" name="lastname" />
    </p>
    <p>
      <input type="submit" value="Submit" />
    </p>
  </fieldset>
</form>

<h2>Using the POST method</h2> //2 、 POST方式请求
<h3>Enctype: application/x-www-form-urlencoded (default)</h3>

<form action="register.php" method="post" onsubmit="AJAXSubmit(this); return false;">
  <fieldset>
    <legend>Registration example</legend>
    <p>
      First name: <input type="text" name="firstname" /><br />
      Last name: <input type="text" name="lastname" />
    </p>
    <p>
      <input type="submit" value="Submit" />
    </p>
  </fieldset>
</form>

<h3>Enctype: text/plain</h3>  // 3、编码类型为text/plain

<form action="register.php" method="post" enctype="text/plain"
    onsubmit="AJAXSubmit(this); return false;">
  <fieldset>
    <legend>Registration example</legend>
    <p>
      Your name: <input type="text" name="user" />
    </p>
    <p>
      Your message:<br />
      <textarea name="message" cols="40" rows="8"></textarea>
    </p>
    <p>
      <input type="submit" value="Submit" />
    </p>
  </fieldset>
</form>

<h3>Enctype: multipart/form-data</h3> // 4、编码类型为multipart/form-data :上传文件

<form action="register.php" method="post" enctype="multipart/form-data"
    onsubmit="AJAXSubmit(this); return false;">
  <fieldset>
    <legend>Upload example</legend>
    <p>
      First name: <input type="text" name="firstname" /><br />
      Last name: <input type="text" name="lastname" /><br />
      Sex:
      <input id="sex_male" type="radio" name="sex" value="male" />
      <label for="sex_male">Male</label>
      <input id="sex_female" type="radio" name="sex" value="female" />
      <label for="sex_female">Female</label><br />
      Password: <input type="password" name="secret" /><br />
      What do you prefer:
      <select name="image_type">
        <option>Books</option>
        <option>Cinema</option>
        <option>TV</option>
      </select>
    </p>
    <p>
      Post your photos:
      <input type="file" multiple name="photos[]">
    </p>
    <p>
      <input id="vehicle_bike" type="checkbox" name="vehicle[]" value="Bike" />
      <label for="vehicle_bike">I have a bike</label><br />
      <input id="vehicle_car" type="checkbox" name="vehicle[]" value="Car" />
      <label for="vehicle_car">I have a car</label>
    </p>
    <p>
      Describe yourself:<br />
      <textarea name="description" cols="50" rows="8"></textarea>
    </p>
    <p>
      <input type="submit" value="Submit" />
    </p>
  </fieldset>
</form>

</body>
</html>

参考:
https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/Using_XMLHttpRequest#Submitting_forms_and_uploading_files
在这里插入图片描述

web开发神器网站

https://developer.mozilla.org/en-US/docs/Web/API

在这里插入图片描述

  • 4
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一些可能会出现在Web前端开发中级考试中的理论题以及对应的答案: 1. 请简要解释一下什么是 MVC 架构? MVC 架构是一种软件设计模式,它将一个应用程序分为三个部分:模型(Model)、视图(View)和控制器(Controller)。模型负责处理数据和业务逻辑,视图负责将数据呈现给用户,控制器负责协调模型和视图之间的交互。 2. 请简单介绍一下HTML5新特性。 HTML5是HTML的最新版本,它引入了很多新的特性,例如:语义化标签、视频和音频支持、地理定位、Web存储、Web Workers等。 3. 请列举一些CSS3中新增的特性。 CSS3引入了很多新的特性,例如:圆角、阴影、渐变、动画、多列布局、媒体查询等。 4. 请简要说明JavaScript中的几种数据类型。 JavaScript中的数据类型包括:基本类型和引用类型。基本类型包括:字符串、数字、布尔值、undefined和null。引用类型包括:对象、数组、函数等。 5. 请简述 Ajax 的工作原理。 Ajax的工作原理是通过XMLHttpRequest对象向服务器异步发送请求,获取服务器返回的数据,然后通过JavaScript动态更新页面内容,从而实现局部刷新,提高用户体验。 6. 请简单介绍一下 HTTP 协议的几个常见方法。 HTTP协议定义了很多请求方法,其中常见的有:GET、POST、PUT、DELETE、HEAD等。GET用于获取资源,POST用于提交数据,PUT用于更新资源,DELETE用于删除资源,HEAD用于获取资源的元数据。 7. 请简述一下浏览器缓存的工作原理。 浏览器缓存是指浏览器将已经获取的资源存储在本地,下次访问同一资源时直接从本地获取,从而提高访问速度和用户体验。浏览器缓存的工作原理是通过HTTP协议中的响应头来控制缓存行为,例如:Cache-Control、Expires、Last-Modified、ETag等。 8. 请简单介绍一下 Web 安全相关的知识。 Web安全涉及到很多方面,包括:XSS攻击、CSRF攻击、SQL注入、文件上传漏洞、密码安全等。开发者需要采取一系列措施来保障Web应用的安全,例如:输入验证、输出转义、密码加密、HTTPS协议等。 9. 请简述一下前端性能优化的几个方面。 前端性能优化可以从多个方面入手,例如:减少HTTP请求、压缩和合并JavaScript和CSS文件、优化图片、使用CDN、减少DOM操作、使用缓存等。 10. 请简述一下现代前端框架的特点和优势。 现代前端框架的特点和优势包括:提供了一整套解决方案,可以快速构建Web应用;采用组件化的开发方式,可以提高代码的可重用性和可维护性;提供了丰富的工具和插件,可以提高开发效率和代码质量;支持自动化构建和测试,可以提高开发效率和代码质量。常见的现代前端框架包括:React、Vue、Angular等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值