上传文件返回提取码_如何使用提取上传文件

上传文件返回提取码

There’s a task that should be simple, but sometimes it leads to hours of research on the Web: uploading files to the server.

有一项任务应该很简单,但有时会导致在Web上进行大量研究:将文件上传到服务器。

In this tutorial I explain you how to do so using fetch.

在本教程中,我将向您解释如何使用fetch

Given a form with a file input field:

给定一个带有文件输入字段的表单:

<input type="file" id="fileUpload" />

We attach a change event handler on it:

我们在其上附加一个change事件处理程序:

document.querySelector('#fileUpload').addEventListener('change', event => {
  handleImageUpload(event)
})

and we manage the bulk of our logic in the handleImageUpload() function:

并且我们在handleImageUpload()函数中管理大部分逻辑:

const handleImageUpload = event => {
  const files = event.target.files
  const formData = new FormData()
  formData.append('myFile', files[0])

  fetch('/saveImage', {
    method: 'POST',
    body: formData
  })
  .then(response => response.json())
  .then(data => {
    console.log(data)
  })
  .catch(error => {
    console.error(error)
  })
}

In this example we POST to the /saveImage endpoint.

在此示例中,我们发布到/saveImage端点。

We initialize a new FormData object and we assign it to the formData variable, and we append there the uploaded file. If we have more than one file input element, we’d have more than one append() call.

我们初始化一个新的FormData对象,并将其分配给formData变量,然后在此附加上载的文件。 如果我们有多个文件输入元素,那么将有多个调用append()

The data variable inside the second then() will contain the JSON parsed return data. I’m assuming your server will give you JSON as a response.

第二个then()内部的data变量将包含JSON解析的返回数据。 我假设您的服务器将给您JSON作为响应。

How to handle images uploaded server-side is a different topic, one I’ll write about tomorrow.

如何处理在服务器端上传的图像是另一个主题,我明天将写一个主题。

翻译自: https://flaviocopes.com/how-to-upload-files-fetch/

上传文件返回提取码

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
本程序用java编写,运行的时候需要JDK1.5或以上环境,无需安装。程序通过分析CSDN博客源来生成一些必要的数据,可能在以后使用当中出现爬取不了的情况,可能是CSDN的源结构修改了。程序只是用于学习之用,严禁用于非法目的而照成CSDN服务器过载。 由于生成PDF的时候需要依赖字库,所以在打包程序的时候把一些必要的字库已经放到程序中去了。可能在生成一些PDF文件的时候出现乱问题,那是因为里面缺少需要的字库,如遇到这个问题,请和本人联系wyphao.2007@163com #################################################### 程序功能: 1、支持输入CSDN博客用户名针对性下载 2、支持选择保存下载的文件 保存的结构目录为: 选择的保存路径\CSDN用户名\img 该用户的头像保存路径 选择的保存路径\CSDN用户名\pdf 生成的PDF文件保存路径 选择的保存路径\CSDN用户名\doc 生成的DOC文件保存路径 选择的保存路径\CSDN用户名\txt 生成的TXT文件保存路径 3、支持获取用户博客信息 4、支持显示用户所有的帖子列表 5、可以自己选择需要下载的帖子,有全选、反选、重置按钮 6、支持下载的文件保存为 pdf、doc、txt三种格式 7、生成的pdf、doc文件支持图片 8、支持进度显示 #################################################### 制作时间:2012年07月17日 - 2012年07月18日 制 作:w397090770 个人博客:http://blog.csdn.net/w397090770 Email :wyphao.2007@163.com 版权所有,翻版不究 ####################################################
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值