首页 > 其他分享 >文件上传

文件上传

时间:2023-12-16 23:35:06浏览次数:23  
标签:文件 const formData upload ipt 上传

文件上传

流程:

  1. 客户端将文件数据发送给服务器
  2. 服务器保存上传的文件数据到服务器端
  3. 服务器响应给客户端一个文件访问地址

测试地址:http://xxx/api/upload
键的名称(表单域名称):imagefile

请求方法:POST
请求的表单格式:multipart/form-data
请求体中必须包含一个键值对,键的名称是服务器要求的名称,值是文件数据

HTML5 中,JS 仍然无法随意的获取文件数据,但是可以获取到 input 元素中,被用户选中的文件数据
可以利用 HTML5 提供的 FormData 构造函数来创建请求体

举个栗子

<body>
    <img src="" alt="" id="imgAvatar">
    <input type="file" id="avatar">
    <button>上传</button>
  <script>
    const fileServeUrl = 'http://xxx/api/upload';
    const upload = async ()=> {
        const ipt = document.querySelector('#avatar');
        if (ipt.files.length === 0) {
            alert('请选择文件后在上传!');
            return;
        }
        if(!ipt.multiple) { // 不是多图
            const formData = new FormData(); // 构建请求体
            formData.append('imagefile', ipt.files[0]);
            console.log("form", formData);
            const resp = await fetch(fileServeUrl, {
                method: "POST",
                body: formData
            });
            return await resp.json();
        }
    }
    document.querySelector('button').onclick = async ()=> {
        const resp = await upload();
       document.querySelector('img').src = resp.path;
    }
  </script>
</body>

标签:文件,const,formData,upload,ipt,上传
From: https://www.cnblogs.com/bingquan1/p/17908576.html

相关文章

  • 实验6 模板类、文件I/O和异常处理
    任务4#include<iostream>#include<stdexcept>#include<stdlib.h>template<typenameT>classVector{public:friendvoidoutput(constVector<T1>&v);Vector<T>()=default;Vector<T......
  • 第四章:mapper映射文件存放位置、springboot支持事务
    一、mapper映射文件存放位置二、springboot支持事务......
  • 二、SpringBoot的配置文件
    1、核心配置文件properties2、核心配置文件yml3、两种核心配置文件同时存在4、设置maven私服仓库5、多环境下核心配置文件6、多环境下核心配置文件yml7、获取自定义配置8、将自定义配置映射到对象......
  • 一、Spring Boot的概述及pom文件和代码实现
    一、概述和四大特性二、学习创建springboot项目三、项目目录结构和pom文件内容四、springboot继承springmvc-查看springboot父工程pom五、代码的实现......
  • Chrome扩展的核心:manifest 文件(下)
    大家好,我是dom哥。这是我关于Chrome扩展开发的系列文章,感兴趣的可以点个小星星。在上篇和中篇中已经完成了对manifest文件中以下字段的解释:"manifest_version""name""version""description""icons""content_scripts""background""perm......
  • 实验6 模板类、文件I/O和异常处理
    实验任务4Vector.hpp1#pragmaonce23#include<iostream>4#include<stdexcept>56usingnamespacestd;78template<typenameT>910classVector{11private:12T*data;13size_tsize;1415public:16......
  • 西门子 博图 上传 焦作项目
    1、新建项目2、添加设备,选择型号,根据plc上面硬刷信息。 3、博图的版本要都是V15,电控用v15,我(数采)用v18,导致上传报错,必须用v15上传。         erwa.cn二娃备忘......
  • tsconfig.json文件配置
    tsconfig.json配置TypeScript使用tsconfig.json文件作为其配置文件,当一个目录中存在tsconfig.json文件,则认为该目录为TypeScript项目的根目录。基础字段files-设置要编译的文件的名称;['./src/main.tsx']include-设置需要进行编译的文件,支持路径模式匹配;['src']......
  • 实验7_文件应用编程
    task4#define_CRT_SECURE_NO_WARNINGS#include<stdio.h>#include<stdlib.h>intmain(){FILE*fp;charch;intcnt=0;fp=fopen("data4.txt","r");if(fp==NULL){printf("failtoop......
  • Golang 配置文件动态更变(viper)
    一.下载包gogetgithub.com/spf13/viper二.源码1funcLoadConf(fpnamestring){2ini:=viper.New()3ini.SetConfigFile(fpname)45ini.SetDefault("database.dbname","esaletest")6ini.SetDefault("database.dbhos......