文件上传

文件上传

** file 对象转 url**

  • 只用于本地展示,一般情况下,上传的图片都需要是 file 对象,或 blob 对象等二进制文件。
    • input type=file 上传后。得到的是一个 file 对象。需要使用 window 自带的方法进行转换,以下是兼容性写法

// file 对象转 url

1
2
3
4
5
6
7
8
9
10
11
12
function getFileUrl(res) {
var url;
var agent = navigator.userAgent;
if (agent.indexOf("MSIE") >= 1 || agent.indexOf("NET") !== -1) {
url = window.URL.createObjectURL(res);
} else if (agent.indexOf("Firefox") > 0) {
url = window.URL.createObjectURL(res);
} else if (agent.indexOf("Chrome") > 0) {
url = window.webkitURL.createObjectURL(res);
}
return url;
}

Blob 对象长这样:

Blob 的三种应用场景及 Blob,url,base64 之间的转换

https://blog.csdn.net/qdmoment/article/details/98213994

== Blob 对象转化为 bloburl 对象等一系列 url

var url = window.URL.createObjectURL(‘必须是 Blob,File 或 MediaSource 对象’);

使用 js 将 blob 对象转 file 对象

注意只能转换 blob 对象,不能转换 bloburl, 不然 size 有 63byte,根本不算 file

1
2
3
4
let file = new File([blob], "img.png", {
type: "image/png(MIME类型,必须写对应,不然显示不出来)",
lastModified: Date.now(),
});

blob===> https://blog.csdn.net/weixin_31787977/article/details/112126140

MIME====> https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Basics_of_HTTP/MIME_types

text/plain

text/html

image/jpeg

image/png

audio/mpeg

audio/ogg

audio/*

video/mp4

application/*

application/json

application/javascript

application/ecmascript

application/octet-stream

HTML5 之 FileReader 方法上传并读取文件https://www.cnblogs.com/cckui/p/9411518.html

form 表单中的 enctype=“multipart/form-data“

  • 用于文件上传得 请求头  enctype 默认是: application/x-www-form-urlencoded,不能用于文件上传,只有使用了 multipart/form-data,才能完整的传递文件数据。 multipart/form-data 是将文件以**二进制**的形式上传,这样可以实现多种类型的文件上传。
  • https://blog.csdn.net/lingxiyizhi_ljx/article/details/102514560

本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!